AppleScript vs. Bash script?

I'm a long-time Windows "power user" and developer who has recently decided to move over to Mac OS X.

On OS X Lion and indeed previous versions there seem to be two main options for scripting and automation in OS X: AppleScript and Bash script. The latter is of course a direct consequence of OS X using the Unix "bash" shell, while the former is Apple's own innovation. The syntaxes are clearly very different, with AppleScript having a pseudo-natural-language style.

My question is, are AppleScript and Bash script commonly used (indeed, should they be used) for disparate tasks and purposes on OS X. I am vaguely cognizant of the vast array of tasks I can accomplish with Bash script, though of course the code for some more advanced ones can quickly become convoluted. Do these two languages have overlapping or near-identical usage cases, despite Apple's later invention of AppleScript -- or are they intended to be used with different scenarios in mind? A high-level overview as well as some specific examples would be appreciated.


Both shell- and AppleScripts can be used just about anywhere either is appropriate.

AppleScripts work better when talking to apps and user-level system facilities. (For instance, you can say tell app "iTunes" to play and it will, or tell app "Finder" to open the first file of the second window.)

Shell scripts (you referred to bash) work better when talking to low-level system objects and Unixy stuff.

They're both interoperable: you can call a shell command from AppleScript with do shell script "command here" and you can call AppleScript code from the shell with osascript -e "AppleScript". Further, there's quite a bit of overlap between the two (especially when it comes to file system operations).

There are certainly more resources for learning shell scripting online than AppleScript and it doesn't help that AppleScript has a reputation for being a "read-only" language (i.e., harder to write than read).

In any case, the most modern way to do scripting is with Automator, where you can use either shell- or AppleScripting (or both) along with pre-built modules from various apps you have installed.


While either scripting context can do anything the other can (because shell scripts can call /usr/bin/osascript to invoke an AppleScript, and AppleScript has the do shell script command), there are indeed contexts for which one is better suited than the other.

Both scripting languages are "glue" languages -- they can do minimal things themselves, and instead accomplish most of their tasks by invoking the capabilities of other programs. Shell scripts make use of Unix pipes, while AppleScript has the tell application syntax.

Where they diverge is that AppleScripts directly communicate with scriptable applications (almost always GUI applications), while shell scripts communicate primarily with command-line programs (some of which can invoke graphic user interfaces, but many do not).

When it comes to file management tasks, both approaches can work. One can Tell Application Finder to copy files, or run the cp command in a shell script. So why use one over the other? Certainly some script writers are more familiar with one language than the other, and thus will prefer to use that tool. But a better consideration is of the user of the script. AppleScript can often be invoked from the Mac GUI: double-clicking an AppleScript application, or dropping files onto one, or using an AppleScript menu either system-wide or within a particular program. Programs like Mail can be set up to run an AppleScript on incoming messages to filter them. For users accustomed to using Macs in a "Mac-like" way (i.e. from the GUI), AppleScripts are often more accessible.

Shell scripts often (but not always) live in the Terminal. If one is already using Terminal, running a shell script there can be more convenient than invoking an AppleScript. On the other hand, many users are off-put by having to type commands in a Terminal window, or even clicking a script file that opens a Terminal window to do its magic. I personally find the syntax of shell scripts more readable than that of AppleScripts, but I suspect I'm in a minority on that one. Shell scripts are familiar to users of many different Unix-like systems.

In any event, both are powerful tools for gaining better control over your Mac.