Raycast 中文文档
  • 介绍
  • 链接
    • 官网
    • 官网 API 文档
    • 社区
    • GitHub
    • Store
    • Icon 生成器
    • 扩展图标模板
  • 基础
    • 起步
    • 创建您的第一个扩展
    • 贡献一个扩展
    • 过审一个扩展
    • 发布一个扩展
    • 调试一个扩展
    • 安装一个扩展
  • 团队
    • 开始
    • 发布私人扩展
    • 协作开发私有扩展
  • 例子
    • Doppler 共享 Secrets
    • Hacker News
    • Todo 列表
    • Spotify Controls
  • 资料
    • 最佳实践
    • 工具
      • CLI
      • ESLint
      • VS Code(社区工具)
    • 文件结构
    • 生命周期
      • 参数
      • 后台刷新
      • Deeplinks
    • Manifest
    • 安全性
    • 术语
    • 版本控制
  • API 参考
    • AI
    • Cache
    • Command
    • Clipboard
    • Environment
    • Feedback
      • Alert
      • HUD
      • Toast
    • Keyboard
    • Menu Bar Commands
    • OAuth
    • Preferences
    • Storage
    • System Utilities
    • 用户界面
      • Action Panel
      • Actions
      • Detail
      • Form
      • List
      • Grid
      • Colors
      • Icons & Images
      • Navigation
    • 窗口 & 搜索栏
  • 公共包
    • 起步
    • 功能
      • 执行 AppleScript
    • 图标
      • getAvatarIcon
      • getFavicon
      • getProgressIcon
    • React hooks
      • useCachedState
      • usePromise
      • useCachedPromise
      • useFetch
      • useForm
      • useExec
      • useSQL
      • useAI
  • 迁移
  • FAQ
由 GitBook 提供支持
在本页
  • 签名
  • 参数
  • 返回
  • 例子
  • 类型
  • ParseExecOutputHandler
  1. 公共包
  2. 功能

执行 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.signal 是一个 Signal 对象,允许您在需要时通过 AbortController 对象中止请求。

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

返回

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

例子

import { showHUD } from "@raycast/api";
import { runAppleScript } from "@raycast/utils";

export default async function () {
  const res = await runAppleScript(
    `
on run argv
  return "hello, " & item 1 of argv & "."
end run
`,
    ["world"]
  );
  await showHUD(res);
}

类型

ParseExecOutputHandler

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

export type ParseExecOutputHandler<T> = (args: {
  /** The output of the script on stdout. */
  stdout: string;
  /** The output of the script on stderr. */
  stderr: string;
  error?: Error | undefined;
  /** The numeric exit code of the process that was run. */
  exitCode: number | null;
  /** The name of the signal that was used to terminate the process. For example, SIGFPE. */
  signal: NodeJS.Signals | null;
  /** Whether the process timed out. */
  timedOut: boolean;
  /** The command that was run, for logging purposes. */
  command: string;
  /** The options passed to the script, for logging purposes. */
  options?: ExecOptions | undefined;
}) => T;
上一页功能下一页图标

最后更新于1年前

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

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

AppleScript
ParseExecOutputHandler