The Divide Between GUI and Terminal
In macOS, there is generally a strict divide between the Terminal (where you type Unix commands like ls and grep) and the graphical user interface (where you click buttons in Safari or Apple Music). Standard bash scripts cannot easily click buttons, trigger graphical pop-up windows, or command graphical apps to perform specific tasks.
To automate graphical applications, Apple created AppleScript, a highly readable scripting language designed to control macOS apps. However, AppleScript is traditionally written and executed inside the dedicated “Script Editor” app, not the Terminal.
If you are writing a massive bash script in the Terminal and you suddenly need to trigger a graphical pop-up window or tell Apple Music to pause, you must use the osascript (Open Scripting Architecture Script) command. This powerful tool acts as a bridge, allowing you to execute native AppleScript code directly from the command line.
Step 1: Running One-Line AppleScripts
The easiest way to use osascript is to pass it a single line of AppleScript code using the -e (execute) flag.
For example, if you want your bash script to display a native macOS notification banner in the top-right corner of the screen, run this in the Terminal:
osascript -e 'display notification "Backup Complete!" with title "System Alert"'
Because you enclose the AppleScript code in single quotes (' '), the bash shell ignores the text and passes it cleanly to the Open Scripting Architecture engine, which executes it graphically.
Step 2: Controlling Graphical Applications
You can use osascript to command almost any native macOS application to perform an action.
If you want to instantly pause the music playing in the Apple Music app, you can run:
osascript -e 'tell application "Music" to pause'
If you want to force Safari to open a specific webpage in a new tab, run:
osascript -e 'tell application "Safari" to open location "https://apple.com"'
This is incredibly useful for developers who want to automate their morning workflow. A single bash script can boot up, launch Docker, start a Node.js server, and then use osascript to automatically open Safari to localhost:3000.
Step 3: Creating Interactive Pop-Ups
Sometimes a bash script needs human input before it can continue. You can use osascript to generate a graphical prompt, and then capture the user’s input back into your bash script.
osascript -e 'display dialog "Enter the server IP:" default answer ""'
This command will freeze the Terminal and pop up a beautiful, native macOS dialog box on the desktop with a text field. When the user types the IP address and clicks “OK,” osascript will print the result back to the Terminal (e.g., text returned:192.168.1.50), which your bash script can then parse and use.
Step 4: Running Full AppleScript Files
If your AppleScript logic requires 50 lines of complex if/then statements to control an Excel spreadsheet, jamming it all into one massive Terminal command using fifty -e flags is impossible to read.
Instead, write your AppleScript cleanly in a text file (e.g., automation.scpt) using the Script Editor app.
Then, from the Terminal, you can execute the entire file simply by pointing osascript at the file path:
osascript /Users/admin/Desktop/automation.scpt
The Terminal will run the script, wait for the graphical automation to finish, and then return the prompt back to you.