执行 AppleScript

执行 AppleScript 脚本的函数。

签名

有两种方法可以使用该功能。

执行静态脚本时应首选第一个。

function runAppleScript<T>(
  script: string,
  options?: {
    humanReadableOutput?: boolean;
    language?: "AppleScript" | "JavaScript";
    signal?: AbortSignal;
    timeout?: number;
    parseOutput?: ParseExecOutputHandler<T>;
  }
): Promise<T>;

第二个可用于将参数传递给脚本。

function runAppleScript<T>(
  script: string,
  arguments: string[],
  options?: {
    humanReadableOutput?: boolean;
    language?: "AppleScript" | "JavaScript";
    signal?: AbortSignal;
    timeout?: number;
    parseOutput?: ParseExecOutputHandler<T>;
  }
): Promise<T>;

参数

  • script 是要执行的脚本。

  • arguments 是作为参数传递给脚本的字符串数组。

有几个选项:

  • options. humanReadableOutput 是一个布尔值,告诉脚本以什么形式输出。默认情况下,runAppleScript 以人类可读的形式返回其结果:字符串周围没有引号,字符不会转义,列表和记录的大括号被省略等。这通常更有用,但可能会引起歧义。例如,列表 {"foo", "bar"}{{"foo", {"bar"}}} 都将显示为 ‘foo, bar’。要以可重新编译为相同值的明确形式查看结果,请将 humanReadableOutput 设置为 false

  • options.language 是一个字符串,用于指定脚本是使用 AppleScriptarrow-up-right 还是 JavaScript。默认情况下,它会假设它使用 AppleScript

  • options.signal 是一个 Signal 对象,允许您在需要时通过 AbortController 对象中止请求。

  • options.timeout 是一个数字。如果大于 0,则当脚本运行时间超过超时毫秒时,父级将发送信号 `SIGTERM`。默认情况下,执行将在 10000 毫秒(例如 10 秒)后超时。

  • options.parseOutput 是一个函数,它接受脚本的输出作为参数并返回钩子将返回的数据 - 请参阅 ParseExecOutputHandler。默认情况下,该函数将以字符串形式返回 stdout

返回

返回一个默认解析为字符串的 Promise。您可以通过传递 options.parseOutput 来控制它返回的内容。

例子

类型

ParseExecOutputHandler

接受脚本输出作为参数并返回函数将返回的数据的函数。

最后更新于