osascript - macOS


Overview

osascript is a macOS command-line tool for automating tasks via AppleScript. It allows users to interact with macOS applications, Control Center widgets, and perform high-level system functions like controlling the volume or setting the screen resolution.

Syntax

osascript [options] [AppleScript script]

Options/Flags

  • -e, –eval: Execute the specified AppleScript script as a one-line command.
  • -f, –file: Load the AppleScript script from the specified file and execute it.
  • -l, –language: Optionally specify the AppleScript version to use (e.g., “-l JavaScript”).
  • -d, –debug: Run the script in debug mode to assist troubleshooting.

Examples

  • Execute a one-line script:
osascript -e 'tell application "System Events" to set volume 10'
  • Run a script from a file:
osascript -f /path/to/my_script.scpt
  • Run a JavaScript for Automation script:
osascript -l JavaScript -e 'Application("System Events").setVolume(10)'
  • Debug a script:
osascript -d -f my_script.scpt

Common Issues

  • Syntax errors: Check that your AppleScript syntax is correct using the Debugger or AppleScript Editor.
  • Permission issues: Make sure the script has appropriate permissions to perform the desired actions.
  • Compatibility: Ensure that the script is compatible with the current macOS version and application.

Integration

osascript can integrate with other macOS commands through scripting. For instance, you can pipe a script into osascript to interact with the output:

ls -l | osascript -e 'tell app "Terminal" to activate'
  • applescript: Compile AppleScript scripts into a standalone executable.
  • Script Editor: A graphical interface for creating and editing AppleScript scripts.
  • AppleScript documentation: Official documentation for AppleScript.