Skip to main content

AppleScript 和 OSA

开放脚本架构 (OSA)[^1] 通过脚本实现 macOS 应用自动化。OSA 最初支持 "AppleScript" 语言。现代 macOS 版本(OSX 10.10 及更高版本)本身支持使用 "JXA"[^2] 的 JavaScript 脚本。

¥Open Scripting Architecture (OSA)[^1] enables macOS app automation with scripts. OSA originally supported the "AppleScript" language. Modern macOS releases (OSX 10.10 and later) natively support JavaScript scripts using "JXA"[^2].

SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。

¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.

此演示使用 OSA Scripts 中的 SheetJS 从电子表格中提取数据。我们将探索如何在 AppleScript 和 JavaScript 脚本中使用 SheetJS 库。"完整演示" 解析工作簿并生成 CSV 行。

¥This demo uses SheetJS in OSA Scripts to pull data from a spreadsheet. We'll explore how to use SheetJS libraries in AppleScript and JavaScript scripts. The "Complete Demo" parses workbooks and generates CSV rows.

测试环境

本 demo 在以下环境下进行了测试:

¥This demo was tested in the following environments:

苹果系统语言日期
14.5AppleScript (OSA)2024-06-30
14.5JavaScript (JXA)2024-06-30

集成细节

¥Integration details

SheetJS 独立脚本 可以从 JS 引擎解析和评估。评估后,将定义 XLSX 全局。JS 存根可以公开 AppleScript 脚本中的方法。

¥The SheetJS Standalone scripts can be parsed and evaluated from the JS engine. Once evaluated, the XLSX global will be defined. A JS stub can expose methods from AppleScript scripts.

以下代码片段将文件读取为二进制字符串:

¥The following snippet reads a file into a binary string:

ObjC.import("Foundation");
function get_bstr(path) {
/* create NSString from the file contents using a binary encoding */
var str = $.NSString.stringWithContentsOfFileEncodingError(path, $.NSISOLatin1StringEncoding, null);
/* return the value as a JS object */
return ObjC.unwrap(str);
}

加载库

¥Loading the Library

假设独立库与源文件位于同一目录中,则可以使用 eval 评估脚本:

¥Assuming the standalone library is in the same directory as the source file, the script can be evaluated with eval:

var src = get_bstr("./xlsx.full.min.js");
eval(src);

解析文件

¥Parsing Files

同样的方法可以用来读取二进制字符串并用 type: "binary" 解析:

¥The same method can be used to read binary strings and parse with type: "binary":

var file = get_bstr("./pres.numbers");
var wb = XLSX.read(file);

完整演示

¥Complete Demo

此示例将从指定的文件名中读取并以 CSV 格式打印第一个工作表数据。

¥This example will read from a specified filename and print the first worksheet data in CSV format.

  1. 下载 SheetJS 独立脚本和测试文件。将这两个文件移动到项目目录:

    ¥Download the SheetJS Standalone script and test file. Move both files to the project directory:

curl -LO https://xlsx.nodejs.cn/pres.numbers
curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
  1. 将以下脚本保存到 sheetosa.js

    ¥Save the following script to sheetosa.js:

sheetosa.js
#!/usr/bin/env osascript -l JavaScript

ObjC.import("Foundation");
function get_bstr(path) {
var str = $.NSString.stringWithContentsOfFileEncodingError(path, $.NSISOLatin1StringEncoding, null);
return ObjC.unwrap(str);
}
eval(get_bstr("./xlsx.full.min.js"));

function run(argv) {
var filedata = get_bstr(argv[0]);
var wb = XLSX.read(filedata, { type: "binary" });
console.log(XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]));
}
  1. 使脚本可执行:

    ¥Make the script executable:

chmod +x sheetosa.js
  1. 运行脚本,将测试文件的路径作为参数传递:

    ¥Run the script, passing the path to the test file as an argument:

./sheetosa.js pres.numbers

如果成功,将打印第一个工作表中的 CSV 行:

¥If successful, CSV rows from the first worksheet will be printed:

Name,Index
Bill Clinton,42
GeorgeW Bush,43
Barack Obama,44
Donald Trump,45
Joseph Biden,46

[^1]: 有关更多详细信息,请参阅 Apple 开发者文档中的 "AppleScript 简介概述"

¥See "Introduction to AppleScript Overview" in the Apple Developer documentation for more details.

[^2]: 有关更多详细信息,请参阅 Apple 开发者文档中的 "用于自动化的 JavaScript 简介发行说明"

¥See "Introduction to JavaScript for Automation Release Notes" in the Apple Developer documentation for more details.