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
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.
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
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
osascript remotely, which may explain
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)
I got this error when I forgot the closing single quote!
I get this one too, but it goes away when I remove the icon reference…
And you don’t get any of my errors?
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.
Thanks man, but you can actually just use “osascript -e ‘display dialog …” no need for SystemEvent etc