# AppleScript

**AppleScript**是苹果公司创造的一种脚本语言，用户可以用它直接控制可脚本化的Macintosh应用程序以及macOS自身的许多部分，利用脚本自动化重复性任务、组合多个应用程序的功能，并构建复杂的工作流。<sup>[1](https://developer-mdn.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html)</sup> 其决定性特征在于，脚本并不通过应用程序的图形界面来操控它们；相反，一条针对可脚本化应用程序的语句可能会生成一个苹果事件（Apple event），即一条进程间消息，发送给该应用程序。<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一种用于直接控制可脚本化应用程序及macOS可脚本化部分的脚本语言<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup> |
| 底层机制 | 苹果事件，即封装命令、跨越进程边界传送命令并返回回复事件的进程间消息<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup> |
| 可扩展性模型 | 每个可脚本化应用都在脚本词典——即应用包中的`.sdef`文件——中发布自己的命令、类和属性<sup>[3](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AboutScriptingTerminology.html)</sup> |
| 框架 | 开放脚本架构（OSA）支持多种脚本语言；AppleScript和JavaScript组件都位于`System/Library/Components`中<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup> |
| 起源 | HyperCard（1987年）常被视为先驱；苹果事件应用间通信基础先行问世，AppleScript 1.0工具包于1993年发布<sup>[5](https://catalogimages.wiley.com/images/db/pdf/9780470562291.excerpt.pdf)</sup> |
| 方言 | 类似于特定人类语言或编程语言的该语言版本；自AppleScript 1.3起，英语是唯一受支持的方言<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup> |
| 默认回复超时 | 苹果事件等待回复的默认超时为60秒，除非发送方指定了其他值<sup>[6](https://www.apeth.com/rbappscript/03appleevents.html)</sup> |

## 工作原理：苹果事件与开放脚本架构

开放脚本架构为macOS提供了一种通过交换苹果事件进行应用间通信的标准、可扩展机制。可脚本化的应用程序通过执行操作或提供数据来响应苹果事件。任何带有图形用户界面的应用都会在最低限度上响应苹果事件，因为操作系统本身就用它们来指示所有应用执行启动、退出、打开文档和打印等核心任务。<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup>

开放脚本架构框架为脚本组件定义了标准的数据结构、例程和资源，这些组件实现对特定语言的支持。AppleScript和[JavaScript](https://www.edgepedia.org/zh/javascript)组件使得可以用任一种语言控制可脚本化应用，而通过框架的标准接口，一个可脚本化应用可以与任何脚本组件交互，无论其语言为何。该框架还提供了用于编译、执行、加载和存储脚本的API。在底层，苹果事件管理器（实现在CoreServices中的AE框架内）为创建可脚本化应用提供支持；开发者通过Foundation框架中的苹果事件API来使用它。<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup>

**从类英语单词到事件。** 传输中的苹果事件经过紧凑编码，围绕四字母代码构建，这些代码实际上是四个字节，既可作为打包的ASCII字符查看，也可作为整数查看。当脚本中说`tell application "iTunes" to play track 1 of playlist "Library"`时，AppleScript编译器会向iTunes索取其词典，查找“play”、“track”和“playlist”这些词条，替换为它们的四字母代码，并结合表达式的语法用这些代码构成实际的苹果事件，然后交给系统投递。系统会附上回复信封，投递事件，等待目标的回复，并将其返回给发送方；如果发送方未指定超时，则默认适用60秒。<sup>[6](https://www.apeth.com/rbappscript/03appleevents.html)</sup> 苹果事件本身是与语言无关的，因此其他工具（如Ruby的rb-appscript或用于自动化的JavaScript）可以构建并发送相同的事件。<sup>[6](https://www.apeth.com/rbappscript/03appleevents.html)</sup>

## 脚本词典与术语

与拥有固定词汇表的语言不同，AppleScript允许每个可脚本化应用用自己的术语来扩展核心语言。邮件（Mail）引入了用于创建和发送电子邮件的术语；iTunes引入了用于音乐和播放列表的术语。这些术语存放在应用的脚本词典中，即应用包中存储的`.sdef`文件，它描述了该应用支持的命令、类和属性。操作系统的脚本组件、应用自身以及其他交互的脚本都使用这些信息，脚本编辑器可以在你编写脚本时显示词典作为参考。<sup>[3](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AboutScriptingTerminology.html)</sup> 要检查一个应用是否可脚本化，看看它是否有脚本词典即可。<sup>[3](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AboutScriptingTerminology.html)</sup>

不同应用之间的脚本支持程度差异很大，术语也可能随应用或macOS版本的变化而变化，因此重要脚本在升级后应重新测试。<sup>[3](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AboutScriptingTerminology.html)</sup>

**扩展语言。** 脚本附加是全局范围内添加术语的包。macOS自带的“标准附加”（Standard Additions）脚本附加包含了使用剪贴板、显示提示、朗读文本、执行shell脚本等命令，其命令对所有脚本可用。可脚本化的后台应用程序（有时称为代理程序）在没有可见界面的情况下运行，使脚本能够访问系统功能：系统事件（System Events）处理属性列表文件和系统偏好设置，图像事件（Image Events）执行裁剪、旋转和调整大小等基本图像操作。<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup>

## 语言特点

AppleScript提供少量内置命令和一个框架，可脚本化应用中针对具体任务的命令可以插入这个框架。<sup>[1](https://developer-mdn.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html)</sup> 其语法类似英语：复合语句以表示其功能的保留字开头，以`end`结尾；`tell`块设定默认目标，即当未指定对象或对象指定不完整时接收命令的对象。<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup> 应用中的对象通过对象说明符来寻址，这些短语根据包含对象的容器来标识对象；绝对说明符携带足够的信息以唯一标识一个对象，以应用本身作为最外层的容器。<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup>

该语言包含用于条件判断的控制语句、带`repeat`语句和循环变量的循环、带`try`和`error`语句的错误处理，以及事务——一种控制结构，适用于支持将一系列相关事件分组、使得要么所有更改都生效、要么都不生效的应用。<sup>[2](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)</sup> AppleScriptObjC是一座桥梁，让脚本可以使用脚本术语与[Objective-C](https://www.edgepedia.org/zh/objective-c)框架（如Foundation和AppKit）交互，并构建具有Cocoa应用外观和感觉的脚本界面。<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup>

在兼容性方面，为macOS 10.5及更高版本编写的AppleScript 2.0可以使用为1.1到1.10.7任何版本开发的脚本，而用2.0创建的脚本只要避免使用目标版本中不可用的功能，就能在回溯至1.1的任何AppleScript上运行。<sup>[1](https://developer-mdn.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html)</sup>

## 历史

基础先行：苹果在1990年代初开发了苹果事件，作为AppleScript将运行其上的应用间通信层，而1987年发布的[HyperCard](https://www.edgepedia.org/zh/hypercard)常被视为AppleScript的先驱和灵感来源。AppleScript 1.0工具包于1993年发布。<sup>[5](https://catalogimages.wiley.com/images/db/pdf/9780470562291.excerpt.pdf)</sup> 该语言实现为开放脚本架构中的AppleScript脚本组件，而开放脚本架构从一开始就设计为支持AppleScript之外的脚本语言。<sup>[4](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)</sup>

2016年10月，长期担任AppleScript产品经理、被誉为自动化传道者的萨尔·索吉安（Sal Soghoian）离开苹果，其职位“出于商业原因”被撤销。苹果软件工程高级副总裁[克雷格·费德里吉](https://www.edgepedia.org/zh/craig-federighi)（Craig Federighi）在一封电子邮件中回应说：“我们完全有意继续支持macOS中出色的自动化技术！”，尽管一些评论人士对AppleScript的未来尤其表示怀疑。<sup>[7](https://www.boomer.org/macintech/MiT_20250724L.pdf)</sup> 此后，苹果的自动化技术栈不断扩展，在AppleScript之外加入了自动操作（Automator）、用于自动化的JavaScript（JXA）和快捷指令（Shortcuts）；2015年苹果企业演示将AppleScript（含AppleScriptObjC）、用于自动化的JavaScript和自动操作并列为当时的自动化技术。<sup>[8](https://devstreaming-cdn.apple.com/videos/wwdc/2015/306vjwcqnm/306/306_supporting_the_enterprise_with_os_x_automation.pdf)</sup> JXA和AppleScript是苹果为应用自动化提供的两种主要语言。<sup>[9](https://wiki.keyboardmaestro.com/JavaScript_for_Automation)</sup>

## 安全限制

应用沙盒（App Sandbox）是苹果的内核级机制，用于约束应用，它限制了苹果事件的使用。沙盒化的应用无法向其他应用发送苹果事件，除非配置了`scripting-targets`权限或`apple-events`临时例外权限。<sup>[10](https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/AppSandboxTemporaryExceptionEntitlements.html)</sup> 苹果的沙盒文档将向任意应用发送苹果事件列为沙盒化应用被禁止的活动之一，与加载内核扩展、终止其他运行中的应用以及在打开和存储对话框中模拟用户输入并列。<sup>[11](https://developer.apple.com/documentation/security/protecting-user-data-with-app-sandbox)</sup> 苹果2015年2月发布的技术问答备注QA1888记录了沙盒化如何影响可脚本化应用、AppleScript应用或自动操作动作，包括apple-events临时例外权限。<sup>[12](https://developer-mdn.apple.com/library/archive/qa/qa1888/_index.html)</sup>

## AppleScript在macOS自动化工具中的位置

AppleScript与macOS的其他自动化工具有重叠，但不可互换。实用建议是：对于可能在iPhone、iPad和Mac上运行的新的可视化工作流，从快捷指令入手；对于现有的Mac工作流，保留自动操作；当可脚本化的Mac应用提供了快捷指令所不具备的命令时，则使用AppleScript；这些工具可以组合使用，由快捷指令调用脚本。<sup>[13](https://wearemacwise.com/blog/shortcuts-vs-automator-vs-applescript-which-to-use/)</sup> 自动操作本身就是一个可脚本化应用，可以由AppleScript和用于自动化的JavaScript命令控制。<sup>[14](https://support.apple.com/en-am/guide/automator/aut4bb6b2b4f/mac)</sup> `do shell script`命令由JXA从AppleScript继承而来，让脚本可以执行shell命令，但它有局限：标准错误和标准输出中只能方便地访问一个，且换行符以回车符形式返回。<sup>[15](https://github-wiki-see.page/m/JXA-Cookbook/JXA-Cookbook/wiki/Shell-and-CLI-Interactions)</sup>

## References

1. [Introduction to AppleScript Language Guide, Apple Developer Documentation Archive](https://developer-mdn.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html)
2. [AppleScript Language Guide: Glossary, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_glossary.html)
3. [Mac Automation Scripting Guide: About Scripting Terminology, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AboutScriptingTerminology.html)
4. [Mac Automation Scripting Guide: How Mac Scripting Works, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/HowMacScriptingWorks.html)
5. [AppleScript history timeline, Wiley book excerpt](https://catalogimages.wiley.com/images/db/pdf/9780470562291.excerpt.pdf)
6. [Matt Neuburg, Chapter 3: Apple Events, *Scripting Mac Applications With Ruby* draft](https://www.apeth.com/rbappscript/03appleevents.html)
7. [Sal Soghoian departure and Apple response, MacInTech notes (July 2025)](https://www.boomer.org/macintech/MiT_20250724L.pdf)
8. [Supporting the Enterprise with OS X Automation, WWDC 2015 session notes](https://devstreaming-cdn.apple.com/videos/wwdc/2015/306vjwcqnm/306/306_supporting_the_enterprise_with_os_x_automation.pdf)
9. [JavaScript for Automation, Keyboard Maestro Wiki](https://wiki.keyboardmaestro.com/JavaScript_for_Automation)
10. [App Sandbox Temporary Exception Entitlements, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/AppSandboxTemporaryExceptionEntitlements.html)
11. [Protecting user data with App Sandbox, Apple Developer Documentation](https://developer.apple.com/documentation/security/protecting-user-data-with-app-sandbox)
12. [Technical Q&A QA1888: Sandboxing and Automation in OS X, Apple Developer Documentation Archive](https://developer-mdn.apple.com/library/archive/qa/qa1888/_index.html)
13. [Shortcuts, Automator, or AppleScript: Which Should You Use?, We Are MacWise](https://wearemacwise.com/blog/shortcuts-vs-automator-vs-applescript-which-to-use/)
14. [Use scripts with Automator on Mac, Apple Support](https://support.apple.com/en-am/guide/automator/aut4bb6b2b4f/mac)
15. [Shell and CLI Interactions, JXA-Cookbook wiki](https://github-wiki-see.page/m/JXA-Cookbook/JXA-Cookbook/wiki/Shell-and-CLI-Interactions)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/applescript

English original: https://www.edgepedia.org/applescript
