osascript

On this page 7

osascript is the macOS command-line utility that executes Open Scripting Architecture (OSA) scripts, most often AppleScript or JavaScript for Automation, from a shell prompt, a script file or another program. Within Terminal & scripting on macOS, it is the bridge between the Unix world of zsh, pipes and exit codes and the Apple event world in which applications such as Finder, Mail or System Events expose scriptable objects.

Because it lets a shell script control graphical applications, display dialogs and query application state, osascript appears in installer packages, management scripts, developer tooling and personal automation. Its power also makes it a frequent subject of privacy prompts and security monitoring.

How osascript runs scripts through the Open Scripting Architecture

osascript works by loading an OSA language component, compiling the supplied source into an executable form and running it inside its own process. The default component is AppleScript; the option -l JavaScript selects JavaScript for Automation (JXA), and third-party OSA languages can be installed, although they are rare today.

When a script addresses an application, for example tell application "Finder", osascript sends Apple events to that application through the system’s interprocess messaging layer. The target application performs the action and returns a reply, which osascript converts to text and writes to standard output. Errors go to standard error, and the process returns a nonzero exit status, so shell scripts can test the result in the usual way.

Source code can arrive in three ways: one or more -e statements on the command line, a script file path (plain text .applescript, .js or compiled .scpt), or standard input when no file is given. Arguments that follow the script path are passed to the script’s run handler as a list, which allows parameterized scripts without string concatenation.

Key options and invocation forms

A small set of flags covers nearly every practical use of osascript. The following table lists the options administrators and developers rely on most often.

OptionPurposeTypical use
-e statementRuns one line of script source; repeatableShort one-liners in shell scripts
-l languageSelects the OSA language component-l JavaScript for JXA code
-iInteractive mode, reads statements line by lineQuick experiments in Terminal
-s h or -s sHuman-readable or recompilable output formatParsing results reliably
-s oSends script errors to standard outputLogging errors with results
Script path plus argumentsRuns a file and passes arguments to runReusable parameterized tools
Common osascript options and their effect

The output format flag deserves attention. With -s s, lists and strings are returned in source form, such as {"a", "b"}, which preserves quoting; the default human-readable form strips quotes and joins list items with commas, which can make parsing ambiguous when values contain commas.

osascript is one entry point among several for macOS automation, and choosing well depends on where the code runs and who maintains it. The comparison below places it next to its closest neighbors in the Topic.

ToolInterfaceBest suited for
osascriptCommand lineCalling Apple events from shell scripts
Script EditorGraphical editorWriting, testing and saving scripts or applets
ShortcutsVisual actions, also shortcuts commandUser-facing workflows across devices
defaultsCommand lineReading and writing preference domains directly
openCommand lineLaunching apps, files and URLs without scripting
Where osascript fits among macOS automation tools

The language choice also matters. AppleScript has the richest documentation and the most examples for application dictionaries, while JavaScript for Automation offers familiar syntax, JSON handling and the Objective-C bridge. Both reach the same scripting dictionaries, so the decision is mostly about the maintainers’ skills.

Practical uses in shell scripts and administration

The most common pattern is embedding a short AppleScript in a zsh or bash script to perform one task that has no command-line equivalent. Typical tasks include showing a notification with display notification, asking the user a question with display dialog, quitting an application gracefully, or reading the frontmost window title.

  • User notifications from long-running scripts, such as a backup finishing.
  • Graceful application shutdown with tell application "Safari" to quit, which lets the app save state, unlike kill.
  • Interactive prompts in installer or onboarding scripts run by management tools.
  • Querying system state, for example the current Wi-Fi network shown in an app or the volume level with output volume of (get volume settings).
  • Here-documents that pass multi-line scripts through standard input, which avoids fragile quoting.

When a management agent runs scripts as root, osascript calls that target the logged-in user’s applications should run in the user’s context, typically through launchctl asuser combined with sudo -u. Otherwise the Apple events may be sent from the wrong session and fail or behave unpredictably.

Permissions, security and common errors

Since macOS Mojave, Transparency, Consent and Control (TCC) requires explicit approval before one process can send Apple events to another application. The first time a script controls an app, macOS asks the user to grant Automation permission to the responsible process, which is often Terminal, iTerm2 or a management agent rather than osascript itself.

Scripting user interface elements through System Events additionally requires Accessibility permission. In managed fleets, these approvals are granted silently with a Privacy Preferences Policy Control (PPPC) configuration profile, although some categories still require user consent by design.

SymptomLikely causeFix
Error -1743Automation permission not grantedApprove in Privacy & Security or deploy a PPPC profile
Error -1719 or -25211Accessibility access missing for UI scriptingGrant Accessibility to the calling process
Error -600Target application is not runningLaunch it first or test with application id ... is running
Error -1728Referenced object does not existCheck the dictionary and object path
Script hangs for 2 minutesDefault Apple event timeout reachedUse a with timeout block or fix the blocked app
Frequent osascript failures and how to resolve them

From a defensive perspective, attackers have used osascript to show fake password dialogs and to run scripts filelessly. Endpoint security tools therefore log and flag unusual osascript invocations, especially display dialog with hidden answers launched from unexpected parents. Legitimate scripts should avoid collecting credentials this way.

In plain words

Many Mac applications understand a special set of commands, a bit like a remote control that other programs can press. osascript is a small tool that lets you press those buttons from the command line. You type or save a short script, and osascript delivers it to the right application.

This means a simple text script can tell Mail to check for messages, ask Finder to open a folder, or show a pop-up asking a question. The key point is that osascript connects typed commands with ordinary Mac apps, and macOS asks for permission before it lets one program control another.

A real-world example

An IT administrator at a design studio with 80 managed Macs needs to install a large update to a creative suite. The update fails if the application is open, and forcibly killing it would lose unsaved work. The administrator writes a zsh script that the management agent runs as root at 6 p.m.

The script first checks whether the application is running. If it is, it uses launchctl asuser with osascript to show the logged-in designer a dialog with a 10-minute deadline, offering Save and Quit or Postpone. After the designer chooses Save and Quit, osascript tells the application to quit gracefully, the script confirms the exit status is zero, and the installer proceeds.

A PPPC profile deployed earlier pre-approves Automation access for the agent, so no unexpected prompts appear. The result is that updates complete on almost every machine overnight without lost files or support tickets.