In the late 1980s, Apple considered using HyperCard's HyperTalk scripting language as the standard language for end-user development across the company and within its classic Mac OS operating system, and for interprocess communication between Apple and non-Apple products.6 HyperTalk could be used by novices to program a HyperCard stack. Apple engineers recognized that a similar, but more object-oriented scripting language could be designed to be used with any application, and the AppleScript project was born as a spin-off of a research effort to modernize the Macintosh as a whole and finally became part of System 7.7
AppleScript was released in October 1993 as part of System 7.1.1 (System 7 Pro, the first major upgrade to System 7).8 QuarkXPress (ver. 3.2) was one of the first major software applications that supported AppleScript. This, in turn, led to AppleScript being widely adopted within the publishing and prepress world, often tying together complex workflows. This was a key factor in retaining the Macintosh's dominant position in publishing and prepress, even after QuarkXpress and other publishing applications were ported to Microsoft Windows.
After some uncertainty about the future of AppleScript on Apple's next generation OS, the move to Mac OS X (around 2002) and its Cocoa frameworks greatly increased the usefulness and flexibility of AppleScript. Cocoa applications allow application developers to implement basic scriptability for their apps with minimal effort, broadening the number of applications that are directly scriptable. At the same time, the shift to the Unix underpinnings and AppleScript's ability to run Unix commands directly, with the do shell script command,9 allowed AppleScripts much greater control over the operating system itself.10: 863 AppleScript Studio, released with Mac OS X 10.2 as part of Xcode, and later AppleScriptObjC framework, released in Mac OS X 10.6, allowed users to build Cocoa applications using AppleScript.11: 969
In a 2006 article, Macworld included AppleScript among its rankings of Apple's 30 most significant products to date, placing it at #17.12
In a 2013 article for Macworld, veteran Mac software developer and commentator John Gruber concluded his reflection on "the unlikely persistence of AppleScript" by noting: "In theory, AppleScript could be much better; in practice, though, it's the best thing we have that works. It exemplifies the Mac's advantages over iOS for tinkerers and advanced users."13
In October 2016, longtime AppleScript product manager and automation evangelist Sal Soghoian left Apple when his position was eliminated "for business reasons".14 Veterans in the Mac community such as John Gruber and Andy Ihnatko generally responded with concern, questioning Apple's commitment to the developer community and pro users.15 Apple senior vice president of software engineering Craig Federighi responded in an email saying that "We have every intent to continue our support for the great automation technologies in macOS!", though Jeff Gamet at The Mac Observer opined that it did little to assuage his doubt about the future of Apple automation in general and AppleScript in particular.16 For the time being, AppleScript remains one component of macOS automation technologies, along with Automator, Shortcuts, Services, and shell scripting.
AppleScript was designed to be used as an accessible end-user scripting language, offering users an intelligent mechanism to control applications, and to access and modify data and documents. AppleScript uses Apple events, a set of standardized data formats that the Macintosh operating system uses to send information to applications, roughly analogous to sending XPath queries over XML-RPC in the world of web services.17: xxvi Apple events allow a script to work with multiple applications simultaneously, passing data between them so that complex tasks can be accomplished without human interaction.18 For example, an AppleScript to create a simple web gallery might do the following:
For the user, hundreds or thousands of steps in multiple applications have been reduced to the single act of running the script, and the task is accomplished in much less time and with no possibility of random human error. A large complex script could be developed to run only once, while other scripts are used again and again.
An application's scriptable elements are visible in the application's Scripting Dictionary (distributed as part of the application), which can be viewed in any script editor. Elements are generally grouped into suites, according to loose functional relationships between them. There are two basic kinds of elements present in any suite: classes and commands.
All scriptable applications share a few basic commands and objects, usually called the Standard Suite—commands to open, close or save a file, to print something, to quit, to set data to variables—as well as a basic application object that gives the scriptable properties of the application itself. Many applications have numerous suites capable of performing any task the application itself can perform. In exceptional cases, applications may support plugins which include their own scripting dictionaries.
AppleScript was designed with the ability to build scripts intuitively by recording user actions. Such AppleScript recordability has to be engineered into the app—the app must support Apple events and AppleScript recording;19 as Finder supports AppleScript recording, it can be useful for reference. When AppleScript Editor (Script Editor) is open and the Record button clicked, user actions for recordable apps are converted to their equivalent AppleScript commands and output to the Script Editor window. The resulting script can be saved and re-run to duplicate the original actions, or modified to be more generally useful.
Whereas Apple events are a way to send messages into applications, AppleScript is a particular language designed to send Apple events. In keeping with the objective of ease-of-use for beginners, the AppleScript language is designed on the natural language metaphor, just as the graphical user interface is designed on the desktop metaphor. A well-written AppleScript should be clear enough to be read and understood by anyone, and easily edited. The language is based largely on HyperCard's HyperTalk language, extended to refer not only to the HyperCard world of cards and stacks, but also theoretically to any document. To this end, the AppleScript team introduced the AppleEvent Object Model (AEOM), which specifies the objects any particular application "knows".
The heart of the AppleScript language is the use of terms that act as nouns and verbs that can be combined. For example, rather than a different verb to print a page, document or range of pages (such as printPage, printDocument, printRange), AppleScript uses a single "print" verb which can be combined with an object, such as a page, a document or a range of pages.
Generally, AEOM defines a number of objects—like "document" or "paragraph"—and corresponding actions—like "cut" and "close". The system also defines ways to refer to properties of objects, so one can refer to the "third paragraph of the document 'Good Day'", or the "color of the last word of the front window". AEOM uses an application dictionary to associate the Apple events with human-readable terms, allowing the translation back and forth between human-readable AppleScript and bytecode Apple events. To discover what elements of a program are scriptable, dictionaries for supported applications may be viewed. (In the Xcode and Script Editor applications, this is under File → Open Dictionary.)
To designate which application is meant to be the target of such a message, AppleScript uses a "tell" construct:
Alternatively, the tell may be expressed in one line by using an infinitive:
For events in the "Core Suite" (activate, open, reopen, close, print, and quit), the application may be supplied as the direct object to transitive commands:
The concept of an object hierarchy can be expressed using nested blocks:
The concept of an object hierarchy can also be expressed using either nested prepositional phrases or a series of possessives:
which in another programming language might be expressed as sequential method calls, like in this pseudocode:
AppleScript includes syntax for ordinal counting, "the first paragraph", as well as cardinal, "paragraph one". Likewise, the numbers themselves can be referred to as text or numerically, "five", "fifth" and "5" are all supported; they are synonyms in AppleScript. Also, the word "the" can legally be used anywhere in the script in order to enhance readability: it has no effect on the functionality of the script.
A "Hello, World!" program can be written as a single line. To show a modal window with "OK" and "Cancel" buttons:
To show a modal window with an "OK" button and an alert icon:
To output audio using a synthesized voice:
This script controls the Finder application to login with username "John" and password "app123":
The development tools below provide a programing environment for scripting with AppleScript – supporting composing, validating, compiling, running and debugging scripts. Some also provide for listing AppleScript dictionaries, saving scripts in a number of formats, syntax highlighting and inserting code snippets.
For development and ad hoc scenarios, a script can be run from a script editor, but to support automation a script must run without opening another application. There are a number of options for doing so:
Re-usable AppleScript modules (available since OS X Mavericks), written in AppleScript or AppleScriptObjC and saved as script files or bundles in certain locations,36 that can be called from other scripts. When saved as a bundle, a library can include an AppleScript dictionary (sdef) file,37 thus functioning like a scripting addition but written in AppleScript or AppleScriptObjC.
A framework for attaching Cocoa interfaces to AppleScript applications, part of the Xcode package in Mac OS X 10.4 and 10.5, now deprecated in favor of AppleScriptObjC.38: 438
A Cocoa development software framework, also called AppleScript/Objective-C or ASOC,39 part of the Xcode package since Mac OS X Snow Leopard.40 AppleScriptObjC allows AppleScripts to use Cocoa classes and methods directly.41 The following table shows the availability of AppleScriptObjC in various versions of macOS:42
AppleScriptObjC can be used in all subsequent Mac OS X versions.
Main article: Automator (software)
A graphical, modular editing environment in which workflows are built up from actions. It is intended to duplicate many of the functions of AppleScript without the necessity for programming knowledge. Automator has an action specifically designed to contain and run AppleScripts, for tasks that are too complex for Automator's simplified framework.43
These background-only applications, packaged with macOS, are used to allow AppleScript to access features that would not normally be scriptable. As of Mac OS X 10.6.3 they include the scriptable applications for:
Plug-ins for AppleScript developed by Apple or third parties.44 They are designed to extend the built-in command set, expanding AppleScript's features and making it somewhat less dependent on functionality provided by applications. macOS includes a collection of scripting additions referred to as Standard Additions (StandardAdditions.osax) that adds a set of commands and classes that are not part of AppleScript's core features, including user interaction dialogs, reading and writing files, file system commands, date functions, and text and mathematical operations; without this OSAX, AppleScript would have no capacity to perform many basic actions not directly provided by an application.
Variables are not strictly typed, and do not need to be declared. Variables can take any data type (including scripts and functions). For example:
A subroutines cannot be called directly from an application tell block. Use of my or of me is required.
Using the same technique for scripting addition commands can reduce errors and improve performance.
A script can define custom data types, or use one of the many built-in classes and objects which are provided by the language and tend to be recognized by scriptable applications. Notable built-in types and objects include:
AppleScript supports compound statement code structure via either single or multiple line syntax. The multiple line syntax ends a code block with a phrase that like end keyword where keyword is the statement keyword at the start of the block. For example:
A script object is full object – encapsulating methods and data and inheriting data and behavior from a parent script. Script objects can use the same 'tell' constructs that are used for application objects and can be loaded from and saved to files. Runtime performance can be enhanced in some cases by using script objects. A script object is defined as:
The loop construct has multiple variations; all using the keyword repeat. The loop can be exited via exit repeat.
For repeat while, the block is executed as long as a condition evaluates to true. The repeat until loop is the same except that the block is executed as long as the condition evaluates to false.
A variable is initialized to a value and after each execution of the block, the variable is incremented by the step value; 1 if not specified.
A variable has the value of each list item as the loop progresses.
A handler, a variation of the block structure defines a subroutine.
Handlers can also be defined using "to" in place of "on" and can be written to accept labeled parameters, not enclosed in parens.
There are four types of predefined handlers in AppleScript—run, open, idle, and quit—each of which is created in the same way as the run handler shown above.
When a script containing an "open handler' is saved as an applet, the applet becomes a droplet. A droplet can be identified in the Finder by its icon, which includes an arrow, indicating items can be dropped onto the icon. The droplet's open handler is executed when files or folders are dropped onto droplet's icon. References to the items dropped on the droplet's icon are passed to the droplet's script as the parameter of the open handler. A droplet can also be launched the same way as an ordinary applet, executing its run handler.
An idle handler can be used in applets or droplets saved as stay-open applets, and is useful for scripts that watch for particular data or events. The length of the idle time is 30 seconds by default,47 but can be changed by including a 'return x' statement at the end of the subroutine, where x is the number of seconds the system should wait before running the handler again.
A comment can be formatted various ways. A line comment begins with -- or alternatively in later versions (AppleScript 2.0, first released in Mac OS X Leopard) with #. The latter permits an AppleScript script to run as an executable if it begins with a shebang line #!/usr/bin/osascript. For example:
A block comment (can be multiple lines) is delimited by (* and *). For example:
AppleScript has several user interface options, including dialogs, alerts, and list of choices. (The character, produced by typing ⌥ Option+return in the Script Editor, denotes continuation of a single statement across multiple lines.)
Each user interaction method can return the values of buttons clicked, items chosen or text entered for further processing. For example:
Apple provides the Open Scripting Architecture (OSA) for other scripting languages and third-party scripting/automation products (such as QuicKeys and UserLand Frontier) to function on an equal status with AppleScript. AppleScript is implemented as a component of Component Manager, and the basic specs for interfacing such components to the OSA are public, allowing other developers to add their own scripting components to the system. Public client APIs for loading, saving and compiling scripts work the same for all such components, which means that applets and droplets can hold scripts in any of those scripting languages.48
One feature of the OSA is scripting additions, or OSAX for Open Scripting Architecture eXtension,49 which were inspired by HyperCard's External Commands. Scripting additions are libraries that allow programmers to extend the function of AppleScript. Commands included as scripting additions are available system-wide, and are not dependent on an application (see also § AppleScript Libraries). The AppleScript Editor is also able to directly edit and run some of the OSA languages.
Under OS X Yosemite and later versions of macOS, the JavaScript for Automation (JXA) component remains the only serious OSA language alternative to AppleScript,50 though the Macintosh versions of Perl, Python, Ruby, and Tcl all support native means of working with Apple events without being OSA components.51: 516
JXA also provides an Objective-C (and C language) foreign language interface.52 Being an environment based on WebKit's JavaScriptCore engine, the JavaScript feature set is in sync with the system Safari browser engine. JXA provides a JavaScript module system and it is also possible to use CommonJS modules via browserify.53
Goldstein, Adam (2005). AppleScript: the missing manual. Sebastopol, CA: O'Reilly Media. ISBN 0596008503. OCLC 56912218. 0596008503 ↩
Sanderson, Hamish; Rosenthal, Hanaan (2009). "Interacting with the Unix Command Line". Learn AppleScript (3rd ed.). Berkeley: Apress. pp. 863–896. doi:10.1007/978-1-4302-2362-7_27. ISBN 9781430223610. OCLC 308193726. 9781430223610 ↩
Flynn, Laurie (February 27, 1989). "Apple Ponders Standardizing on HyperTalk". InfoWorld. Vol. 11, no. 9. p. 31. https://books.google.com/books?id=IToEAAAAMBAJ&pg=PT30 ↩
Cook, William (2007). "AppleScript" (PDF). Proceedings of the third ACM SIGPLAN conference on History of programming languages. Association for Computing Machinery. pp. 1–21. doi:10.1145/1238844.1238845. ISBN 9781595937667. S2CID 220938191. 9781595937667 ↩
"AppleScript Language Guide commands reference: do shell script". developer.apple.com. Retrieved September 15, 2019. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW40 ↩
"Apple's greatest hits: 30 significant products". macworld.com. Retrieved September 12, 2019. https://www.macworld.com/article/1050178/30applelist.html ↩
Gruber, John (March 2013). "The unlikely persistence of AppleScript". Macworld. 30 (3): 100. /wiki/John_Gruber ↩
Lovejoy, Ben (November 17, 2016). "Sal Soghoian says 'ask Apple' about future of Mac user automation as company eliminates position". 9to5mac.com. Retrieved May 8, 2017. https://9to5mac.com/2016/11/17/mac-user-automation-sal-soghoian/ ↩
Evans, Jonny (November 17, 2016). "Does Apple really want to kill Automator, AppleScript? Shock termination of veteran Apple developer guru sends shockwaves across the Mac community". Computerworld.com. Retrieved May 8, 2017. http://www.computerworld.com/article/1676596/does-apple-really-want-to-kill-automator-applescript.html ↩
Gamet, Jeff (November 23, 2016). "Apple's intent isn't the same as committing to mac automation". macobserver.com. Retrieved May 8, 2017. https://www.macobserver.com/analysis/apple-automation-commitment/ ↩
"Scriptable Applications". developer.apple.com. Retrieved July 26, 2018. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptX/Concepts/scriptable_apps.html#//apple_ref/doc/uid/TP40001569-1153888-BAJICJEG ↩
Siracusa, John (October 16, 2014). "OS X 10.10 Yosemite: The Ars Technica Review: JavaScript automation". Ars Technica. Retrieved May 8, 2017. https://arstechnica.com/apple/2014/10/os-x-10-10/24/#javascript-automation ↩
"Smile and SmileLab Home Page". satimage.fr. Retrieved May 8, 2017. http://www.satimage.fr/software/en/ ↩
"ASObjC Explorer 4 Discontinued". macosxautomation.com. Archived from the original on June 21, 2017. Retrieved May 8, 2017. https://web.archive.org/web/20170621152438/http://www.macosxautomation.com/applescript/apps/explorer.html ↩
"Mark Alldritt's Journal » FaceSpan". blog.latenightsw.com. Retrieved May 8, 2017. http://blog.latenightsw.com/?cat=10 ↩
"AppleScript Language Guide: Folder Actions Reference". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_folder_actions.html ↩
Miller, Dan (December 22, 2010). "Capsule review: Hazel 2.3". Macworld.com. Retrieved May 8, 2017. http://www.macworld.com/article/1156565/hazel.html ↩
Beam, Brian (February 10, 2015). "Alfred review: This Mac app launcher continues to shine, but Alfred Remote doesn't stack up". Macworld.com. Retrieved May 10, 2017. http://www.macworld.com/article/2881953/alfred-review-this-mac-app-launcher-continues-to-shine-but-alfred-remote-doesnt-stack-up.html ↩
Frakes, Dan (June 2, 2011). "Capsule review: FastScripts 2.5". Macworld.com. Retrieved May 8, 2017. http://www.macworld.com/article/1160256/fastscripts_25.html ↩
Breen, Christopher (June 4, 2013). "Mac Gems: Keyboard Maestro 6 is a genius at repetitive tasks". Macworld.com. Retrieved May 10, 2017. http://www.macworld.com/article/2040496/mac-gems-keyboard-maestro-6-is-a-genius-at-repetitive-tasks.html ↩
Breen, Christopher (May 7, 2010). "Capsule review: QuicKeys 4". Macworld.com. Retrieved May 8, 2017. http://www.macworld.com/article/1150918/quickeys4_review.html ↩
"AppleScripts – Quicksilver Wiki". qsapp.com. Retrieved May 10, 2017. https://docs.qsapp.com/documentation/applescripts ↩
Fleishman, Glenn (June 12, 2015). "TextExpander 5 review". Macworld.com. Retrieved May 8, 2017. http://www.macworld.com/article/2931533/textexpander-5-review-typing-shortcut-utility-makes-you-more-productive-by-learning-your-habits.html ↩
"Mac Automation Scripting Guide: Using the Systemwide Script Menu". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/UsetheSystem-WideScriptMenu.html ↩
"osascript(1) Mac OS X Manual Page". www.unix.com. Retrieved July 23, 2024. https://www.unix.com/man-page/osx/1/osascript/ ↩
"AppleScript Libraries". macosautomation.com. Archived from the original on July 26, 2020. Retrieved May 8, 2017. https://web.archive.org/web/20200726074006/http://www.macosautomation.com/mavericks/libraries/ ↩
"WWDC 2013 Session 416: Introducing AppleScript Libraries". asciiwwdc.com. Retrieved May 8, 2017. http://asciiwwdc.com/2013/sessions/416 ↩
Neuburg, Matt (2006). AppleScript: the definitive guide (2nd ed.). Beijing: O'Reilly Media. ISBN 0596102119. OCLC 68694976. 0596102119 ↩
Tsai, Michael (September 29, 2014). "AppleScript and Yosemite". mjtsai.com. Retrieved May 9, 2017. https://mjtsai.com/blog/2014/10/29/applescript-and-yosemite/ ↩
"AppleScriptObjC Release Notes". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/releasenotes/ScriptingAutomation/RN-AppleScriptObjC/ ↩
Waldie, Ben (September 6, 2012). "Building a basic AppleScriptObjC (Cocoa-AppleScript) application with Xcode". peachpit.com. Retrieved May 9, 2017. http://www.peachpit.com/articles/article.aspx?p=1942301 ↩
Table adapted from: Naganoya, Takaaki. "ASObjCExtras Scripting Guide" (PDF). piyocast.com. Archived (PDF) from the original on October 9, 2022. Retrieved May 9, 2017. http://www.piyocast.com/download_file/ASObjCExtras_scripting_guide.pdf ↩
"Introduction to Automator AppleScript Actions Tutorial". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/AutomatorTutorialAppleScript/Introduction/Introduction.html ↩
"Technical Note TN1164: Scripting Additions for Mac OS X". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/technotes/tn1164/_index.html ↩
"AppleScript Release Notes: 10.5 Changes". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/mac/library/releasenotes/AppleScript/RN-AppleScript/RN-10_5/RN-10_5.html ↩
"POSIX file — Class Reference — AppleScript Language Guide". developer.apple.com. Retrieved January 8, 2018. https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-SW15 ↩
"AppleScript Language Guide: Handlers in Script Applications". developer.apple.com. Retrieved July 21, 2013. https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW14 ↩
"AppleScript Overview: Open Scripting Architecture". developer.apple.com. Retrieved May 8, 2017. https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptX/Concepts/osa.html ↩
"Importing Scripts". GitHub. JXA-Cookbook. December 6, 2019. Retrieved December 9, 2019. https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/Importing-Scripts ↩