docwhat's avatardocwhat's blog

Mac Tricks: Shell Script Dialogs

As you may know, I have had a MacBook Pro (15” - glossy) for a few months now. I am starting to feel pretty comfortable in it and have started to learn more of the advanced features that I don’t need to get things done, but because I like learning how stuff works.

In this case, I discovered how to create dialogs from the command line or a shell script. In Linux, I’d use gdialog or something similar.

On the mac, I’m I discovered osascript. This is a way into the AppleScript goodness inside OS X. You can get more documentation, like most things in the command line world of OS X, using man.

An example creating a ‘stop’ dialog:

osascript -e 'tell app "System Events" to display dialog "A stop dialog with only one button." buttons "OK" default button 1 with title "and a title" with icon stop'

I would assume there are other values you could feed in for the icon, but I haven’t a clue and I can’t find it in Script Editor Library. How annoying. [Note: I did end up finding it in the Apple Script Language Guide, see below.]

Here’s a version with multiple buttons:

osascript -e 'tell app "System Events" to display dialog "A dialog with no buttons and the exit code will tell the script which button was pressed."'

If the exit code is 0, then “OK” was pressed; otherwise it was canceled. Errors go to stderr, so you can catch them and hide them.

Ah-ha! I found the missing display dialog documentation. It was in the Apple Script Language Guide. Excellent.

Here are some tutorials aimed at AppleScript beginners (and I mean beginners; I’d much prefer a “Dive into” style tutorial):

  1. AppleScript Tutorial for Beginners I - Getting Started & Script Editor
  2. AppleScript Tutorial for Beginners II - Variables and Dictionaries
  3. AppleScript Tutorial for Beginners III - The Power of Lists
  4. AppleScript Tutorial for Beginners IV - Records & Repeats
  5. AppleScript Tutorial for Beginners V - Testing & Shorthand
  6. AppleScript Tutorial for Beginners VI - User Interaction
  7. AppleScript Tutorial for Beginners VII - Errors
  8. AppleScript Tutorial for Beginners VIII - Getting the Drop on Droplets
  9. AppleScript Tutorial for Beginners IX - Math

Comments

Gravatar for jonathan flusser
Jonathan Flusser

Tell me why whenever I go and try to do this, on MY Mac it says “28:89: execution error: System Events got an error: AppleEvent timed out. (-1712)” and on a remote Mac (MBP) it says “28:89: execution error: An error of type -10810 has occurred. (-10810)“?? Is it just because this post is older?

Gravatar for docwhat
docwhat

The first one is what it says: it timed out.

I don’t think you can run osascript remotely, which may explain number two.

I’m getting my own errors: 28:150: execution error: System Events got an error: Can’t make application “System Events” into type number or string. (-1700)

Gravatar for philippp
Philippp

I got this error when I forgot the closing single quote!

Gravatar for jp
JP

I get this one too, but it goes away when I remove the icon reference…

Gravatar for alexander
Alexander

I know this is late, and you probably already figured this out in the last 4 years, but you just have to replace “System Events” with any application you have open at the time of running the command (eg. “Terminal”). You’re basically telling the computer what application you want to make the pop-up from.

Gravatar for georg
Georg

Thanks man, but you can actually just use “osascript -e ‘display dialog …” no need for SystemEvent etc

Submit a Comment

docwhat

The personal blog of Christian Höltje.
docwhat docwhat contact