How to Trigger an Alert Dialog Pop-Up from Command Line in Mac OS

Sep 6, 2016 - 5 Comments

Trigger an alert dialog from command line on Mac

Ever wished you could make a dialog alert pop-up on the Mac by way of the Terminal? Well it turns out that you can with the always useful osascript command, which allows execution of AppleScript from the Terminal. For those who spend a lot of time at the command line of MacOS, this can be a great little trick to notify you of when a specific task has completed, or even to be included as part of a script. This is sort of a visual approach to one of my favorite simple Terminal tricks which is to verbally announce when a command line task has completed.

Let’s review how advanced Mac users can trigger alert dialog boxes in the MacOS GUI from the command line. You can choose to specify a specification application to trigger the pop-up alert to appear within, or, perhaps better yet, trigger a alert dialog in whatever the foremost application in Mac OS X is.


And yes this works in every version of macOS or Mac OS X that has existed, so there shouldn’t be any compatibility issues here.

How to Make a Dialog Alert Pop-Up in Mac OS

Perhaps the most useful dialog alert is one that is visible from anywhere and is thus sent to whatever is the foremost application. This insures the alert box isn’t missed.

The syntax to trigger a dialog alert box in the frontmost application on the Mac is as follows:

osascript -e 'tell application (path to frontmost application as text) to display dialog "Hello from osxdaily.com" buttons {"OK"} with icon stop'

The resulting pop-up alert box looks like this:

Trigger alet dialog on Mac from command line

For example, you could use this to trigger a dialog box in the frontmost application when a task at the command line has completed. Let’s say we’re running a python script and want an alert box to notify us when it has completed, the syntax for such a use case could look like the following:

python MagicScript.py && osascript -e 'tell application (path to frontmost application as text) to display dialog "The script has completed" buttons {"OK"} with icon caution'

That example would trigger a dialog box that says “The script has completed” with the yellow caution icon to the frontmost application in Mac OS X GUI after python has finished running ‘MagicScript.py’. You can pick other icons like stop, note, caution, or even specify a path to a custom icon if desired.

While you can specify an application, System Events, or SystemUIServer, choosing the broader frontmost application allows the alert dialog window to appear onscreen no matter what application is at the forefront. Let’s cover triggering dialog alerts into specific apps, since that may be desirable as well.

Trigger a Dialog Alert in Specific Application

To send a dialog or alert into a specific application, simply specify the app name in question, like so:

Triggering an alert dialog in Mac OS Finder by way of command line:
osascript -e 'tell app "Finder" to display dialog "Hello from osxdaily.com"'

Triggering an alert dialog in Terminal app via command line:
osascript -e 'tell app "Terminal" to display dialog "Hello from osxdaily.com"'

Triggering an alert dialog in Safari via command line:
osascript -e 'tell app "Safari" to display dialog "Hello from osxdaily.com"'

Trigger an alert dialog to System Events by way of command line:
osascript -e 'tell app "System Events" to display dialog "Howdy Doo"'

You can specify any application to send the alert to this way, but for many of us the broader frontmost or System Events are likely the more useful choice.

If a general pop-up dialog trigger is too intrusive, you might appreciate sending alerts to the Notification Center on Mac with terminal-notifier, terminal-notifier is a third party solution that allows command line messages to appear in the general Notifications Center of Mac OS. An even less invasive option would be to trigger a notification badge onto the Terminal Dock icon though that may be too subtle for many users needs.

Anyway, this is a basic overview of triggering visual alert dialogs into the graphical interface of Mac OS by way of the command line. You can go much deeper than this if desired through more complex uses of AppleScript and osascript including having interactions with the dialog box impact what happens next, but that’s approaching a more complex topic which would be better served in it’s own article. Users who are interested in learning more about scripting with AppleScript can review the documentation included with the Script Editor app which is quite thorough and detailed.

Have any interesting ways to use this tip, or know of another method to trigger dialog boxes into the GUI of Mac OS from the command line? Let us know in the comments.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

5 Comments

» Comments RSS Feed

  1. Fnordmeister says:

    Here’s how to make a simple alarm clock:

    echo osascript -e ‘tell app “Finder” to display dialog “Time to go home!”‘ | at 5:00 PM Wed

  2. RM says:

    nice! thank u

  3. Conor says:

    There are quite a few options here regarding icons, buttons (and their behavior), even including more advanced stuff like returning an item from a list.

    If you aren’t an AppleScript guru or have all of the relevant code memorized, there are apps out there (check the Mac App Store) that provide a UI to construct the dialog box and output the osascript code required.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Recent Posts