Skip to content

MacOS osascript command

The MacOS osascript command is a useful utility for executing AppleScript commands directly from the command line. This allows users to automate tasks, interact with applications, and control various system functions using scripting languages. With osascript, users can seamlessly integrate AppleScript functionality into shell scripts and terminal commands, streamlining the workflow and enhancing productivity.

osascript Syntax:

Terminal window
osascript [option] [parameter]

Options:

OptionDescription
-eExecute a one-line script
-sUses stdin for script input
-lSet the language dialect
-ssUses stdin for Apple event input
-soEvent start offset for file input

Parameters:

ParameterDescription
scriptThe script to run
inputInput for the script (used with -s and -ss)
dialectScripting language dialect (used with -l)
offsetStarting position in the event input file (used with -so)

Get System Information

Terminal window
osascript -e 'do shell script "system_profiler SPHardwareDataType"'

This command uses osascript to retrieve system information by running the system_profiler command.

Display a Notification

Terminal window
osascript -e 'display notification "Hello, from osascript!" with title "Notification Title"'

Shows a system notification with a message and a custom title using the display notification command in osascript.

Execute a Shell Command

Terminal window
osascript -e 'do shell script "ls -l" with administrator privileges'

Executes a shell command with administrator privileges, in this case, listing files in the current directory using osascript.

Display a Dialog Box

Terminal window
osascript -e 'display dialog "This is a dialog box" with title "Dialog Title"'

Opens a dialog box with a message and a custom title using the display dialog command in osascript.

osascript Usage:

EXAMPLES

Common Questions on osascript Usage:

How do I run a simple AppleScript using osascript in MacOS?

To run a simple AppleScript using osascript in MacOS, use the following command:

Terminal window
osascript -e 'display dialog "Hello, World!"'

How do I execute an external AppleScript file with osascript in MacOS?

To execute an external AppleScript file with osascript in MacOS, use the following command:

Terminal window
osascript path/to/script.scpt

How can I pass arguments to an AppleScript executed with osascript in MacOS?

To pass arguments to an AppleScript executed with osascript in MacOS, use the following command:

Terminal window
osascript -e 'display dialog "Hello, $1"' -- 'World'

How do I suppress output when running an AppleScript using osascript in MacOS?

To suppress output when running an AppleScript using osascript in MacOS, use the following command:

Terminal window
osascript -e 'display dialog "Hello, World!"' > /dev/null

How can I execute a JavaScript in an AppleScript file using osascript in MacOS?

To execute a JavaScript in an AppleScript file using osascript in MacOS, use the following command:

Terminal window
osascript -l JavaScript path/to/script.applescript

How do I get the result of an AppleScript executed through osascript in MacOS?

To get the result of an AppleScript executed through osascript in MacOS, use the following command:

Terminal window
result=$(osascript -e 'display dialog "Hello, World!"'); echo $result

How can I specify a different language when executing an AppleScript with osascript in MacOS?

To specify a different language when executing an AppleScript with osascript in MacOS, use the following command:

Terminal window
osascript -l AppleScript path/to/script.applescript

Applications of the osascript command

  • Execute AppleScript commands
  • Automate tasks in MacOS
  • Interact with applications and system preferences
  • Access and control various functionalities of MacOS
  • Retrieve system information
  • Control and manage scripting capabilities in MacOS