Mac Unix shell + AppleScript FAQ: How can I display a dialog with AppleScript from the Unix command line shell? (Either from a command line shell like bash through the MacOS Terminal, or from a shell crontab job.)
AppleScript from the Mac Unix shell (using osascript)
This “Unix shell and AppleScript” tip is still baking in the oven — meaning I haven’t tested it much, and it has known (and unknown) side effects — but, if you want to run an AppleScript command from the Mac Unix shell (either a Bash shell command in the MacOS Terminal, or as a cron/crontab job) on MacOS, such as having the ability to display a dialog from a Unix shell script or other bash shell command, you can run the osascript
command like this from your Unix command line:
osascript -e 'tell app "System Events" to display dialog "Yada yada yada"'
When you run this command from the Mac OS X Unix shell, it displays a dialog with the text “Yada yada yada,” as shown in this figure:
In short, osascript
lets you “execute AppleScripts and other OSA language scripts.” (See the "osascript" man page on Apple's website for more details.)
AppleScript dialog from the Unix shell - known problems
There are several problems with this approach I haven’t worked out yet. First, there is a long error message in the output from this AppleScript/osascript command:
2010-05-16 09:39:30.091 osascript[31732:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find: /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
Second, this AppleScript Unix shell command waits for the user to click one of the buttons. This is actually a very desirable feature for an interactive Unix shell script, but for my purposes, I want to run this from a background Unix process being run through cron (or something like cron), and I don’t want my shell script to come to a complete stop. Therefore, I will probably have to run this with the usual Unix nohup
and &
syntax, and also redirect that error output.
Third, the AppleScript dialog seems to be created as some sort of “background” dialog. I don’t understand this yet, but here are two symptoms I see:
- When first created, the dialog does not jump to the foreground and interrupt my work. In fact, it can easily get hidden behind other windows.
- When I tried to take a picture of this dialog, it kept jumping behind other windows I had open on my desktop. I'd have to describe the behavior as this dialog is a child of the desktop, and any other window that is displayed has a higher z-order.
AppleScript dialog from Unix shell: possible solution
A possible solution to this problem is to display the dialog through the Finder, like this:
osascript -e 'tell app "Finder" to display dialog "Yada yada yada"'
This makes the Finder icon jump in the Dock, and when I click it, my dialog is displayed in the foreground. Also, when I try to take a picture of it, it doesn’t jump to the background.
This may be a better approach for my needs, as I’m running a background process to monitor some things, and if something goes wrong, I want to see this dialog asap, so if the Finder icon starts bouncing around in the Dock, well, I’m okay with that.
Summary: Running AppleScript from the Unix command line
As mentioned, this command to run an AppleScript command from the Unix command line (the Bash shell) is still a work in progress, with the known issues shown above (and other problems yet to be found). FWIW, the keys to this solution seem to be (a) knowing that the osascript
command exists, and then (b) working through the details of the AppleScript language.
As I work through these issues I’ll update this article, but in the meantime, if this helps point anyone who is looking to run an AppleScript command from the Unix command line in the right direction, well, I hope this helps a little bit.