10 AppleScripts to make you love your Mac (even more)

Apple's highly useful scripting language is one of the Mac's hidden gems

Why, when people were trying to get me to switch from Windows to a Mac, did no one tell me about AppleScript?

Sure, a stable OS with Unix shell access and a sophisticated UI are nice. But a scripting language that lets me automate tedious tasks and hike my creativity-to-boredom ratio? How come I never heard about that?

"It's the hidden secret," said Sal Soghoian, co-author of Apple Training Series: AppleScript 1-2-3 and Apple's product manager for automation technology. "Apple never talks about it."

AppleScript's appeal is that it can control both your operating system and your applications, easily passing information among them. Soghoian estimates that four out of five "top-tier" Apple customers use AppleScript for serious automation -- his examples include The New York Times generating daily stock charts and software developers testing applications. Even Microsoft uses it, he says (for work on developing Office for Macintosh).

But AppleScript is also well suited for the desktop. You can use it to set your system to boot up with certain apps open in a particular way, right down to the size, location and content of each window. You can batch process files, rename and resize multiple images, or fetch Web pages and manipulate the results. You can connect to network servers and even create a simple database.

After a few months on a Mac, I wrote a script that copies data from a weekly report I get as a PDF, and formats it for insertion into a spreadsheet. This saves a boatload of tedious cutting and pasting. Another script pulls data out of our Web analytics tool and formats it for our home page "popular right now" box. Another one reads a text document and inserts the article information into proper fields of our content management system, saving more cut-and-paste operations.

It's also extremely easy to share AppleScripts with colleagues who use Macs -- a bit more elegant than, say, trying to share Perl scripts with Windows users who don't have Perl (and the required add-on modules) already installed.

While other languages offer some similar capabilities to AppleScript, "there's simply nothing like it in Windows," declares David Pogue in his book Switching to the Mac: The Missing Manual, Leopard Edition. "It's a programming language that's both very simple and very powerful, because it lets Mac programs send instructions or data to each other." And it does so using commands that are closer to plain English than most other scripting options.

A few caveats

AppleScript's use of (relatively) natural language for its commands is a mixed blessing. While it's popular among non-IT power users, even Soghoian admits that the syntax can frustrate those who regularly work in a language like Perl or Java. I lost count of how many errors I got because I'd write something like "x = 4" instead of the AppleScript-approved "set x to 4." And I don't program full time.

Although I've become an AppleScript enthusiast, I will caution that "natural language" doesn't always mean "intuitive and easy." While there are usually multiple ways to express something in AppleScript, quite a number of other syntaxes that might make sense won't work in a script. The "nouns" and "verbs" to script specific applications can be hard to ferret out and apply properly, even after perusing the appropriate app "dictionary" (a listing of that application's scriptable objects and methods). So some patience (and checking out others' code) is needed.

Fortunately, though, some highly scriptable applications allow you to record activities in the AppleScript editor, very much like a macro recorder which translates your actions into (editable) AppleScript commands. This was very handy when I couldn't figure out on my own the proper AppleScript for TextWrangler's search and replace. For example, I use this AppleScript snippet to delete all percentages in a report that are formatted with a space followed by one or two digits followed by a period followed by one digit followed by a percent sign -- as in 5.3% or 23.8%:

tell application "TextWrangler" to replace "\\s\\d{1,2}\\.\\d\\%" using "" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

A final warning: AppleScript is not particularly well suited for a lot of text manipulation. There's no built-in support for regular expressions; there's not even a simple search-and-replace function as part of core AppleScript.

The good news here is that you can get some decent support for regular expressions by installing a free add-on from Satimage or, for smaller amounts of text, dumping it into an app like TextWrangler, which has such support. AppleScript can also incorporate more powerful commands from Perl, Ruby, Unix or anything else that can be accessed via a shell script, and then assign the results to a variable. For more on that, check out Apple's technical note called "do shell script in AppleScript."

Despite its drawbacks -- and what language doesn't have any? -- I've found AppleScript to be a great tool for parsing drudgework out of my workday.

And now, without further ado, here's a sampling of cool AppleScripts out there that just might make you love your Mac even more.

Join the PC World newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection

Tags software developmentunixAppleapplescriptsnow leopardWindowsMac OS X

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Sharon Machlis

Show Comments

Most Popular Reviews

Latest Articles


PCW Evaluation Team

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?