Yes, you can trigger an alert dialog on macOS from the command line using AppleScript or the built-in osascript command. The simplest one-liner is osascript -e 'display alert "Hello"', which shows a standard macOS alert with an OK button. You can also add buttons, icons, and even text input fields.
Background: Why Use a Command-Line Alert?
Command-line alerts are useful when you want a script or a background process to get your attention without opening a terminal window. For example, a cron job that finishes a long backup, a build script that hits an error, or a monitoring tool that detects a problem can all pop up a visible notification. This is particularly handy for users who work in the terminal but want a graphical nudge when a task completes.
How It Works: The osascript Command
osascript is a macOS utility that runs AppleScript and JavaScript for Automation (JXA) code from the command line. It sends the script to the system’s AppleScript engine, which then executes it. The display alert command is part of the Standard Additions suite, which provides a simple way to show a modal dialog.
The basic syntax is:
osascript -e 'display alert "Your message here"'
You can also use multiple -e flags or a single string with line breaks. For more control, you can use the full AppleScript syntax:
osascript -e 'display alert "Title" message "This is the body text."'
This shows a dialog with a title and a message. You can also specify buttons and an icon.
Customizing the Alert
The display alert command supports several parameters:
- as critical — Shows a warning icon (the alert symbol).
- buttons — A list of button names (e.g., {“Cancel”, “OK”}).
- default button — Which button is highlighted by default.
- giving up after — Number of seconds before the alert automatically dismisses.
Example with multiple buttons and a timeout:
osascript -e 'display alert "Update Available" message "Do you want to install now?" buttons {"Later", "Install"} default button "Install" giving up after 30'
This shows a dialog with two buttons, defaults to “Install”, and disappears after 30 seconds if no choice is made.
Using JavaScript for Automation (JXA)
If you prefer JavaScript, you can use JXA. The equivalent command is:
osascript -l JavaScript -e 'app.displayAlert("Hello")'
But JXA’s dialog methods are slightly different. The displayAlert method is available on the app object, but it’s less flexible than the AppleScript version. For most purposes, the AppleScript syntax is simpler and more common in examples online.
Common Problems and Solutions
Here are a few issues you might run into:
- Alert appears behind other windows — Use
display alert ... as criticalto force it to the front, or addwith titleto give it a title bar. - Cannot run osascript from a non-interactive shell — If you’re using SSH or a launchd script, the alert may not appear because there’s no GUI session. You can still use
display notification(which shows a notification banner) as a fallback. - Special characters in the message — Escape double quotes and backslashes inside the AppleScript string. Use
\"for a quote. - Alert flashes and disappears — This can happen if the script exits immediately after showing the alert. Add a delay or use
display alert ... giving up after 0to keep it open until clicked.
Alternatives: Notification Center and Terminal Notifier
If you don’t need a modal dialog, you can use osascript -e 'display notification "Message" with title "Title"' to show a banner from Notification Center. This is less intrusive and doesn’t require user interaction. There are also third-party tools like terminal-notifier that offer more control, but they require installation. For a pure built-in solution, display notification is a good choice.
Frequently Asked Questions
Can I make the alert appear in the front even if another app is active?
Yes, use as critical in the display alert command. This gives the dialog a warning icon and forces it to the front. However, macOS may still not bring it forward if the system is locked or if another app is in full-screen mode.
How do I get the clicked button as a result?
Use button returned of (display alert ...). For example: osascript -e 'set b to button returned of (display alert "Question" buttons {"No", "Yes"})' -e 'if b is "Yes" then do shell script "echo yes"'. This stores the button name in a variable.
Can I show a text input dialog?
Yes, use display dialog instead of display alert. The command is osascript -e 'display dialog "Enter your name:" default answer "" buttons {"Cancel", "OK"} default button "OK"'. The result includes a text returned property.
Does this work on macOS Catalina and later?
Yes, osascript and the display alert command have been part of macOS for decades and continue to work in current versions. However, you may need to grant automation permissions if you’re running it from a third-party app.
Can I use this in a shell script that runs on a schedule?
Yes, but be aware that if the script runs in the background without a GUI session (e.g., from a launchd agent), the alert may not appear. In that case, use display notification or write to a log file instead.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Luxury V6 Sports Sedan: Can It Easily Top 100k Miles?
- Puppets in TV Shows: Classic Proof They’re Better
- Best Streaming Remote: Top Picks for Every Setup
- Claude Pro Hidden Features Power Users Try
- Tesla Rental Prices: Why They’re So Low255.00 USD
- Best Family Western Movies to Stream Right Now