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

Sep 6, 2016 - 8 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.

.

Related articles:

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

8 Comments

» Comments RSS Feed

  1. Bachsau says:

    Isn’t there a way to just tell *the system* (Quarz, Aqua, whatever) to display a dialog just like any normal application window? Attaching it to an already running application sounds like a bunch of bs to me and I bet it won’t work with applications not using Aqua at all.

    • baun says:

      Sure, you can use NSAlert in obj-c or in Swift you can use something like the following to create a:

      func dialogWindow(question: String, text: String) -> Bool {
      let alert = NSAlert()
      alert.messageText = question
      alert.informativeText = text
      alert.alertStyle = NSAlertStyle.warning
      alert.addButton(withTitle: "OK")
      alert.addButton(withTitle: "Cancel")
      return alert.runModal() == NSAlertFirstButtonReturn
      }

      let answer = dialogWindow(question: "This is a dialog", text: "Would you like to see this?")

  2. ash says:

    hey i was wondering if you can make a pop up come up when you access certain websites like facebook

  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.

  4. RM says:

    nice! thank u

  5. 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

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site