MacOS – AppleScript vs. Bash script

applescriptbashmacos

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.

Best Answer

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.