AppleScript

On this page 7

AppleScript is the English-like scripting language built into macOS that automates applications and system tasks by sending them Apple events, the interprocess messages that scriptable apps understand. Within Terminal & scripting, it is the classic tool for controlling graphical apps such as Finder, Mail, Numbers or Photoshop, which shell commands alone cannot drive.

Introduced with System 7.1.1 in 1993, AppleScript still ships with every Mac. Scripts are written in Script Editor, run from the command line with osascript, or embedded in Shortcuts, Automator workflows, Folder Actions and third-party launchers.

How AppleScript sends Apple events

AppleScript works as a translator between readable commands and Apple events. A statement such as tell application "Finder" to get name of every window is compiled into a series of events addressed to the Finder process. The target app executes the request and returns a result, which the script can store in a variable or pass on.

Each scriptable app publishes a scripting dictionary, technically an sdef file, that lists its classes, properties and commands. Script Editor shows this dictionary through File > Open Dictionary, and it is the primary reference for what a given app can do. Apps without a dictionary can be controlled only indirectly, through GUI scripting of menus and buttons via System Events.

The language is built on the Open Scripting Architecture (OSA), which allows other languages to use the same event machinery. The most important sibling is JavaScript for Automation, introduced in OS X Yosemite, which reaches the same dictionaries with JavaScript syntax.

The following table summarizes the building blocks a script relies on.

ElementPurposeExample
tell blockDirects commands to one apptell application “Mail”
Object specifierIdentifies an element inside an appfirst message of inbox
HandlerDefines a reusable routineon run, on open, custom handlers
Record and listHold structured data{name:”A”, size:2}
try blockCatches runtime errorstry … on error … end try
do shell scriptRuns a shell command and returns outputdo shell script “date”
Core building blocks of the language

Ways to run AppleScript on macOS

AppleScript can be run from several entry points, and the choice affects permissions, performance and how users trigger the automation. Compiled scripts are stored in the .scpt format, while plain-text source uses .applescript.

MethodFile or commandBest suited for
Script Editor.scpt or .applescriptWriting, testing and reading dictionaries
osascriptosascript -e or a script pathCalling scripts from zsh, cron or launchd
Script applet.app saved from Script EditorDouble-click tools and drag-and-drop droplets
Shortcuts actionRun AppleScript actionMixing scripts with modern Shortcuts steps
Folder ActionsScript attached to a folderReacting to files added to a folder
Script menu~/Library/ScriptsQuick access from the menu bar
Execution methods and their typical use

From the shell, osascript is the bridge between Terminal and the GUI. It allows a zsh script, a Launch agent or a CI job to open documents, show dialogs or query app state, while do shell script works in the opposite direction and lets AppleScript call command-line tools such as mdfind or diskutil.

AppleScript compared with other macOS automation tools

AppleScript fits a specific niche: deep control of scriptable apps with a stable, documented interface. Other tools are better for file processing, quick personal automations or cross-platform work.

ToolMain strengthMain weakness
AppleScriptDirect access to app dictionariesVerbose syntax and inconsistent dictionaries
JavaScript for AutomationFamiliar JavaScript syntaxSparse documentation and small community
Shell scriptingFast file and system operationsCannot control most GUI apps
ShortcutsVisual building and iPhone parityLimited logic and debugging
AutomatorSimple workflows without codeLegacy status with few updates
Comparison with neighboring automation options

In practice, these tools are combined rather than chosen exclusively. A common design uses Shortcuts or a launcher as the trigger, AppleScript for the app-specific steps, and shell commands for heavy text or file processing.

Privacy permissions and security controls

Since macOS Mojave, AppleScript is governed by Transparency, Consent and Control (TCC). The first time a script or host app sends Apple events to another app, macOS asks the user to approve Automation permission for that specific pair. GUI scripting additionally requires Accessibility permission, and reading protected locations may need Full Disk Access.

Permissions belong to the process that runs the script, which is often Terminal, Script Editor or a saved applet, not the script file itself. This explains why a script that works in Script Editor fails when launched from cron or from another tool. Applets distributed to other Macs should be signed with codesign and notarized, or Gatekeeper will block them; the xattr command can show the quarantine attribute that triggers this check.

Limitations and common mistakes

AppleScript has a reputation for being easy to read but hard to write, because its natural-language syntax hides strict grammar rules. Most failures come from dictionary quirks, timing and permissions rather than from the language itself.

SymptomCauseFix
Error -1743Automation permission not grantedAllow it in Privacy and Security settings
Error -1728Object specifier points to nothingCheck the element exists before using it
AppleEvent timed outApp busy longer than 120 secondsWrap the call in a with timeout block
GUI script breaks after updateMenu or button labels changedPrefer dictionary commands over UI clicks
Works in editor, fails in launchdDifferent host process lacks permissionGrant permission to the actual runner
Typical errors and how to resolve them
  • Assuming every app is scriptable: many modern apps offer no dictionary at all.
  • Relying on delay statements instead of checking app state, which makes scripts fragile on slower Macs.
  • Ignoring that Apple has added few new language features since about 2014; the language is maintained but not actively evolving.

Stripped to the essentials

AppleScript is a way to give written instructions to Mac apps, almost in everyday English. You write something like “tell Mail to check for new messages”, and the app does it as if you had clicked the button yourself.

It is useful for repetitive jobs that would otherwise take dozens of clicks. The Mac asks for your permission before one app is allowed to control another, so nothing runs behind your back.

Where it is applied

A small prepress studio receives about 150 customer PDF files per week that must be checked, renamed and moved into job folders. An operator previously opened each file, noted its page count and dimensions, and filed it by hand, which took roughly five hours a week.

The studio’s technician writes an AppleScript droplet saved as an applet. When files are dropped onto it, the script reads page count and size through a PDF tool’s dictionary, uses do shell script with mdls to fetch metadata, renames each file to the pattern job number, customer and page count, and moves it into the matching folder in Finder. Files that fail a size check are tagged red for manual review.

After a one-time Automation approval for Finder and the PDF app, the weekly intake drops to about twenty minutes, and filing errors, which previously occurred several times a month, practically disappear.