Chapter 5 - Controlling Files
Most people have a love-hate relationship with their computer. Sure, your Mac is a great tool for when you want to edit images or video, send and receive email, or play Halo. But your computer also serves as a digital file cabinet: a place where you can create and store files, move them around, organize them in folders, trash them when theyre no longer needed, and copy them to another disk or computer on the fly.
Files, of course, are nothing more than individual packages of information that you keep on your hard drive. But for all the filing tasks they perform, most computer users tend to handle files manually: drag this file here, create a new folder there, and so on. After a while, these mundane tasks are what make people start to hate their computers.
If youre sick of dealing with your files one at a timeand taking up half your day in the processtheres no better tool in your arsenal than AppleScript. By commanding the Finder, AppleScript lets you:
Move all the files off your desktop in one fell swoop (page 90)
Back up an important folder to a separate hard drive, just in case your computer dies (page 93)
Rename all the files in a folderwithout having to type their new names individually (page 113)
For these jobs and more, AppleScript can save you annoyance, tedium, andmost of alltime.
Note: The example scripts from this chapter can be found on the AppleScript Examples CD (see page 24 for instructions).
File Path Boot Camp
The one thing AppleScript cant save you from is the fact that files are essentially geeky things. Mac OS Xs Unix heritage, while great news for programmers, also means that old Mac fans have to adapt to a few new file conventionshow to name files, what programs to open them with, and so on. Therefore, before you jump headfirst into controlling files with AppleScript, there are a few things you should know:
In Mac OS X, files should always have a file extension. A file extension is a short abbreviation added after a period in a file name. Microsoft Word files, for example, end in .doc, while sound files often end in .mp3 or .aiff.
To Mac OS X (and Windows, for that matter) a file extension reveals what kind of information a file holds. In many cases, a file extension also tells Mac OS X which program should open a file: .doc files open in Microsoft Word, while .psd files open in Photoshop. (Of course, certain types of files can open in several different programs; .jpg files, for example, can open in just about any image-viewing program on the planet.)
Note: As a general rule, folders should not have file extensions. The exceptions are bundleslittle folders that masquerade as files (page 34), like the .key files that Keynote produces.
To see whats inside in a bundle, Control-click the bundle in the Finder and select Show Package Contents from the shortcut menu. In the new window that appears, you can sift through the files that comprise the package, discovering, for example, that Keynote "files" are actually made up of dozens of smaller files.
Although certain programs dont require file extensions, its still a good idea to use them. That way, if you ever need to send a file to a Windows user, you wont get back an angry email asking you to resend the file with an extension so your recipient can actually open it.
A path is a string that tells you how to get to a certain file or folder. Each item in path is separated by a forward-slash (/) in Mac OS Xa by-product of your computers Unix heritage. That means the path to your Home - Desktop folder would be /Users/yourUsername/Desktop/, while the path to your copy of Text-Edit would be /Applications/TextEdit.app.
When you want to play with a path in AppleScript, you can use special type of information called a POSIX file. (POSIX is nerd lingo for "portable operating system interface," which basically means that file paths can be used anywhere, on any computer that supports the POSIX standard. To learn more about POSIX, you can read up on it online at satimage.fr/ software/en/file_paths.html.) To get the path to your Desktop folder, for instance, youd write the following, replacing yourUsername with your actual one-word username:
POSIX file "/Users/yourUsername/Desktop/"
Still, youll find that most commands (like choose file, for presenting an Open dialog box [page 97]) use the alias type to refer to files. The alias format separates each folder in a path with a colon, rather than a forward slash. To get the alias to your Desktop folder, for example, youd write this:
Note: Of course, you should replace yourUsername with your actual username. If you dont know what your username is, you can look it up in System Preferences - Accounts; youll find your username in the Short Name field.
You can open any file or folder with the open command directed at the Finder. For example, to open Library - Desktop Pictures - Aqua Blue.jpg (the image that appears behind Mac OS Xs login dialog box), you could write:
tell application "Finder"
activate --Bring the Finder forward
open POSIX file "/Library/Desktop Pictures/Aqua Blue.jpg"
Note: Youll notice a couple of oddities when you run this AppleScript. First off, the open POSIX file statement gets changed to open file. Then, all the forward slashes are converted to colons, and AppleScript inserts the name of your hard drive at the beginning of the path string. None of these changes affect what your code actually does; AppleScript just makes these changes so it understands what youre asking it to do.