Recent from talks
Nothing was collected or created yet.
AppleScript
View on Wikipedia
| AppleScript | |
|---|---|
| Paradigm | Natural language programming, Scripting |
| Developer | Apple Inc. |
| First appeared | 1993[1] |
| Stable release | |
| Typing discipline | Weak, dynamic |
| OS | System 7, Mac OS 8, Mac OS 9, macOS |
| License | Proprietary (parts available under APSL) |
| Filename extensions | .scpt, .scptd, .applescript |
| Website | developer |
| Influenced by | |
| Natural language, HyperTalk | |
AppleScript is a scripting language created by Apple Inc. that facilitates automated control of Mac applications. First introduced in System 7, it is currently included in macOS in a package of automation tools. The term AppleScript may refer to the scripting language, to a script written in the language, or to the macOS Open Scripting Architecture that underlies the language.[4][5]
AppleScript is primarily a mechanism for driving Apple events – an inter-application communication (IAC) technology that exchanges data between and controls applications.[4][5] Additionally, AppleScript supports basic calculations and text processing, and is extensible via scripting additions that add functions to the language.
AppleScript is tightly bound to the Mac environment, similar to how Windows Script Host is bound to the Windows environment. In other words, AppleScript is not a general purpose scripting language like Python. One way that AppleScript is bound to the unique aspects of its environment is that it relies on applications to publish dictionaries of addressable objects and operations.
As is typical of a command language, AppleScript is not designed to directly perform intensive processing. For example, a script cannot efficiently perform intensive math operations or complicated text processing. However, AppleScript can be used in combination with other tools and technologies which allows it to leverage more efficient programming contexts.
The language has aspects of structured, procedural, object-oriented and natural language programming, but does not strictly conform to any of these paradigms.[5]: xxvi
History
[edit]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.[1]
AppleScript was released in October 1993 as part of System 7.1.1 (System 7 Pro, the first major upgrade to System 7).[1] 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,[7] allowed AppleScripts much greater control over the operating system itself.[5]: 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.[5]: 969
In a 2006 article, Macworld included AppleScript among its rankings of Apple's 30 most significant products to date, placing it at #17.[8]
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."[9]
In October 2016, longtime AppleScript product manager and automation evangelist Sal Soghoian left Apple when his position was eliminated "for business reasons".[10] 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.[11] 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.[12] For the time being, AppleScript remains one component of macOS automation technologies, along with Automator, Shortcuts, Services, and shell scripting.
Intent
[edit]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.[5]: 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.[4] For example, an AppleScript to create a simple web gallery might do the following:
- Open a photo in a photo-editing application (by sending that application an Open File Apple event).
- Tell the photo-editing application to manipulate the image (e.g. reduce its resolution, add a border, add a photo credit)
- Tell the photo-editing application to save the changed image in a file in some different folder (by sending that application a Save and/or Close Apple event).
- Send the new file path (via another Apple event) to a text editor or web editor application.
- Tell that editor application to write a link for the photo into an HTML file.
- Repeat the above steps for an entire folder of images (hundreds or even thousands of photos).
- Upload the HTML file and folder of revised photos to a website, by sending Apple events to a graphical FTP client, by using built-in AppleScript commands, or by sending Apple events to Unix FTP utilities.
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.
- Classes are scriptable objects—for example, a text editing application will almost certainly have classes for windows, documents, and texts—and these classes will have properties that can be changed (window size, document background color, text font size, etc.), and may contain other classes (a window will contain one or more documents, a document will contain text, a text object will contain paragraphs and words and characters).
- Commands, by contrast, are instructions that can be given to scriptable objects. The general format for a block of AppleScript is to tell a scriptable object to run a command.
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;[13] 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.
Natural language metaphor
[edit]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.
print page 1
print document 2
print pages 1 thru 5 of document 2
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:
tell application "Microsoft Word"
quit
end tell
Alternatively, the tell may be expressed in one line by using an infinitive:
tell application "Microsoft Word" to quit
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:
quit application "Microsoft Word"
The concept of an object hierarchy can be expressed using nested blocks:
tell application "QuarkXPress"
tell document 1
tell page 2
tell text box 1
set word 5 to "Apple"
end tell
end tell
end tell
end tell
The concept of an object hierarchy can also be expressed using either nested prepositional phrases or a series of possessives:
pixel 7 of row 3 of TIFF image "my bitmap"
TIFF image "my bitmap"'s 3rd row's 7th pixel
which in another programming language might be expressed as sequential method calls, like in this pseudocode:
getTIFF("my bitmap").getRow(3).getPixel(7);
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.
Examples
[edit]Hello, world!
[edit]A "Hello, World!" program can be written as a single line. To show a modal window with "OK" and "Cancel" buttons:
display dialog "Hello, world!"
To show a modal window with an "OK" button and an alert icon:
display alert "Hello, world!"
To output audio using a synthesized voice:
say "Hello, world!"
Failsafe calculator
[edit]tell application "Finder"
-- Set variables
set the1 to text returned of (display dialog "1st" default answer "Number here" buttons {"Continue"} default button 1)
set the2 to text returned of (display dialog "2nd" default answer "Number here" buttons {"Continue"} default button 1)
try
set the1 to the1 as integer
set the2 to the2 as integer
on error
display dialog "You may only input numbers into a calculator." with title "ERROR" buttons {"OK"} default button 1
return
end try
-- Add?
if the button returned of (display dialog "Add?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 + the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Subtract?
else if the button returned of (display dialog "Subtract?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 - the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Multiply?
else if the button returned of (display dialog "Multiply?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 * the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
-- Divide?
else if the button returned of (display dialog "Divide?" buttons {"No", "Yes"} default button 2) is "Yes" then
set ans to (the1 / the2)
display dialog ans with title "Answer" buttons {"OK"} default button 1
say ans
else
delay 1
say "You haven't selected a function. The operation has cancelled."
end if
end tell
Drive login
[edit]This script controls the Finder application to login with username "John" and password "app123":
tell application "Finder"
set passAns to "app123"
set userAns to "John"
if the text returned of (display dialog "Username" default answer "") is userAns then
display dialog "Correct" buttons {"Continue"} default button 1
if the text returned of (display dialog "Username : John" & return & "Password" default answer "" buttons {"Continue"} default button 1 with hidden answer) is passAns then
display dialog "Access granted" buttons {"OK"} default button 1
else
display dialog "Incorrect password" buttons {"OK"} default button 1
end if
else
display dialog "Incorrect username" buttons {"OK"} default button 1
end if
end tell
Development tools
[edit]Script editors
[edit]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.
- AppleScript Editor
- An editor from Apple packaged with macOS, called AppleScript Editor in Mac OS X Snow Leopard (10.6) through OS X Mavericks (10.9) and Script Editor in all earlier and later versions of macOS. Scripts are written in document editing windows where they can be compiled and run, and these windows contain various panes in which logged information, execution results, and other information is available for debugging purposes. Access to scripting dictionaries and prewritten code snippets is available through the application menus. Since OS X Yosemite (10.10), Script Editor includes the ability to write in both AppleScript and JavaScript.[14]
- Xcode
- A suite of tools from Apple for developing applications with features for editing scripts and creating standalone applications written in AppleScript.
- Script Debugger
- A commercial IDE from Late Night Software, Script Debugger is a relatively advanced AppleScript environment that allows the script writer to debug AppleScripts via single stepping, breakpoints, stepping in and out of functions/subroutines, variable tracking, etc. Script Debugger also contains an advanced dictionary browser that allows the user to see the dictionary in action in real world situations. That is, rather than just a listing of what the dictionary covers, one can open a document in Pages, for example, and see how the dictionary's terms apply to that document, making it easier to determine which parts of the dictionary to use. Script Debugger is not designed to create scripts with a GUI, other than basic alerts and dialogs, but is focused more on the coding and debugging of scripts.
- Smile and SmileLab
- A third-party freeware/commercial IDE for AppleScript, itself written entirely in AppleScript.[15] Smile is free, and primarily designed for AppleScript development. SmileLab is commercial software with extensive additions for numerical analysis, graphing, machine automation and web production. Smile and SmileLab use an assortment of different windows—AppleScript windows for running and saving full scripts, AppleScript terminals for testing code line-by-line, unicode windows for working with text and XML. Users can create complex interfaces—called dialogs—for situations where the built-in dialogs in AppleScript are insufficient.
- ASObjC Explorer 4
- A discontinued commercial IDE from Shane Stanley for AppleScript, especially for AppleScriptObjC.[16] The main feature is Cocoa-object/event logging, debugging and code-completion. Users can read Cocoa events and objects like other scriptable applications. This tool was originally built for AppleScript Libraries (available in OS X Mavericks). AppleScript Libraries aims for re-usable AppleScript components and supports built-in AppleScript dictionary (sdef). ASObjC Explorer 4 can be an external Xcode script editor, too.
- FaceSpan
- A discontinued commercial IDE from Late Night Software for creating AppleScript applications with graphic user interfaces.[17]
Script launchers
[edit]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:
- Applets
- AppleScripts can be saved from a script editor as applications (called applets, or droplets when they accept input via drag and drop).[5]: 69 Applets can be run from the Dock, from the toolbar of Finder windows, from Spotlight, from third-party application launchers, or from any other place where applications can be run.
- Folder actions
- Using AppleScript folder actions, scripts can be launched when specific changes occur in folders (such as adding or removing files).[18] Folder actions can be assigned by clicking on a folder and choosing Folder Actions Setup... from the contextual menu; the location of this command differs slightly in Mac OS X 10.6.x from earlier versions. This same action can be achieved with third-party utilities such as Hazel.[19]
- Hotkey launchers
- Keyboard shortcuts can be assigned to AppleScripts in the script menu using the Keyboard & Mouse Settings Preference Pane in System Preferences. In addition, various third-party utilities are available—Alfred,[20] FastScripts,[21] Keyboard Maestro,[22] QuicKeys,[23] Quicksilver,[24] TextExpander[25]—which can run AppleScripts on demand using key combinations.
- Script menu
- This system-wide menu provides access to AppleScripts from the macOS menu bar, visible no matter what application is running.[26] (In addition, many Apple applications, some third-party applications, and some add-ons provide their own script menus. These may be activated in different ways, but all function in essentially the same manner.) Selecting a script in the script menu launches it. Since Mac OS X 10.6.x, the system-wide script menu can be enabled from the preferences of Script Editor; in prior versions of Mac OS X, it could be enabled from the AppleScript Utility application. When first enabled, the script menu displays a default library of fairly generic, functional AppleScripts, which can also be opened in Script Editor and used as examples for learning AppleScript. Scripts can be organized so that they only appear in the menu when particular applications are in the foreground.
- Unix command line and launchd
- AppleScripts can be run from the Unix command line, or from launchd for scheduled tasks,[5]: 716 by using the osascript command line tool.[27] The osascript tool can run compiled scripts (.scpt files) and plain text files (.applescript files—these are compiled by the tool at runtime). Script applications can be run using the Unix open command.
Resources
[edit]AppleScript Libraries
[edit]Re-usable AppleScript modules (available since OS X Mavericks), written in AppleScript or AppleScriptObjC and saved as script files or bundles in certain locations,[28] that can be called from other scripts. When saved as a bundle, a library can include an AppleScript dictionary (sdef) file,[29] thus functioning like a scripting addition but written in AppleScript or AppleScriptObjC.
AppleScript Studio
[edit]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.[30]: 438
AppleScriptObjC
[edit]A Cocoa development software framework, also called AppleScript/Objective-C or ASOC,[31] part of the Xcode package since Mac OS X Snow Leopard.[32] AppleScriptObjC allows AppleScripts to use Cocoa classes and methods directly.[33] The following table shows the availability of AppleScriptObjC in various versions of macOS:[34]
| OS version | In Xcode | In applets | In AppleScript Libraries |
In Script Editor |
|---|---|---|---|---|
| 10.6 | ||||
| 10.7 | ||||
| 10.8 | ||||
| 10.9 | ||||
| 10.10 |
AppleScriptObjC can be used in all subsequent Mac OS X versions.
Automator
[edit]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.[35]
Scriptable core system applications
[edit]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:
- VoiceOver (scriptable auditory and braille screen reader package)
- System Events (control of non-scriptable applications and access to certain system functions and basic file operations)
- Printer Setup Utility (scriptable utility for handling print jobs)
- Image Events (core image manipulation)
- HelpViewer (scriptable utility for showing help displays)
- Database Events (minimal SQLite3 database interface)
- AppleScript Utility (for scripting a few AppleScript related preferences)
Scripting Additions (OSAX)
[edit]Plug-ins for AppleScript developed by Apple or third parties.[36] 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.
Language
[edit]Typing
[edit]Variables are not strictly typed, and do not need to be declared. Variables can take any data type (including scripts and functions). For example:
-- create an integer variable called variable1
set variable1 to 1
-- create a text variable called variable2
set variable2 to "Hello"
-- create a list variable called variable3
copy {17, "doubleday"} to variable3
-- copy the list items of variable3 into separate variables variable4 and variable5
set {variable4, variable5} to variable3
-- set a variable to an instance of a script
set variable6 to script myScript
Scoping
[edit]A subroutines cannot be called directly from an application tell block. Use of my or of me is required.
tell application "Finder"
set x to my myHandler()
-- or
set x to myHandler() of me
end tell
on myHandler()
--commands
end myHandler
Using the same technique for scripting addition commands can reduce errors and improve performance.
tell application "Finder"
set anyNumber to my (random number from 5 to 50)
end tell
Types and objects
[edit]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:
- Basic objects
- application: used mostly as a specifier for tell statements (
tell application "Finder" …) - script: script objects are containers for scripts; every AppleScript creates a script object when run, and script objects may be created within AppleScripts
- class: meta-object that specifies the type of other objects
- reference: object that encapsulates an unevaluated object specifier that may or may not point to a valid object; can be evaluated on-demand by accessing its
contentsproperty
- Standard data objects
- constant: constant value; language-defined constants include
pi,tabandlinefeed - boolean: Boolean (true/false) value; subclass of
constant - number: abstract superclass of
integerandreal; rarely used directly - integer: integer; can be manipulated with built-in mathematical operators
- real: floating-point (real) number; can be manipulated with built-in mathematical operators
- date: date and time
- text: before AppleScript 2.0 (Mac OS X 10.4 and below) the
textclass was distinct fromstringandUnicode text, and the three behaved somewhat differently; in 2.0 (10.5) and later, they are all synonyms and all text is handled as UTF-16[37]
- Containers
- list: ordered list of objects; can contain any class, including other lists and classes defined by applications
- record: keyed list of objects; like a list, except structured as key–value pairs; runtime keyed access is unsupported; all keys must be compile-time constant identifiers
- File system
- alias: reference to an existing file system object (file or folder); maintains link to file system object if moved or renamed
- file: reference to a file system object; can can refer to an object that does not exist
- POSIX file: reference to a file system object, in plain text, using Unix (POSIX)-style slash (/) notation; not a true data type, as AppleScript automatically converts a POSIX file to an ordinary file[38]
- Miscellaneous
- RGB color: specifies an RGB triplet (in 16-bit high color format), for use in commands and objects that work with colors
- unit types: converts between standard units; for instance, a value can be defined as
square yards, then converted tosquare feetby casting between unit types using theasoperator
Block
[edit]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:
-- Simple form
tell application "Safari" to activate
-- Compound
tell application "MyApp"
-- commands for app
end tell
Script
[edit]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:
script scriptName
-- commands and handlers specific to the script
end script
Loop
[edit]The loop construct has multiple variations; all using the keyword repeat. The loop can be exited via exit repeat.
- Unconditional
repeat
-- commands to be repeated
end repeat
- Repeat a number of times
repeat 10 times
-- commands to be repeated
end repeat
- Conditional
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.
set x to 5
repeat while x > 0
set x to x - 1
end repeat
set x to 5
repeat until x ≤ 0
set x to x - 1
end repeat
- With a variable
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.
-- repeat the block 2000 times, i gets all values from 1 to 2000
repeat with i from 1 to 2000
-- commands to be repeated
end repeat
-- repeat the block 4 times, i gets values 100, 75, 50 and 25
repeat with i from 100 to 25 by -25
-- commands to be repeated
end repeat
- Enumerate
A variable has the value of each list item as the loop progresses.
set total to 0
repeat with loopVariable in {1, 2, 3, 4, 5}
set total to total + loopVariable
end repeat
Handler
[edit]A handler, a variation of the block structure defines a subroutine.
- Function handler
on myFunction(parameters...)
-- subroutine commands
end myFunction
- Folder actions block
on adding folder items to thisFolder after receiving theseItems
-- commands to apply to the folder or items
end adding folder items to
- Run handler
on run
-- commands
end run
Handlers can also be defined using "to" in place of "on" and can be written to accept labeled parameters, not enclosed in parens.
- Handler with labeled parameters
on rock around the clock
display dialog (clock as string)
end rock
-- called with:
rock around the current date
- Handler using "to" and labeled parameters
to check for yourNumber from bottom thru top
if bottom ≤ yourNumber and yourNumber ≤ top then
display dialog "Congratulations! You scored."
end if
end check
--called with:
check for 8 from 7 thru 10
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.
- Run handler
- Defines the main code of the script, which is called when the script is run. Run handler blocks are optional, unless arguments are being passed to the script. If an explicit run handler block is omitted, then all code that is not contained inside handler blocks is executed as though it were in an implicit run handler.
- Open handler
- Defined using "on open theItems".
on open theItems
repeat with thisItem in theItems
tell application "Finder" to update thisItem
end repeat
end open
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.
- Idle handler
- A subroutine that is run periodically by the system when the application is idle.
on idle
--code to execute when the script's execution has completed
return 60 -- number of seconds to pause before executing idle handler again
end idle
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,[39] 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.
- Quit handler
- A handler that is run when the applet receives a Quit request. This can be used to save data or do other ending tasks before quitting.
on quit
--commands to execute before the script quits
continue quit -- required for the script to actually quit
end quit
Comment
[edit]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:
--This is a line comment
# So is this! (in later versions)
A block comment (can be multiple lines) is delimited by (* and *). For example:
(* This is a
multiple
line
comment *)
User interaction
[edit]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.)
-- Dialog
set dialogReply to display dialog "Dialog Text"
default answer "Text Answer"
hidden answer false
buttons {"Skip", "Okay", "Cancel"}
default button "Okay"
cancel button "Skip"
with title "Dialog Window Title"
with icon note
giving up after 15
-- Choose from list
set chosenListItem to choose from list {"A", "B", "3"}
with title "List Title"
with prompt "Prompt Text"
default items "B"
OK button name "Looks Good!"
cancel button name "Nope, try again"
multiple selections allowed false
with empty selection allowed
-- Alert
set resultAlertReply to display alert "Alert Text"
as warning
buttons {"Skip", "Okay", "Cancel"}
default button 2
cancel button 1
giving up after 2
Each user interaction method can return the values of buttons clicked, items chosen or text entered for further processing. For example:
display alert "Hello, world!" buttons {"Rudely decline", "Happily accept"}
set theAnswer to button returned of the result
if theAnswer is "Happily accept" then
beep 5
else
say "Piffle!"
end if
Open Scripting Architecture
[edit]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.[40]
One feature of the OSA is scripting additions, or OSAX for Open Scripting Architecture eXtension,[36] 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.
JavaScript for Automation
[edit]This section needs expansion with: comparison of JXA and AppleScript scripting. You can help by adding to it. (May 2017) |
Under OS X Yosemite and later versions of macOS, the JavaScript for Automation (JXA) component remains the only serious OSA language alternative to AppleScript,[14] though the Macintosh versions of Perl, Python, Ruby, and Tcl all support native means of working with Apple events without being OSA components.[30]: 516
JXA also provides an Objective-C (and C language) foreign language interface.[14] 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.[41]
See also
[edit]- ARexx – Interpreter for the Rexx language on an Amiga computer
References
[edit]- ^ a b c 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.
- ^ Apple Inc. (2022). Script Editor (Version 2.11). Retrieved November 11, 2022.
- ^ "OS X 10.10 Yosemite release date". October 16, 2014. Retrieved November 16, 2014.
- ^ a b c Goldstein, Adam (2005). AppleScript: the missing manual. Sebastopol, CA: O'Reilly Media. ISBN 0596008503. OCLC 56912218.
- ^ a b c d e f g h 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.
- ^ Flynn, Laurie (February 27, 1989). "Apple Ponders Standardizing on HyperTalk". InfoWorld. Vol. 11, no. 9. p. 31.
- ^ "AppleScript Language Guide commands reference: do shell script". developer.apple.com. Retrieved September 15, 2019.
- ^ "Apple's greatest hits: 30 significant products". macworld.com. Retrieved September 12, 2019.
- ^ Gruber, John (March 2013). "The unlikely persistence of AppleScript". Macworld. 30 (3): 100.
- ^ 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.
- ^ 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.
- ^ Gamet, Jeff (November 23, 2016). "Apple's intent isn't the same as committing to mac automation". macobserver.com. Retrieved May 8, 2017.
- ^ "Scriptable Applications". developer.apple.com. Retrieved July 26, 2018.
- ^ a b c Siracusa, John (October 16, 2014). "OS X 10.10 Yosemite: The Ars Technica Review: JavaScript automation". Ars Technica. Retrieved May 8, 2017.
- ^ "Smile and SmileLab Home Page". satimage.fr. Retrieved May 8, 2017.
- ^ "ASObjC Explorer 4 Discontinued". macosxautomation.com. Archived from the original on June 21, 2017. Retrieved May 8, 2017.
- ^ "Mark Alldritt's Journal » FaceSpan". blog.latenightsw.com. Retrieved May 8, 2017.
- ^ "AppleScript Language Guide: Folder Actions Reference". developer.apple.com. Retrieved May 8, 2017.
- ^ Miller, Dan (December 22, 2010). "Capsule review: Hazel 2.3". Macworld.com. Retrieved May 8, 2017.
- ^ 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.
- ^ Frakes, Dan (June 2, 2011). "Capsule review: FastScripts 2.5". Macworld.com. Retrieved May 8, 2017.
- ^ Breen, Christopher (June 4, 2013). "Mac Gems: Keyboard Maestro 6 is a genius at repetitive tasks". Macworld.com. Retrieved May 10, 2017.
- ^ Breen, Christopher (May 7, 2010). "Capsule review: QuicKeys 4". Macworld.com. Retrieved May 8, 2017.
- ^ "AppleScripts – Quicksilver Wiki". qsapp.com. Retrieved May 10, 2017.
- ^ Fleishman, Glenn (June 12, 2015). "TextExpander 5 review". Macworld.com. Retrieved May 8, 2017.
- ^ "Mac Automation Scripting Guide: Using the Systemwide Script Menu". developer.apple.com. Retrieved May 8, 2017.
- ^ "osascript(1) Mac OS X Manual Page". www.unix.com. Retrieved July 23, 2024.
- ^ "AppleScript Libraries". macosautomation.com. Archived from the original on July 26, 2020. Retrieved May 8, 2017.
- ^ "WWDC 2013 Session 416: Introducing AppleScript Libraries". asciiwwdc.com. Retrieved May 8, 2017.
- ^ a b Neuburg, Matt (2006). AppleScript: the definitive guide (2nd ed.). Beijing: O'Reilly Media. ISBN 0596102119. OCLC 68694976.
- ^ Tsai, Michael (September 29, 2014). "AppleScript and Yosemite". mjtsai.com. Retrieved May 9, 2017.
- ^ "AppleScriptObjC Release Notes". developer.apple.com. Retrieved May 8, 2017.
- ^ Waldie, Ben (September 6, 2012). "Building a basic AppleScriptObjC (Cocoa-AppleScript) application with Xcode". peachpit.com. Retrieved May 9, 2017.
- ^ Table adapted from: Naganoya, Takaaki. "ASObjCExtras Scripting Guide" (PDF). piyocast.com. Archived (PDF) from the original on October 9, 2022. Retrieved May 9, 2017.
- ^ "Introduction to Automator AppleScript Actions Tutorial". developer.apple.com. Retrieved May 8, 2017.
- ^ a b "Technical Note TN1164: Scripting Additions for Mac OS X". developer.apple.com. Retrieved May 8, 2017.
- ^ "AppleScript Release Notes: 10.5 Changes". developer.apple.com. Retrieved May 8, 2017.
- ^ "POSIX file — Class Reference — AppleScript Language Guide". developer.apple.com. Retrieved January 8, 2018.
- ^ "AppleScript Language Guide: Handlers in Script Applications". developer.apple.com. Retrieved July 21, 2013.
- ^ "AppleScript Overview: Open Scripting Architecture". developer.apple.com. Retrieved May 8, 2017.
- ^ "Importing Scripts". GitHub. JXA-Cookbook. December 6, 2019. Retrieved December 9, 2019.
Further reading
[edit]- "AppleScript Language Guide". developer.apple.com. 2016. Retrieved May 9, 2017.
- Munro, Mark Conway (2010). AppleScript. Developer Reference. Indianapolis: Wiley. ISBN 978-0-470-56229-1. OCLC 468969567.
- Rosenthal, Hanaan; Sanderson, Hamish (2010). Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X (3rd ed.). Berkeley: Apress. doi:10.1007/978-1-4302-2362-7. ISBN 978-1-4302-2361-0. OCLC 308193726.
- Soghoian, Sal; Cheeseman, Bill (2009). Apple Training Series: AppleScript 1-2-3. Apple Pro training series. Berkeley: Peachpit Press. ISBN 978-0-321-14931-2. OCLC 298560807.
- Cook, William (2007). "AppleScript" (PDF). Proceedings of the third ACM SIGPLAN conference on History of programming languages. ACM. pp. 1–21. CiteSeerX 10.1.1.86.2218. doi:10.1145/1238844.1238845. ISBN 9781595937667. S2CID 220938191.
- Ford Jr., Jerry Lee (2007). AppleScript Programming for the Absolute Beginner. Boston: Thomson Course Technology. ISBN 978-1-59863-384-9. OCLC 76910522.
- Neuburg, Matt (2006). AppleScript: The Definitive Guide (2nd ed.). Beijing; Farnham: O'Reilly Media. ISBN 0-596-10211-9. OCLC 68694976.
- Goldstein, Adam (2005). AppleScript: The Missing Manual. Missing Manual series. Sebastopol, CA; Farnham: O'Reilly Media. ISBN 0-596-00850-3. OCLC 56912218.
- Trinko, Tom (2004). AppleScript for Dummies. For Dummies series (2nd ed.). Hoboken, NJ: Wiley. ISBN 978-0-7645-7494-8. OCLC 56500506.
External links
[edit]- "AppleScript Overview". developer.apple.com. 2007. Retrieved November 7, 2020.
- "AppleScript for Python Programmers (Comparison Chart)". aurelio.net. 2005. Retrieved May 9, 2017.
- "Doug's AppleScripts for iTunes". dougscripts.com. Retrieved May 9, 2017.
- "MacScripter AppleScript community". macscripter.net. Retrieved May 9, 2017.
AppleScript
View on GrokipediaHistory
Origins and Early Development
AppleScript's origins trace back to 1989, when it was conceived within Apple's Advanced Technology Group (ATG) as part of the Family Farm research project led by Larry Tesler, with the goal of improving application integration and user-friendly automation on the Macintosh platform.[2] This initiative drew significant inspiration from HyperTalk, the English-like scripting language introduced with HyperCard in 1987, which demonstrated the potential for non-programmers to automate tasks through natural language metaphors.[2] Development accelerated in mid-1990 with the creation of Apple Events, a foundational protocol for inter-application communication that enabled applications to send structured messages and data to one another.[2] Implemented through the Apple Event Manager, this technology debuted in System 7 during the summer of 1991, providing the infrastructure for scripting by standardizing how applications could respond to commands.[2][8] The AppleScript project itself formally began in 1991, emphasizing an object-oriented approach to scripting that built directly on Apple Events to allow users to manipulate application objects remotely.[2] Key figures in the early development included Kurt Piersol, who directed the Apple Events team after joining Apple in 1989; William Cook, who led the language design starting in April 1991; Warren Harris, the primary implementer and co-architect; and Donn Denman, who designed the script recording features.[2] A beta version of AppleScript was made available in September 1992, followed by the release of the AppleScript 1.0 Developer's Toolkit in April 1993 to assist in application scripting support.[2] The first public version, AppleScript 1.1, launched in October 1993 alongside System 7.1.1 (marketed as System 7 Pro), marking its integration into the core Macintosh operating system.[2] From its inception, AppleScript prioritized inter-application communication via the Apple Event Manager, allowing scripts to orchestrate workflows across multiple programs without requiring low-level programming.[2] Early adoption surged in the publishing and creative industries, where tools like QuarkXPress 3.2—released in late 1993—quickly incorporated AppleScript support to automate complex layout and production tasks, solidifying its role in professional workflows.[9] Sal Soghoian would later become a prominent long-term advocate for the technology after joining Apple in 1997.[10]Evolution and Key Milestones
With the launch of Mac OS X 10.0 Cheetah in March 2001, AppleScript was adapted for Apple's new Unix-based operating system, debuting in version 1.6 with enhanced stability, support for script bundles, and command-line tools like osascript for executing scripts from the terminal.[11] This transition addressed longstanding issues from the classic Mac OS era, such as crashes during long compilations and inconsistent application object handling, while introducing features like POSIX path support and improved error reporting.[11] Building on its initial inspirations from HyperTalk in earlier development phases, AppleScript's port to Mac OS X marked a pivotal shift toward modern integration with developer tools. Subsequent releases refined these foundations, with Mac OS X 10.2 Jaguar in 2002 bringing AppleScript 1.9, which introduced Folder Actions for automating responses to file system events and added the Script Menu for quick access to saved scripts.[11] Version 1.9.2 arrived with Mac OS X 10.3 Panther in 2003, enabling robust handling of international text and resolving issues with non-ASCII characters in scripts.[11] A major milestone came in 2001 alongside the developer tools for Mac OS X 10.1 Puma, when AppleScript Studio was introduced as part of Xcode, allowing scripters to build standalone GUI applications by combining AppleScript code with Interface Builder's Cocoa interface elements without deep Objective-C knowledge.[1] This tool fostered creative automation projects, such as custom dialogs and menu-driven apps, and remained a cornerstone for developers until its deprecation in later Xcode versions. AppleScript 2.0, released with Mac OS X 10.5 Leopard in October 2007, represented a comprehensive overhaul, incorporating full Unicode compliance, native 64-bit processing, and improved performance.[11] In Mac OS X 10.6 Snow Leopard (2009), version 2.1 extended Folder Actions to incorporate Automator workflows, allowing hybrid scripting that combined AppleScript's expressiveness with Automator's visual action chaining for easier task automation, and introduced the AppleScriptObjC framework for seamless bridging to Cocoa APIs.[11] Further enhancements in OS X 10.8 Mountain Lion (2012) with version 2.3 improved the AppleScript Editor with features like Auto Save and version history, while OS X 10.10 Yosemite (2014) and 10.11 El Capitan (2015) delivered version 2.8, the final major update, which optimized support for 64-bit applications, enhanced sandboxing compatibility, and fixed threading issues in script execution.[11] By 2016, signs of waning investment emerged when Sal Soghoian, Apple's long-time product manager for automation technologies including AppleScript, departed the company after his role was eliminated, raising concerns among developers about future prioritization of scripting features amid shifting focus to other platforms.[12] Since then, AppleScript has seen no major updates, remaining at version 2.8 and fully supported in macOS up to version 15 Sequoia (2024) as of November 2025.[13] Despite this, these milestones solidified AppleScript's role in macOS automation, with ongoing ties to tools like Automator ensuring its utility for system-level tasks through the mid-2010s.[12]Design Philosophy
Core Intent
AppleScript was designed primarily as an automation tool to enable users to control and orchestrate interactions between scriptable applications on Apple systems, leveraging the Apple Events interapplication messaging protocol to facilitate task automation without delving into low-level programming details.[1] This approach allows scripts to send high-level commands that trigger actions across multiple applications, such as processing data from one program and exporting it to another, thereby streamlining repetitive workflows like document assembly or data manipulation.[2] By abstracting the underlying Apple Events mechanism—which handles remote procedure calls with structured data payloads—AppleScript reduces the complexity of interapplication communication, making it feasible for users to build system-wide automations that integrate disparate software components seamlessly.[14] A key emphasis in AppleScript's design is its accessibility to non-programmers, empowering end-users to script controls for core macOS applications such as Finder for file management and Mail for email handling, without requiring in-depth knowledge of application programming interfaces (APIs).[1] This user-centric focus originated from the need to extend the graphical user interface (GUI) of Macintosh applications with customizable behaviors, allowing individuals to automate UI-driven tasks like batch renaming files or sorting messages, all through simple instructional scripts.[2] Developers and casual users alike benefit from this, as it supports embedding scripts directly into applications to modify their functionality on the fly, fostering efficient, personalized workflows across the operating system.[14] Historically, AppleScript's intent was to democratize scripting on Apple platforms, providing an approachable alternative to command-line tools like shell scripting by prioritizing ease of use for non-technical users over the intricacies of text-based pipelines.[2] Conceived in 1989 as part of efforts to unify scripting capabilities inspired by HyperCard, it aimed to make automation inclusive, contrasting the Unix shell's focus on stream processing with AppleScript's handling of complex, structured objects like documents and images.[14] Released in September 1993 with System 7 Pro, this philosophy positioned AppleScript as a bridge between end-user productivity and advanced customization, enabling broad adoption for tasks that would otherwise demand specialized programming skills.[1]Natural Language Metaphor
AppleScript's syntax is intentionally designed to emulate the structure of natural English sentences, employing verb-noun phrases that mirror spoken commands to enhance intuitiveness. For instance, a script might instruct "tell application 'Finder' to open file 'document.pdf'", where "tell" acts as a directive, "application 'Finder'" specifies the target, and "open file" forms the core action in a familiar imperative form.[15] This approach draws from the Open Scripting Architecture, which integrates English-like terminology with application-specific objects to facilitate readable automation scripts.[16] The metaphor is deeply rooted in English grammar, treating commands as verbs, properties as adjectives or descriptors, and objects as nouns, which promotes quick comprehension among users unfamiliar with traditional programming paradigms. By approximating natural language structures—such as prepositional phrases for relationships (e.g., "the first word of the second paragraph")—AppleScript allows scripts to read almost like prose, supporting dialects in multiple languages including Japanese and French for broader accessibility.[2] This design philosophy contrasts sharply with the procedural syntax of languages like C, which rely on explicit declarations, loops, and punctuation-heavy code, making AppleScript more approachable for non-programmers seeking to automate tasks without deep technical expertise.[15] One key benefit of this metaphor is the reduced learning curve for end-users, particularly in creative and productivity workflows, where individuals can author or modify scripts by recording actions and editing them in plain-English form, thereby enabling efficient customization of applications like document editors or image processors.[16] However, the metaphor has limitations, as AppleScript is not a true natural language processor; it demands precise adherence to the Apple event vocabulary defined in each application's scripting dictionary, where deviations can lead to errors due to syntactic ambiguities or incomplete terminologies.[2] This requirement for exact phrasing underscores that while readable, writing effective scripts still necessitates familiarity with structured rules rather than free-form expression.[15]Language Fundamentals
Syntax Overview
AppleScript employs a statement-based syntax, where individual statements form the fundamental units of executable code, processed sequentially from top to bottom.[15] Each statement typically expresses a command or assignment, drawing inspiration from natural language to enhance readability, though it adheres to strict grammatical rules for parsing.[1] A core structural element is thetell block, which directs commands to a specific application or object, enclosed by tell and end tell delimiters. For instance, to interact with the Finder application, a script might begin with tell application "Finder" and conclude the block with end tell, ensuring commands target the intended recipient.[15] This scoping mechanism prevents ambiguity in multi-application scripts and is essential for Apple event communication.[6]
Commands follow a verb-direct object pattern, resembling English sentence structure, such as set x to 5 to assign a value or get name of first window to retrieve a property.[15] Parameters can be specified using to, of, or with, and nested references allow chaining, like name of first item of desktop.[6] AppleScript supports automatic coercion for type conversion when operands are compatible, converting a string to a number implicitly during arithmetic; explicit coercion uses the as operator, as in set myText to 2 as text.[15]
Lists are delimited by curly braces with commas as separators, for example {1, "two", date "today"}, enabling ordered collections without requiring explicit type declarations.[17] Strings, conversely, are enclosed in double quotes, such as "Hello, world", with escapes for internal quotes using "".[1]
The overall script format begins with optional property declarations, followed by the main body of statements, and may include handlers for modularity.[15] Execution typically starts at the implicit or explicit run handler, defined as on run ... end run, serving as the entry point when the script is invoked.[18] Scripts are saved as plain text files, often with a .scpt extension, and compiled for efficiency.[1]
Data Types and Objects
AppleScript supports a set of primitive data types that form the foundation for scripting values and expressions. These include integer, representing whole numbers within the range of approximately -536,870,912 to 536,870,911; real, for decimal numbers up to about ±1.797693e+308; string (also referred to as text), which holds ordered sequences of Unicode characters; boolean, limited to the valuestrue or false; list, an ordered collection of zero or more values of any class; and record, an unordered collection of labeled properties functioning as key-value pairs.[17] For instance, a list might be defined as {1, "hello", true}, while a record could be {name: "Finder", version: 10.15}.[17]
Beyond primitives, AppleScript employs an object-oriented model where objects are instances of classes, enabling interaction with scriptable applications through references rather than direct manipulation. Application objects represent running applications, such as the Finder or TextEdit, and include properties like name, version, and frontmost.[17] Elements are contained objects within a parent, such as windows within an application or items within a list, allowing hierarchical access like window 1 of application "Finder".[15] Properties are named attributes of objects, which can be read-only (e.g., weekday of a date) or read-write (e.g., month of a date).[17]
The class hierarchy in AppleScript is facilitated by Apple event objects, which use object specifiers to describe and locate elements dynamically within an application's model. This model is inherently dynamic, as scriptable applications define their own classes, elements, and properties via scripting dictionaries, enabling runtime resolution without fixed type declarations.[15] For example, the Finder application exposes classes like folder and file, each with specific properties and elements that scripts can reference, such as every file of folder "Documents" of home.[15]
AppleScript features automatic type coercion to convert between compatible classes when mismatches occur, promoting flexibility in a weakly typed environment without enforced strong typing. Coercion can be explicit using the as operator, such as converting a string to a date with "January 1, 2020" as date, or implicit during operations like assigning a number to a text variable.[15] Supported coercions include integer to real, string to integer (if numeric), list to text, and many others, though not all conversions are bidirectional or always possible.[17] This mechanism allows scripts to handle diverse data seamlessly, such as treating a numeric string as an integer for arithmetic.[15]
Variables and Scoping
In AppleScript, variables are implicitly declared upon their first assignment using theset command, which creates the variable and binds it to a value without requiring a prior declaration statement.[19] For example, the statement set myVariable to "Hello, World!" introduces myVariable as a new variable containing the text object "Hello, World!". This approach contrasts with languages that mandate explicit variable declarations, allowing scripts to define variables dynamically as needed during execution.[19]
AppleScript employs dynamic typing for variables, meaning no explicit type specification is required at declaration or assignment; the class of the variable is determined by the value assigned to it, with automatic coercion occurring when necessary to match contextual expectations.[15] For instance, assigning a numeric value like set counter to 42 results in an integer class, while set message to counter may coerce the integer to text if the context demands it, such as in string concatenation. This coercion is handled implicitly by AppleScript's runtime, supporting conversions between compatible classes like integer to real or text to date, though not all class pairs are coercible.[15] Variables can hold any supported data type, including references to objects from the prior section on data types.[19]
Scoping in AppleScript distinguishes between local and global variables to control accessibility and lifetime. By default, variables set in the top-level run handler (the main script body) are local to that handler and cease to exist after its execution, promoting encapsulation and preventing unintended side effects.[19] To extend a variable's visibility across the entire script or multiple handlers, it must be explicitly declared as global using global variableName before any assignment; such globals persist for the script's duration and are accessible from any handler or the top level.[19] Conversely, local variableName explicitly confines the variable to the current handler or top level, overriding the default behavior if needed and ensuring it does not leak into outer scopes. For example:
global sharedCounter
local tempValue
set sharedCounter to 0
set tempValue to "temporary"
-- In a handler:
on incrementCounter()
set sharedCounter to sharedCounter + 1
-- tempValue is not accessible here
end incrementCounter
global sharedCounter
local tempValue
set sharedCounter to 0
set tempValue to "temporary"
-- In a handler:
on incrementCounter()
set sharedCounter to sharedCounter + 1
-- tempValue is not accessible here
end incrementCounter
if, set, global, or local—to prevent syntax errors or unexpected behavior, as redefining these can lead to compilation issues.[20] For instance, naming a variable setValue is permissible, but set alone would conflict with the built-in command. To minimize scope and reduce bugs from variable shadowing or pollution, developers should declare variables as local whenever possible, only using globals for data truly shared across the script, and prefer the copy command over set for list or record assignments to avoid unintended reference sharing.[19] These conventions enhance script maintainability, especially in larger workflows where scope isolation prevents cascading errors.[19]
Control Structures
Loops and Conditionals
AppleScript provides several control structures for implementing repetition and decision-making in scripts, enabling developers to handle iterative tasks and conditional logic efficiently. These constructs are integral to the language's design, allowing scripts to process data dynamically based on runtime conditions or predefined iterations. The primary loop types include fixed repetition, iterator-based loops, and condition-based loops, while conditionals support branching with optional case-insensitive comparisons.[21] The basicrepeat loop executes a block of statements indefinitely until interrupted by an exit repeat statement, a return command, or a try block that handles errors to terminate the loop. For fixed iterations, the repeat with integer times syntax repeats the block a specified number of times, such as repeat 5 times followed by statements and end repeat; if the integer is less than 1, the loop body is skipped entirely. This form is useful for straightforward, predetermined repetitions without needing counters.[21]
Iterator-based loops in AppleScript use the repeat with loopVariable from startValue to stopValue syntax to increment a variable across a numeric range, optionally stepping by a specified increment (defaulting to 1), as in repeat with i from 1 to 10 to process integers sequentially. The loop variable retains its final value after execution. Similarly, repeat with loopVariable in list iterates over the items of a list, assigning each to the loop variable in turn, for example, repeat with item in myList to handle collections like arrays or records; using contents of can access deeper values if needed. These iterator forms promote clean handling of ranges or sequences without manual indexing.[21]
Condition-based loops include repeat while boolean, which executes the block as long as the condition evaluates to true (checked before each iteration), and repeat until boolean, which continues until the condition becomes true. For instance, repeat while userNotDone might loop until a flag is set, providing flexibility for tasks like data entry until completion. Exiting these loops early relies on exit repeat inside conditional checks, or equivalents like throwing errors in a try block to simulate breaks; there is no native continue statement, so skipping iterations often involves boolean flags or nested structures to bypass code conditionally.[21]
For decision-making, the if statement evaluates a boolean expression and executes statements accordingly, with simple forms like if condition then statement or compound versions supporting else if chains and a final else clause, all enclosed in end if. An example is if temperature < 60 then set message to "Chilly" else set message to "Warm" end if, where only the first true branch runs. AppleScript's considering and ignoring clauses modify conditional comparisons, particularly for text, by specifying attributes like case, diacriticals, or punctuation; for case insensitivity, ignoring case ensures "Apple" = "apple" returns true, overriding the default behavior that ignores case and numeric strings but considers others. These clauses nest and take precedence in inner scopes, enhancing precise string-based decisions.[21]
Handlers and Functions
In AppleScript, handlers are reusable blocks of code that promote modularity and maintainability by encapsulating specific tasks, allowing them to be invoked by name from elsewhere in a script.[18] They function similarly to subroutines in other languages, enabling code reuse without duplication. Handlers are defined using a specific syntax and can accept parameters, return values, and even support recursion, though with practical limitations.[22] The basic syntax for defining a named handler begins withon followed by the handler name and optional parameters, containing statements, and ending with end followed by the handler name. For example, a simple handler without parameters might be written as on helloWorld() display dialog "Hello, World!" end helloWorld, which can then be called simply as helloWorld().[18] With parameters, the syntax extends to on handlerName(paramName) ... end handlerName, where parameters are passed by value for immutable types like numbers and strings, ensuring the original values remain unchanged.[18] AppleScript distinguishes between positional parameters, which must be supplied in a fixed order (e.g., on minimumValue(x, y) return x if x < y end minimumValue), and labeled parameters, which offer greater flexibility by allowing named arguments in any order (e.g., on findNumbers of numberList above minLimit given rounding:roundBoolean ... end findNumbers).[22] Labeled parameters are particularly useful for handlers with multiple optional inputs, as callers can specify only the relevant ones, such as findNumbers of myList above 10 given rounding:true.[18]
AppleScript supports several types of handlers tailored to different script behaviors. Named handlers, as described, are user-defined for custom routines and can be called explicitly. The run handler serves as the script's entry point, executing top-level statements implicitly or explicitly within on run ... end run when the script is launched.[18] For stay-open applications, which remain active after launch, the idle handler runs periodically—defaulting to every 30 seconds—and can return an integer to adjust the interval (e.g., return 5 for 5-second cycles), facilitating ongoing tasks like monitoring.[23] Similarly, the launch handler executes once upon application startup, often used to initialize resources in stay-open scripts.[18]
To exit a handler and optionally return a value, the return statement is used, such as return theResult, which immediately terminates execution and passes the value back to the caller; omitting an expression returns nothing.[22] AppleScript permits recursion, where a handler calls itself to solve problems iteratively, like computing a factorial: on factorial(n) if n ≤ 1 then return 1 else return n * factorial(n - 1) end if end factorial. However, recursion is cautioned against for deep levels due to memory constraints, which can lead to stack overflow errors if the call depth exceeds system limits.[18] For tail-recursive scenarios, the continue statement can transfer control back to the invoking handler, optimizing stack usage.[22]
Scripts and Blocks
AppleScript scripts are organized as a sequence of statements that are executed in order, forming the basic unit of code for automation tasks. The top-level structure of a script consists of a script object that can include properties, handlers, and nested elements, with statements outside any explicit handler implicitly belonging to the mainrun handler, which serves as the default entry point for execution when the script is run. This main run handler can be explicitly defined using on run ... end run to encapsulate initialization or primary logic, ensuring that the script begins execution from this point unless otherwise directed.[15]
A key feature of AppleScript's architecture is the use of tell blocks to direct commands to specific applications or objects, enabling targeted interactions within scriptable environments. A tell block begins with tell followed by a reference, such as tell application "Safari", and encloses the relevant statements, concluding with end tell; this scopes the commands to the specified target, preventing unintended effects on other parts of the system. For instance, within such a block, one might reference elements like the URL of the current tab to manipulate browser behavior precisely.[15]
Block delimiters in AppleScript provide structure for control flow and scoping, with common forms including tell ... end tell for application targeting and if ... end if for conditional logic, allowing statements to be nested hierarchically for complex operations. Nesting occurs when one block is placed inside another, such as an if statement within a tell block, which maintains the inner block's scope while inheriting the outer context, facilitating modular and readable code organization. These delimiters ensure that code blocks are self-contained, promoting clarity in script design.[15]
Script objects extend this structure by allowing the definition of reusable components that bundle properties and handlers, often serving as libraries for shared functionality across multiple scripts. Defined with script ... end script, these objects can be nested within other scripts or placed in designated library folders like ~/Library/Script Libraries/, where they are loaded by name for inheritance and modularity; for example, a library script might define common utilities that are instantiated during script compilation. Handlers defined within script objects can be included in the overall script architecture to support subroutine reuse.[24]
Scripts are typically compiled into binary .scpt files using tools like Script Editor, which optimizes the code for execution and stores it in a compact format suitable for distribution or repeated use, with properties reinitialized each time the script runs. This compilation process, part of AppleScript's runtime environment, prepares the script object for efficient interapplication communication via Apple events.[15]
User Interaction and Documentation
Dialogs and Alerts
AppleScript provides built-in commands from the Standard Additions scripting addition for displaying dialogs and alerts to interact with users, allowing scripts to solicit input or provide feedback during execution.[25] Thedisplay dialog command creates customizable dialogs with user-defined messages, optional buttons, default text answers, and icons, enabling scripts to pause for user decisions or confirmations.[25] For instance, the syntax is display dialog messageText [buttons {list}] [default button numberOrName] [cancel button numberOrName] [default answer string] [with icon iconSpecifier] [giving up after seconds], where the message is the primary text, buttons can be up to three custom labels (defaulting to OK and Cancel), and icons can be predefined as note, caution, or stop, or specified by file path or resource ID.[25] Results are returned as a record containing button returned (the text of the clicked button) and optionally text returned (user-entered text) or gave up (true if timed out).[25]
display dialog "Do you want to continue?" buttons {"No", "Yes"} default button "Yes" with icon caution
display dialog "Do you want to continue?" buttons {"No", "Yes"} default button "Yes" with icon caution
button returned property.[25] The display alert command, a variant for more urgent notifications, supports alert types such as informational (note icon), warning (caution icon), or critical (stop icon), with an optional secondary message parameter for additional details.[25] Its syntax is display alert messageText [message secondaryText] [as alertType] [buttons {list}] [default button numberOrName] [cancel button numberOrName], returning a record with the button returned value but without text input or timeout options.[25]
display alert "Low disk space" message "Free up at least 1 GB." as critical buttons {"Ignore", "Quit App"} default button "Quit App"
display alert "Low disk space" message "Free up at least 1 GB." as critical buttons {"Ignore", "Quit App"} default button "Quit App"
choose from list command presents a scrollable list of strings, allowing single or multiple choices with optional prompts and defaults.[26] The syntax is choose from list itemList [with prompt promptText] [default items {list}] [multiple selections allowed boolean] [empty selection allowed boolean], where itemList is the options (e.g., {"Option 1", "Option 2"}), and it returns a list of selected items or false if canceled (unless empty selection allowed is true, yielding an empty list).[26] Button indexes are not directly returned; instead, results are the selected strings themselves, which can be matched to original list positions if needed.
set choices to {"Red", "Green", "Blue"}
set selected to choose from list choices with prompt "Pick a color:" multiple selections allowed true default items {"Green"}
set choices to {"Red", "Green", "Blue"}
set selected to choose from list choices with prompt "Pick a color:" multiple selections allowed true default items {"Green"}
selected might be {"Red", "Green"}, enabling the script to process multiple user picks.[26]
The choose file and choose folder commands invoke standard file browser dialogs for selecting files or folders, supporting file type filtering and multiple selections.[27] For choose file, the syntax is choose file [with prompt promptText] [of type {typeList}] [without invisible] [multiple selections allowed boolean] [inheriting allow multiple selection], returning an alias or list of aliases to selected files; of type uses Uniform Type Identifiers (e.g., {"public.image"}) to limit options.[27] Similarly, choose folder uses choose folder [with prompt promptText] [without invisibles] [multiple selections allowed boolean], returning folder aliases without type restrictions.[27] Results are direct references usable in file operations, with cancellation throwing an error (-128) unless handled.
set theFile to choose file of type {"public.plain-text"} with prompt "Select a text file:"
set theFile to choose file of type {"public.plain-text"} with prompt "Select a text file:"
choose color command opens a color picker dialog, returning an RGB color triplet for applications like setting UI elements.[28] Its syntax is choose color [default color {red, green, blue}], where values range from 0 to 65535, and the result is a list like {redValue, greenValue, blueValue} based on user selection.[28] Cancellation throws an error (-128).
set userColor to choose color default color {0, 65535, 0}
set userColor to choose color default color {0, 65535, 0}
userColor can then be applied, for example, to text in an application.[28] These commands collectively facilitate interactive scripting while integrating seamlessly with error handling for robust user experiences.[28]
Comments and Error Handling
AppleScript supports comments to document code and facilitate maintenance, ignoring them during compilation and execution. Single-line comments begin with two hyphens (--) and extend to the end of the line, allowing inline notes after statements.[29] Since AppleScript 2.0, a hash symbol (#) can also initiate single-line comments, enabling shebang lines for executable scripts.[29] Multi-line or block comments are delimited by (* at the start and *) at the end, supporting nesting for complex annotations.[29] For example:
set x to 5 -- This sets the value of x
(* This is a multi-line
comment spanning
multiple lines. *)
set x to 5 -- This sets the value of x
(* This is a multi-line
comment spanning
multiple lines. *)
try statement to enclose potentially failing code, followed by an optional on error clause to intercept exceptions and prevent script termination.[21] The on error clause can capture parameters such as the error message (as text), error number (as integer), offending object, and partial result from the failed statement.[21] Scripts can also raise custom errors using the error command, specifying a message and optional number (defaulting to -2700 for unknown errors).[6] An example of trapping an error:
try
set fileRef to open for access file "nonexistent.txt"
on error errMsg number errNum
log "Error " & errNum & ": " & errMsg
-- Handle the error, e.g., create the file
end try
try
set fileRef to open for access file "nonexistent.txt"
on error errMsg number errNum
log "Error " & errNum & ": " & errMsg
-- Handle the error, e.g., create the file
end try
log command, which outputs values to the Script Editor's Event Log pane or history window, displaying them within block comment delimiters for easy identification.[6] This is particularly useful within try blocks to trace execution or inspect variables without interrupting the script. Alternatively, the display dialog command can show messages during runtime, though it requires user interaction. Common errors include type mismatches (-10001), where operations involve incompatible data types, and unhandled events (-1708), occurring when an application receives an unrecognized Apple event.[30] User cancellations from interactive commands yield error -128, which propagates silently if not caught.[30] Errors can be communicated to users via alerts, as covered in the section on dialogs and alerts.
Practical Examples
Basic Scripts
AppleScript's basic scripts introduce fundamental syntax and commands, allowing users to perform simple tasks such as displaying messages, manipulating data, and interacting with applications. These scripts are self-contained and serve as building blocks for more advanced automation, demonstrating core elements like commands, variables, and control flow without relying on complex structures.[15] A quintessential introductory script is the "Hello World" example, which uses thedisplay dialog command from the Standard Additions scripting addition to show a simple message to the user. The following script creates a dialog box with the text "Hello, world!":
display dialog "Hello, world!"
display dialog "Hello, world!"
set command to store data in named variables, which can hold various data types including lists. For instance, to set a list variable and output its contents, consider this script that assigns a list of strings and displays it as a single text string:
set fruitList to {"apple", "banana", "cherry"}
display dialog fruitList as string
set fruitList to {"apple", "banana", "cherry"}
display dialog fruitList as string
fruitList is assigned a list containing three string elements, and the as string coercion converts the list to readable text ("apple, banana, cherry") for display in a dialog. This demonstrates how lists serve as ordered collections in AppleScript, enabling basic data storage and presentation.[31]
Tell blocks direct commands to specific applications, encapsulating interactions within tell and end tell statements. A basic example opens a file in TextEdit, assuming the file exists on the desktop:
tell application "TextEdit"
open file ((path to desktop as string) & "example.txt")
end tell
tell application "TextEdit"
open file ((path to desktop as string) & "example.txt")
end tell
open command to load the specified file path, which activates the application if necessary and displays the document. The path to desktop expression resolves to the user's Desktop folder dynamically. Such blocks are essential for application-specific scripting.[6]
Simple conditionals employ the if statement to execute code based on boolean evaluations, often used to check conditions like file existence via the Finder application. The following script verifies if a file exists on the desktop before displaying a confirmation:
tell application "Finder"
if exists file ((path to desktop as string) & "example.txt") then
display dialog "The file exists."
else
display dialog "The file does not exist."
end if
end tell
tell application "Finder"
if exists file ((path to desktop as string) & "example.txt") then
display dialog "The file exists."
else
display dialog "The file does not exist."
end if
end tell
exists command within the Finder tell block returns a boolean, triggering the appropriate dialog. This pattern prevents errors in scripts that depend on external resources, showcasing conditional logic's role in robust basic automation.[21]
Complex Workflows
Complex workflows in AppleScript often involve integrating multiple applications, handling user input securely, and incorporating error resilience to automate multi-step tasks reliably. These advanced scripts leverage handlers for modular code, control structures for iteration, and built-in commands for inter-application communication, enabling automation of routine processes like calculations, network access, communications, and file management.[22][32] A failsafe calculator demonstrates handler-based mathematics with robust error checking, ensuring operations only proceed on valid numeric inputs. The following script defines a handler to add two numbers, using atry block to validate types and catch coercion errors; if inputs are non-numeric, it displays an alert and returns zero, preventing runtime failures. This approach draws from AppleScript's error handling mechanisms, where the on error clause captures error numbers like -1700 for type mismatches.[33]
on safeAdd(num1, num2)
try
set result to num1 + num2
return result
on error errNum
if errNum is -1700 then
display alert "Error: Inputs must be numbers."
return 0
end if
error errNum -- Re-raise other errors
end try
end safeAdd
-- Usage example
set sum to safeAdd(5, 3) -- Returns 8
display dialog "Sum: " & sum
on safeAdd(num1, num2)
try
set result to num1 + num2
return result
on error errNum
if errNum is -1700 then
display alert "Error: Inputs must be numbers."
return 0
end if
error errNum -- Re-raise other errors
end try
end safeAdd
-- Usage example
set sum to safeAdd(5, 3) -- Returns 8
display dialog "Sum: " & sum
try ensuring stability during computations.[22]
For drive login, AppleScript automates mounting network volumes with authentication, prompting users for credentials to securely connect to remote servers without manual intervention. The mount volume command supports direct specification of usernames and passwords in the URL or as parameters, but for enhanced security, scripts can use display dialog to collect credentials dynamically before mounting. While choose file can select local configuration files containing server details, the core authentication occurs via the mount command itself; this workflow integrates error handling to retry or alert on connection failures.[6]
set serverURL to "smb://example.server.com/share"
set userName to text returned of (display dialog "Enter username:" default answer "")
set password to text returned of (display dialog "Enter password:" default answer "" with hidden answer)
try
mount volume serverURL as user name userName with password password
display dialog "Volume mounted successfully."
on error errMsg
display alert "Mount failed: " & errMsg
end try
set serverURL to "smb://example.server.com/share"
set userName to text returned of (display dialog "Enter username:" default answer "")
set password to text returned of (display dialog "Enter password:" default answer "" with hidden answer)
try
mount volume serverURL as user name userName with password password
display dialog "Volume mounted successfully."
on error errMsg
display alert "Mount failed: " & errMsg
end try
choose file, sets content, and sends it; the make new attachment command embeds the file directly into the message body. Attachments are specified as aliases to avoid path issues, and a brief delay ensures Mail processes the elements correctly.[34]
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:"Automated Report", content:"Please review the attached file."}
tell newMessage
make new to recipient at end of to recipients with properties {address:"[email protected]"}
set theAttachment to choose file with prompt "Select file to attach:"
make new attachment with properties {file name:theAttachment} at after the last paragraph of content
end tell
send newMessage
end tell
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:"Automated Report", content:"Please review the attached file."}
tell newMessage
make new to recipient at end of to recipients with properties {address:"[email protected]"}
set theAttachment to choose file with prompt "Select file to attach:"
make new attachment with properties {file name:theAttachment} at after the last paragraph of content
end tell
send newMessage
end tell
repeat with loop over a list of files obtained via choose file with multiple selections, the script appends a prefix and sequential numbers, updating each item's name property directly. This avoids manual editing for large sets, with optional error checks for naming conflicts.[6]
tell application "Finder"
set selectedFiles to choose file with prompt "Select files to rename:" multiple selections allowed true
set counter to 1
repeat with eachFile in selectedFiles
set oldName to name of eachFile
set newName to "Processed_" & counter & "_" & oldName
set name of eachFile to newName
set counter to counter + 1
end repeat
display dialog "Renaming complete for " & (count of selectedFiles) & " files."
end tell
tell application "Finder"
set selectedFiles to choose file with prompt "Select files to rename:" multiple selections allowed true
set counter to 1
repeat with eachFile in selectedFiles
set oldName to name of eachFile
set newName to "Processed_" & counter & "_" & oldName
set name of eachFile to newName
set counter to counter + 1
end repeat
display dialog "Renaming complete for " & (count of selectedFiles) & " files."
end tell
Development Tools
Script Editors
Script Editor is the built-in application for macOS that serves as the primary integrated development environment (IDE) for authoring, editing, and testing AppleScript code. Included with every macOS installation since its introduction with AppleScript in System 7 in 1993, it provides essential tools for script development without requiring additional software.[35] Key features of Script Editor include syntax highlighting, which colors keywords, commands, and strings to improve readability and help identify structural issues in the code. Auto-completion assists by suggesting completions for commands, properties, and application-specific terms as the user types, accelerating the writing process. Error highlighting flags syntax errors, undefined variables, and compilation issues with visual indicators, allowing developers to resolve problems before execution.[36] The dictionary viewer is a standout capability, enabling users to explore the scripting terminology of scriptable applications. Accessed via the File > Open Dictionary menu, it displays a hierarchical view of an application's classes, objects, properties, elements, and commands, complete with descriptions and syntax examples. This tool is invaluable for discovering how to interact with apps like Finder or Safari through AppleScript. Script Editor supports compilation of scripts into standalone applets, which are executable .app bundles that run independently without the editor. Users can compile a script and save it directly as an applet, with options to include resources or set startup behaviors. Export features allow saving compiled applets in formats suitable for distribution, such as run-only versions that protect source code. For more advanced development, third-party tools like Script Debugger, developed by Late Night Software, extend AppleScript authoring capabilities. Released in 1995 and maintained until its retirement in January 2025, Script Debugger offered enhanced debugging features, including line-by-line stepping through code execution and setting breakpoints to pause at specific points. Following its retirement, past versions have been made freely available, and a Lite version continues to be offered for basic enhanced features. It provided detailed variables inspection, displaying real-time values, types, and contents (such as file previews via QuickLook integration), which was particularly useful for troubleshooting complex scripts. Additional aids included a dictionary explorer for drag-and-drop code generation, code folding for managing large scripts, and built-in error detection for issues like memory leaks.[37][38][39] AppleScript can also be integrated into larger applications using Xcode, Apple's professional IDE for macOS development. Through the AppleScriptObjC framework, developers embed AppleScript code within Cocoa applications, leveraging Objective-C runtime features while writing in AppleScript syntax for user interfaces and logic. This approach is ideal for creating apps with advanced GUIs, where scripts handle automation tasks alongside native controls; projects are set up using Xcode templates for AppleScript apps, allowing compilation into full bundles.[40][41]Launchers and Debuggers
AppleScript provides several built-in and third-party tools for launching scripts outside of development environments, enabling quick execution, scheduling, and event-based triggering. The Script Menu, accessible via the menu bar when enabled in Script Editor preferences, allows users to organize and run saved AppleScript files instantly from a dropdown menu, supporting subfolders for categorization and facilitating rapid access to frequently used scripts.[42] Automator complements this by incorporating Run AppleScript actions into workflows, such as Quick Actions, which can be invoked from context menus in Finder or other apps for on-demand script execution without opening a full editor.[43] For command-line launching, the osascript utility enables execution of AppleScript code or files directly from Terminal, supporting options like passing arguments or running inline scripts, which is useful for automation in shell environments or integration with other command-line tools.[44] Event-driven launching is handled through Folder Actions, a macOS feature that attaches AppleScript handlers to folders; these scripts trigger automatically on events like adding, removing, or opening items, with setup managed via the Folder Actions Setup application in /Applications/AppleScript.[45][46] Stay-open applets extend this by configuring scripts as persistent applications that remain running after execution, responding to idle or quit events until manually closed, ideal for ongoing monitoring or periodic tasks.[47] Debugging capabilities focus on runtime inspection and error tracing. The built-in log command outputs messages to the Event Log pane in Script Editor or the system Console app, aiding in tracking variable states and execution flow during testing.[48] Third-party tools like Script Debugger enhance this with advanced features, including breakpoints to pause execution at specific lines, step-through debugging to advance one statement at a time, and real-time variable inspection to monitor values and scope without halting the script entirely.[37][49] For interactive testing, Smile offers an integrated environment where AppleScript can be edited, debugged line-by-line, and executed interactively, supporting custom user interfaces and data processing to simulate real-world conditions.[50] Wrapping scripts for standalone deployment is facilitated by Platypus, which converts AppleScript files into native macOS applications, bundling them with customizable interfaces and resources for distribution without requiring the original editor.[51] These tools collectively support efficient deployment and troubleshooting, emphasizing runtime behavior over initial composition.Resources and Extensions
Libraries and Frameworks
AppleScript's core capabilities are extended through a collection of standard libraries and frameworks that provide reusable code for advanced functionality, such as networking, GUI development, and integration with system APIs. These tools allow scripters to build more robust applications without relying solely on basic language constructs. Apple's official scripting additions form the foundation, offering pre-built commands that can be invoked directly in scripts. The Standard Additions library, bundled with macOS as part of the AppleScript installation, serves as the primary standard library and includes commands for file system operations, dialog displays, and networking tasks. For instance, the "do shell script" command enables AppleScript to execute shell commands, including network requests via tools like curl, effectively handling HTTP interactions and file downloads.[6] This replaced older additions like URL Access Scripting, which provided direct networking functions but was deprecated in later macOS versions in favor of more flexible shell-based approaches.[52] AppleScriptObjC, introduced in Mac OS X 10.6 Snow Leopard, is a key framework that bridges AppleScript with Objective-C, enabling the creation of full Cocoa applications using AppleScript as the primary language. It allows scripters to instantiate Objective-C classes, call methods, and handle events directly, facilitating access to macOS APIs for tasks like custom interface elements and data processing.[53] This framework marked a significant evolution, providing a more seamless integration with Cocoa compared to prior tools. AppleScript Studio, an earlier framework for building graphical user interfaces, was available from Mac OS X 10.4 Tiger through 10.5 Leopard as part of Xcode, allowing developers to design UIs in Interface Builder and attach AppleScript handlers. It supported drag-and-drop integration for creating standalone applications but was deprecated starting in Mac OS X 10.6 Snow Leopard, with Apple recommending migration to AppleScriptObjC for modern GUI development.[54] In addition to Apple's offerings, community-developed libraries enhance AppleScript for specialized needs like data serialization and storage. JSON Helper, a popular scripting addition, simplifies JSON parsing and generation by converting between JSON strings and native AppleScript records or lists, making it easier to handle web APIs and configuration files.[55] For database access, Shane Stanley's SQLite libraries provide AppleScript interfaces to SQLite databases, supporting queries, insertions, and transactions for lightweight data management without external dependencies. These community resources, often distributed as script libraries or OSAX extensions, extend AppleScript's utility in automation workflows.Scripting Additions and Integrations
Scripting additions, commonly known as OSAX (Open Scripting Architecture eXtensions), are modular extensions that enhance AppleScript's core vocabulary by providing additional commands and functionality through the Open Scripting Architecture (OSA).[56] These extensions operate as background components, typically implemented as bundles that respond to Apple events, allowing scripters to access system-level operations not inherent to the language itself.[6] The most prominent bundled scripting addition is Standard Additions, which includes a suite of commands for common tasks such as file and folder manipulation (e.g.,choose file, path to), user interface interactions (e.g., display dialog, display alert), and text processing (e.g., offset of, text item delimiters).[57] To use these commands, scripts must include the directive use scripting additions at the top, which loads the extension and makes its terminology available.[58] Standard Additions is pre-installed in macOS and forms the foundation for many everyday automation scripts.
Scripting additions are installed by placing the .osax bundle files in designated directories: /Library/Scripting Additions/ for system-wide availability or ~/Library/Scripting Additions/ for user-specific installations.[59] Custom OSAX can be developed using Xcode, where developers create Mach-O bundles in C, Objective-C, or Swift that implement Apple event handlers to define new commands and coerce data types.[15] These custom extensions must adhere to OSA standards, exporting a scripting component that integrates seamlessly with AppleScript's runtime.
Notable examples include Database Events, a faceless application that provides AppleScript commands for interacting with SQLite databases, such as creating records (make new record), querying data (every record of database 1 whose name = "example"), and executing SQL statements.[60] Similarly, Image Events offers commands for basic image processing, including opening files (open file1), scaling (scale to size 800), cropping (crop to bounds {x, y, width, height}), and saving in formats like JPEG or PNG, without requiring external editing software.[61]
Since macOS 10.15 (Catalina), security enhancements require third-party scripting additions to be code-signed with a Developer ID certificate and notarized by Apple to load and execute, preventing unsigned extensions from being used due to Gatekeeper and hardened runtime protections.[62] Apple's own additions, like Standard Additions, are exempt and automatically trusted.[63] This measure ensures that extensions cannot introduce unauthorized code execution, though it has impacted the distribution of older, unsigned OSAX from third-party sources.
System Applications and Automator
Core macOS applications provide extensive support for AppleScript, enabling automation of common system tasks through built-in scripting dictionaries that define commands, objects, and properties specific to each app.[5] The Finder application, for instance, allows scripting of file and folder operations, such as creating, moving, or deleting items on the desktop or in directories. A simple example involves using themake command to create a new folder: tell application "Finder" to make new folder at desktop with properties {name:"New Folder"}. This demonstrates how Finder exposes classes like folder and file for manipulation, facilitating batch file management without manual intervention.[48]
System Events serves as a key utility for user interface (UI) automation, particularly when direct scripting support is unavailable in target applications.[64] It simulates user interactions, such as clicking buttons, selecting menu items, or entering text, by targeting UI elements through the process class. For example, to toggle the sidebar in Safari, the script tell application "System Events" to tell process "Safari" to tell toolbar 1 of window 1 to click first button whose accessibility description is "Sidebar" accesses the app's UI hierarchy after enabling accessibility permissions in System Preferences.[64] This capability extends to non-scriptable apps, allowing broader automation of workflows involving third-party software, though it requires tools like Accessibility Inspector for element identification.[64]
The Calendar application offers robust AppleScript integration for managing events and calendars, supporting creation, modification, deletion, and querying of events.[65] Users can add attendees, set alarms, or locate events by unique identifiers; for instance, tell application "Calendar" to make new event at end of events of calendar "Work" with properties {summary:"Meeting", start date:date "November 10, 2025 10:00:00", end date:date "November 10, 2025 11:00:00"} creates a one-hour event.[66] Alarms can be attached via properties like all day event or custom triggers, and the show command reveals specific events in the UI.[67] This scripting suite enables programmatic scheduling, such as integrating with reminders or exporting data, making Calendar a cornerstone for personal productivity automation.[68]
Accessing these scripting capabilities begins with viewing an application's dictionary in Script Editor, the primary tool for exploring available terminology.[69] Users can open a dictionary by dragging the app icon onto Script Editor, selecting File > Open Dictionary (Shift-Command-O), or using the Library palette (Window > Library) to add and double-click apps like Finder or Calendar.[69] The resulting viewer displays suites, commands (e.g., make in Finder), classes (e.g., event in Calendar), and properties, often including inheritance from standard suites; enabling "Show inherited items" in Script Editor preferences reveals broader terminology. This process is essential for developers to understand and utilize app-specific syntax without trial-and-error coding.[69]
Automator, introduced as part of Mac OS X 10.4 Tiger in 2005, complements AppleScript by providing a visual workflow builder that incorporates scripting actions.[70] It allows users to assemble automations using drag-and-drop actions, including a dedicated "Run AppleScript" action for embedding custom scripts within workflows.[43] For example, a workflow might rename files via Finder scripting, process events in Calendar, or handle UI tasks through System Events, all without writing full scripts from scratch. Workflows can be saved as applications, services, or calendar alarms, exporting AppleScript-compatible results for further integration.[43] Automator itself is scriptable, permitting AppleScript control over workflow execution, which enhances its utility for complex, multi-step automations.[43]
Despite these features, AppleScript's integration with system applications has limitations, as not all macOS apps provide full scripting support, and even supported ones may omit certain tasks.[64] Third-party software often offers only partial scriptability, depending on developer implementation, which can restrict automation to basic functions while requiring UI scripting workarounds for advanced interactions.[15] Accessibility requirements for UI automation add setup overhead, and incomplete dictionaries in some apps necessitate reliance on System Events, potentially reducing reliability across diverse environments.[64]
Related Technologies
Open Scripting Architecture
The Open Scripting Architecture (OSA) is a foundational framework developed by Apple for enabling interapplication communication and scripting on the Macintosh operating system. It provides a standardized, extensible mechanism that allows scripts written in various languages to interact with scriptable applications through Apple events, which are high-level messages for requesting actions or data exchange. OSA separates the scripting language from the underlying communication protocol, permitting developers to create and integrate custom scripting engines while maintaining compatibility across applications. This architecture was announced by Apple in May 1991 as part of the preparations for System 7, marking a shift toward more robust automation capabilities in the Mac OS.[71] At its core, OSA consists of two primary components: the event manager and scripting components. The event manager, implemented via the Apple Event Manager (part of the Core Services framework), handles the sending, receiving, and dispatching of Apple events between applications, using descriptors to encapsulate data and commands in a structured format. Scripting components, also known as language engines, are modular plugins that provide the interpretation and execution logic for specific scripting languages; these are stored in directories like/System/Library/Components and identified by unique subtype codes (e.g., 'ascr' for AppleScript). Each component exposes a standard API for operations such as compiling source code into executable scripts (via functions like OSACompile), executing scripts (via OSAExecute), and managing script contexts to preserve state during interactions. This design ensures that applications can remain agnostic to the scripting language used, focusing instead on defining their own Apple event suites for automation.[72]
Engine registration occurs dynamically through the Component Manager, a system service that catalogs available scripting components at runtime. When an OSA-aware application or script runner needs to process a script, it queries the Component Manager to locate the appropriate engine based on the script's creator code or file type, then dispatches the script to that component for handling. For instance, a script file tagged with the AppleScript creator code would route to the AppleScript engine, while others could target alternative languages like early third-party systems such as UserLand Frontier. This registration model supports multiple scripting languages coexisting on the system, allowing them to uniformly send and receive Apple events to control applications, query databases, or automate workflows without direct language-specific integrations. OSA's extensibility encouraged third-party developers to build compatible engines, leading to over 80 applications supporting the framework by the mid-1990s.[16]
Although OSA remains a core element of macOS automation, with its APIs still functional in recent versions, Apple has de-emphasized its development and documentation since around 2016, coinciding with the retirement of related legacy frameworks like Carbon and a shift toward modern alternatives such as Swift scripting. The official OSA reference was marked as legacy in 2007, and while the architecture underpins tools like AppleScript, it receives minimal updates, prompting developers to explore newer interprocess communication methods for future-proofing.[73]
JavaScript for Automation
JavaScript for Automation (JXA) was introduced in OS X Yosemite in 2014 as an alternative to AppleScript for scripting tasks within the Open Scripting Architecture (OSA), enabling developers to leverage JavaScript for interapplication communication and workflow automation on macOS.[3] This addition expanded the OSA framework by incorporating JavaScript as a peer language, allowing scripts to interact with system applications, processes, and user interfaces in a manner parallel to AppleScript. JXA employs standard JavaScript syntax, including many ES6 features such as arrow functions and template literals (support varies by macOS version via JavaScriptCore), while bridging to Apple events for application control. Developers access applications via theApplication constructor, which sends Apple events equivalent to those in AppleScript; for instance, to activate the Finder, one might use Application('Finder').activate();.[3] In contexts like web browsers, JXA supports executing JavaScript within app contexts, such as Application('Safari').doJavaScript('alert("Hello World");') to run code in the frontmost tab. This bridging maintains compatibility with the same scripting dictionaries provided by applications, ensuring that commands, objects, and properties are identical to those available in AppleScript.[5]
The primary advantages of JXA stem from JavaScript's established ecosystem, making it particularly accessible for web developers transitioning to macOS automation due to its familiarity and cross-platform nature.[3] JavaScript's native support for arrays and objects provides more robust handling than AppleScript's lists—for example, methods like map(), filter(), and reduce() enable concise data manipulation without external libraries. Despite these enhancements, JXA relies on the identical application dictionaries, allowing seamless adoption of existing AppleScript terminology.[5]
JXA coexists with AppleScript within the OSA, permitting scripts in one language to invoke the other through shared runtime environments or explicit calls, such as embedding an AppleScript snippet via OSAScript in JXA or running JXA code from AppleScript handlers.[23] This interoperability supports hybrid workflows where developers can mix languages based on task suitability, with both executing in tools like Script Editor.[48]
// Example: Basic JXA script to get frontmost application
var SystemEvents = Application('System Events');
var frontApp = SystemEvents.applicationProcesses.whose('frontmost', { _equals: true })[0];
frontApp.name();
// Example: Basic JXA script to get frontmost application
var SystemEvents = Application('System Events');
var frontApp = SystemEvents.applicationProcesses.whose('frontmost', { _equals: true })[0];
frontApp.name();
Modern Usage and Limitations
Updates in Recent macOS Versions
Since macOS High Sierra (10.13) in 2017, AppleScript has maintained stability without major version releases or significant syntax changes, focusing instead on compatibility enhancements and bug resolutions to support evolving hardware and system architectures.[74] In macOS Big Sur (11) released in 2020, initial compatibility issues arose on Apple Silicon Macs, such as slower performance in Cocoa function calls from AppleScript and failures in simple scripts on M1 hardware, which were addressed through subsequent point updates and optimizations in later macOS versions like Monterey (12).[75][76] Security enhancements introduced in macOS Mojave (10.14) in 2018 significantly impacted AppleScript usage by expanding the Transparency, Consent, and Control (TCC) framework to require explicit user prompts for sending Apple events to protected applications and system services.[77] This change, aimed at preventing unauthorized automation, often manifests as dialog boxes requesting permission when scripts attempt to control apps like System Events, with developers needing to include an NSAppleEventsUsageDescription key in app bundles to inform users of the required access.[78] Sandboxing restrictions further complicate scripting in contained environments, limiting inter-app communication unless permissions are granted via System Settings > Privacy & Security > Automation.[79] AppleScript integration deepened with the introduction of the Shortcuts app in macOS Monterey (12) in 2021, allowing users to embed "Run AppleScript" actions directly within workflows for hybrid automation combining visual shortcuts and code-based logic.[80] This enables scripting complex tasks, such as processing text or interacting with apps, and supports running shortcuts from AppleScript via theshortcuts command-line tool or dictionary commands like run shortcut named.[81] Enhancements in macOS Sequoia (15) in 2024 improved Shortcuts with Apple Intelligence features, including new intelligent actions for tasks like text generation and image description, while core AppleScript actions remain consistent for backward compatibility. Deeper integration with Apple Intelligence models for more powerful automations, including access within Shortcuts workflows, arrived in macOS Tahoe (26) in 2025.[82][83]
As of macOS Tahoe (26), released on September 15, 2025, AppleScript continues to receive full native support across Intel and Apple Silicon architectures, with ongoing maintenance for stability in scripting additions and dictionary updates for system apps.[84] This ensures seamless operation in modern environments, including enhanced performance optimizations inherited from prior releases.[85]
