As you may know, I have had a Mac Book 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):
- AppleScript Tutorial for Beginners I – Getting Started & Script Editor
- AppleScript Tutorial for Beginners II – Variables and Dictionaries
- AppleScript Tutorial for Beginners III – The Power of Lists
- AppleScript Tutorial for Beginners IV – Records & Repeats
- AppleScript Tutorial for Beginners V – Testing & Shorthand
- AppleScript Tutorial for Beginners VI – User Interaction
- AppleScript Tutorial for Beginners VII – Errors
- AppleScript Tutorial for Beginners VIII – Getting the Drop on Droplets
- AppleScript Tutorial for Beginners IX – Math

2 Comments
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?
The first one is what it says: it timed out.
I don’t think you can run
osascriptremotely, 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)