I’m not a coder – never have been. For some reason my brain just isn’t wired that way. But that doesn’t stop me from trying.
I’ve tried my hand at everything from QBasic to PHP with varying degrees of success. However, ever since joining the Mac community I have heard rumors of a lean, mean scripting machine called “AppleScript” that promised to make life with my Mac a little slice of heaven… or Candyland – I can’t remember which.
In any case, I decided to try my hand at this little language they call AppleScript – and take you along for the ride. So if you’re like me and have heard of the infamous AppleScript but never known where to begin maybe I can help.
So What is AppleScript?
AppleScript is a scripting language that allows you to control your Mac without using the keyboard or mouse. Instead, you can write a few lines of mostly plain-English code using Apple’s free Script Editor to access and manipulate many popular applications and even the Mac OS itself. Think of it as the poor-man’s way of writing your own little program. You can run your scripts straight from the Script Editor, save them as a stand-alone “script application” to be run later, or you can even save them as script files to be used from within your favorite Mac programs.
How Does it Work?
In a nutshell, AppleScript works by using and manipulating objects. What’s an object you ask? An object is an item – such as a file or folder, or even a paragraph within a document – that can respond to commands. As you might begin to see, there are a LOT of objects out there that can be commanded to do things – which is just one reason that AppleScript is such a powerful, yet often misunderstood tool.
Each AppleScript is written as a series of statements that contain instructions for AppleScript to perform. These statements can be fairly complex with nested instructions and control statements, but your first AppleScript can contain a statement as simple as: “tell application “Finder” to close the front window.” It really is that simple.
Your First AppleScript
If you’re like me though, you probably need to see AppleScript in action to believe it. So go ahead right now and open up Script Editor (Applications > AppleScript > Script Editor). You’ll see a window similar to the one below.
In the upper window, called the script pane, simple type in
tell application “Finder” to open the startup disk
Now press Run and watch AppleScript work its magic!
Want to try another? Try running these other simple AppleScripts:
activate application “Finder”
tell application "Finder" to count each folder in startup disk
tell application "Finder" to open trash
Hopefully you now see that AppleScript isn’t as scary as I originally feared. Within about 30 seconds you have now written your very own AppleScript. Sure it doesn’t do much – but you can start to see that even plain English statements can be turned into powerful tools to improve your productivity, harness the hidden powers of your applications, and create cool scripts to show off to your PC buddies. Regardless of how you use it, AppleScript should no longer be ignored.
Be sure to check back with The Apple Blog for part two of “My First Apple Script” where we kick it up a notch and create a few useful scripts.