The Bridge Between Terminal and GUI
The macOS Terminal is incredibly powerful, but it exists in a silo. A standard bash script cannot easily click a button in the Safari web browser, change the system volume, or tell the Apple Music app to skip to the next track.
To automate the graphical interface of macOS, Apple created AppleScript, an English-like scripting language. Usually, users write and execute these scripts using the dedicated Script Editor app. However, if you want to trigger graphical automations directly from the command line-or embed them inside a larger bash script-you must use the osascript command.
osascript executes Open Scripting Architecture (OSA) scripts, acting as a powerful bridge between the rigid, text-based terminal and the dynamic, windowed environment of macOS.
1. Executing One-Line Scripts
The most common use of osascript is triggering a single command directly from the terminal prompt using the -e (execute) flag.
Controlling System Volume
To instantly mute your Mac from the terminal:
osascript -e 'set volume with output muted'
To set the volume to exactly 50% (the scale is 0 to 100):
osascript -e 'set volume output volume 50'
Controlling Applications
You can send commands directly to running applications. To tell the Music app to pause playing:
osascript -e 'tell application "Music" to pause'
To tell Safari to open a specific URL in a new tab:
osascript -e 'tell application "Safari" to open location "https://digitash.com"'
2. Creating Graphical Pop-ups from Bash
If you write a bash script that runs in the background (like a cron job), it has no easy way to alert the user when it finishes. osascript allows you to inject native macOS dialogue boxes and notifications onto the screen.
Displaying a Notification
To trigger a standard macOS notification banner in the top-right corner of the screen:
osascript -e 'display notification "The server backup has completed successfully." with title "Backup Status"'
Displaying an Interactive Alert
To display an aggressive, center-screen dialogue box that requires the user to click an “OK” button:
osascript -e 'display dialog "Critical Error: Cannot connect to the database." buttons {"OK"} default button "OK" with icon caution'
3. Executing Multi-Line Script Files
If you have a complex AppleScript (e.g., a script that loops through 50 open Finder windows and closes them all), writing it as a single line with the -e flag is impossible.
Instead, write your script in a standard text file and save it with an .scpt or .applescript extension (e.g., cleanup.scpt).
You can then execute the entire file from the terminal by simply passing the file path to osascript:
osascript ~/Documents/cleanup.scpt
4. Executing JavaScript for Automation (JXA)
While AppleScript is powerful, its English-like syntax is often frustrating for modern developers. macOS actually supports writing automation scripts in pure JavaScript (JXA).
You can tell osascript to interpret the code as JavaScript rather than AppleScript by using the -l (language) flag.
osascript -l JavaScript -e 'Application("Music").play()'
Conclusion
The osascript command is the skeleton key for macOS automation. By integrating it into your terminal workflows, you can build bash scripts that don’t just manipulate text files, but actively command the graphical operating system, providing visual feedback and automating complex UI interactions instantly.