MacOS – Display dialog from command line (like xmessage does)

10.710.8command linedialogmacosscript

Background

I'd like to be able to display a notification window in OS X from within a script. The script will be running in background, so the notification should not go to the terminal but instead be displayed in a dialog window.

What I found so far

xmessage

On Linux I'd probably use xmessage or one of its variants for this. But on OS X, starting the bulky X server just for this task seems both overkill and too slow for my taste.

Finder dialog

I've found a similar question in this forum thread, where the “accepted” reply suggested this command:

osascript -e 'tell app "Finder" to display dialog "Hey!"'

But on my OS X , the resulting dialog isn't displayed immediately. Instead, the Finder icon starts to bounce, and the dialog becomes visible only after I click on the icon. Too many mouse clicks, too disruptive.

Notifier

There is an answer on Super User which suggests a tool called terminal-notifier. Unfortunately that appears to only work for OS X , so it won't help in my case.

Question

So is there some tool, either included in OS X or freely available, which allows displaying a small dialog to the user without too much overhead?

Best Answer

One option is to tell a background process like SystemUIServer to display the dialog:

osascript -e 'tell application "SystemUIServer"
display dialog "message"
end
activate application (path to frontmost application as text)'

You can also tell the frontmost application to display a dialog, but it's not shown immediately if the application is not responding. If MPlayer OS X is frontmost, text dialogs don't accept any keyboard input.

osascript -e 'tell application (path to frontmost application as text)
display dialog "message"
end'