Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
    
If you have tasks that relate to files and folders buy "Hazel", it's a really powerful rule based tool that is very useful. It can also kick shell scripts and Applescripts based on rules. –  Stuart Woodward Jun 7 '12 at 22:23
add comment

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
    
Thanks; that's a very informative overview. Do you have any recommendations for good learning resources on AppleScript perchance? (Bash script too, although I could find those quite easily myself probably.) –  Noldorin Jun 1 '12 at 15:00
1  
AppleScript has a (somewhat earned) reputation for being read-only; Shell scripts sometimes lean toward the "write-only" end of the spectrum. –  Daniel Lawson Jun 1 '12 at 15:18
    
@Noldorin I bought a book to learn AppleScript, but I tend to use Google first and the book's index second when I'm trying to remember the syntax of a particular command. –  CajunLuke Jun 1 '12 at 15:59
    
@CajunLuke: Ah, fair enough. How did you learn AppleScript for the first time though?? –  Noldorin Jun 1 '12 at 16:17
    
@Noldorin Pretty much trial and error and Google. Some things are hard to Google for, so that's why I bought the book. –  CajunLuke Jun 1 '12 at 16:22
show 5 more comments

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.

share|improve this answer
    
Thank you; this certainly clarifies the matter and confirms some of my suspicions. –  Noldorin Jun 1 '12 at 15:01
add comment

The other answers give great high-level overviews of the intention behind these two scripting techniques. Maybe I'll just give you examples of how I use them.

With Applescript, I've made processing rules for Mail, connected BBEdit to R for statistical processing, created complex Folder Actions, and done a massive time-based reorganization of my photos in iPhoto and Aperture. I've also created mini-apps with buttons and text displays. Basically, this is automatic manipulation of complex apps with GUIs.

With Bash scripting, I've created automatic image processing routines with Imagemagick, PDF processing routines with Ghostscript, time and date routines, and lots of text processing with Bash, sed/awk, and Perl. Unix utilities tend to be lean, fast, and flexible, and Bash is a great way to combine them into very powerful, easy tools.

I do sometimes call Bash scripts from within an Applescript, usually for text processing purposes (GREP) or for light website fetching (curl). I almost always use this technique to obtain a bit of data or a variable to use further in my Applescript.

share|improve this answer
    
Ah, these specific examples are rather helpful. Thanks for this. Do you have any suggested learning resources for AppleScript by the way? For a complete beginner. (I have used a little Bash script in the past and can probably relearn it myself.) –  Noldorin Jun 1 '12 at 15:03
1  
Yes, the book I benefitted most from was Matt Neuberg's "AppleScript: The Definitive Guide." Apple's developer documentation is ok, and worth consulting for reference, but Neuberg's book quite honestly assesses both the strengths and weaknesses of the language. You will find that Applescript's insistence on "natural" grammar isn't very natural, and in many cases will be counterintuitive. Neuberg clarifies when that happens and why. –  Ash Jun 2 '12 at 14:17
    
That's exactly the impression I got from initially looking at the language. This half-way house between machine and natural languages seems rather awkward. Cheers for the recommendation! –  Noldorin Jun 3 '12 at 18:48
add comment

It may be oversimplification, but shell scripting is for combining existing programs to perform new tasks whereas AppleScript is to control (mostly GUI) applications remotely. Classical Unix programs typically read some input and produce some output and shell scripting lets you combine them. AppleScript is to automate GUI applications.

share|improve this answer
    
Thanks; this confirms my initial inclinations. –  Noldorin Jun 1 '12 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.