5.2. Displaying FoldersWhen you're working on a bunch of related documents at once, you might want to jump quickly to their folder in the Finder. Normally, of course, you'd switch to the Finder and navigate through your hard drive to get to the correct folder. Or perhaps, if you're a power user, you've already put the folder in the Finder's Sidebar for easy access. Either way, though, you have to switch to the Finder and open a new window, which is a massive waste of time. Why go through all those steps when you can get AppleScript to do it for you? Using AppleScript, you can save a folder-opening script as an application (Section 2.2.2) and place the script on the Dock for easy access. From then on, all you'll need to do is click the script's icon in the Dock, and a Finder window pops open and takes you right to the folder you want. "But wait," you say, "I could just put the folder's icon on the Dock, no script required." You are, of course, correctand your method is what most people use for accessing commonly used folders. The trouble is, when you click a folder's icon on the Dock, you never know where the folder's window will open onscreen, or whether it'll be in List, Column, or Icon view. Plus, a folder icon on the Dock can open only one specific folder, whereas a script can open multiple folders at oncelike your Music and Pictures foldersas shown the following example: tell application "Finder" activate open the folder "Users:yourUsername:Music" open the folder "Users:yourUsername:Pictures" end tell Again, just save this script as an application (Section 2.2.2), and then drag the script's icon to your Dock. From then on, you'll be just one click away from opening two folders at once.
5.2.1. Opening a Folder with AppleScript (Reprise)As you've seen, there's more than one way to open a folder from AppleScript. If you want to open your Applications folder, for example, you'd have five separate choices: tell application "Finder" activate make new Finder window to alias "Macintosh HD:Applications:" --Option 1 make new Finder window to POSIX file "/Applications/" --Option 2 open alias "Macintosh HD:Applications:" --Option 3 open POSIX file "/Applications/" --Option 4 open the folder "Applications" of the startup disk --Option 5 end tell If you don't have any Finder windows open and you use one of these commands, the same thing would happen: a new Finder window would appear, taking you right to the Applications folder.
However, if you already have your Applications folder open, the commands behave differently:
The difference between these approaches is pretty small, of course, but it's important to understand: the make command always creates a new copy of something (in this case, a window), while the open command opens a new copy only if one doesn't already exist. 5.2.2. Changing a Finder Window's ViewWhy stop with just opening a folder when you can change the Finder window's view, too? If you've been using Mac OS X for more than a few days, you probably already know that the Finder has three viewing options (available at the top of the View menu), each of which provides a different, potentially timesaving way of looking at your files:
But the fun doesn't stop there; each Finder view also has its own options. For example, you can change a window's background color from the default white to some other coloror post a picture behind a Finder window. Simply open the Finder's dictionary (Section 3.2.2) and navigate to the Finder window entry (Figure 5-2). As you can see, there are several useful properties you can set for Finder windows. If you want your script to automatically open the Applications folder in Column view, for example, you could modify your script like this: tell application "Finder" activate open the folder "Applications" of the startup disk set the current view of the front Finder window to column view end tell Now when you run your script, the Applications folder comes to the front and then quickly switches into Column view.
5.2.3. More Finder Window SettingsAs you can see from the Finder's dictionary, there are a good number of extra properties you can set for Finder windows. You might have noticed with some puzzlement, however, that there's a big bold <Inheritance> label inside the entry for Finder window. Your first instinct might be to assume that this property is off-limits to youafter all, it's got the [r/o] label, which usually means that you can't change the setting. As it turns out, however, inheritance is a powerful tool in AppleScript that puts even more control at your fingertips. When you see the <Inheritance> label in a dictionary, look at the word immediately to its right. In the entry for Finder window, for instance, you'll see window next to <Inheritance>. That means that a Finder window, along with all its own properties, also has all the properties of a regular, everyday AppleScript window. So what's that mean to you when you're up late at night writing scripts? It tells you to look in the dictionary entry for window in addition to the entry for Finder window (Figure 5-3), essentially doubling the number of commands you can send to a Finder window. Armed with this information, you can add an extra command to your script: tell application "Finder" activate open the folder "Applications" of the startup disk set the current view of the front Finder window to column view --Minimize the window to the Dock. set the collapsed of the front Finder window to true end tell
5.2.4. Working with More than One WindowThe current script is great for showing your Applications folder, but it won't save you that much time; you can always just click once on the desktop and use a keyboard shortcut (Shift--A) to launch the Applications folder instead. You really start saving time when your script opens more than one folder. That way, you can have a quick way to view your Applications, Documents, Music, and Movies folders, for exampleall with a single click.
One approach to opening multiple folders from your script is to simply copy and paste the existing commands repeatedly. (Then you'd insert the names of the folders as appropriate.) Using this method, your final script would look something like this: tell application "Finder" activate open the folder "Applications" of the startup disk set the current view of the front Finder window to column view set the collapsed of the front Finder window to true open the folder "Documents" of home set the current view of the front Finder window to column view set the collapsed of the front Finder window to true open the folder "Music" of home set the current view of the front Finder window to column view set the collapsed of the front Finder window to true open the folder "Movies" of home set the current view of the front Finder window to column view set the collapsed of the front Finder window to true end tell
This kind of approach, unfortunately, has several downsides:
Luckily, there's a solution to these problems: breaking the redundant commands into a separate portion of your script, known as a subroutine. 5.2.5. SubroutinesIn AppleScript (and in other programming languages), a subroutine is a section of code that's meant to be used over and over again. Rather than having to retype a big block of code, a subroutine lets you write that code just once, assign a name to the code, and then simply use the subroutine's name whenever you want to run the corresponding code. Think of it like using a kitchen appliance: no matter what time of day it is, you can expect the "coffeemaker" appliance to behave the same way. Similarly, no matter what part of your script you run a subroutine from, you can expect the subroutine to run the exact same lines of code. You can do anything you want in a subroutine: tally the points from a sports game, connect to a Web site, or anything else you're likely to do more than once in your script. (And incidentally, subroutines are sometimes called handlers, too.) Now that you know what subroutines can do, it's time to put them to use in simplifying your Finder window script. 5.2.5.1 Defining a subroutineIn general, a subroutine looks something like this: on subroutineName( <any variables being passed into the subroutine> ) --Any commands you want in the subroutine go here end subroutineName You'll notice that, unlike repeat, if, and tell statements, subroutines begin with the keyword on. That's your way of telling AppleScript, "Hey! There's a subroutine here, and whenever I refer to this name, please run the lines of code that follow."
5.2.5.2 Running a subroutineCalling a subroutine from your script couldn't be easier. (Calling is just geek-speak for "running the code contained in a subroutine.") You simply type the name of the subroutine, followed by parentheses, like this: subroutineName( <any variables you want to pass into the subroutine> ) On the other hand, if your code is inside a tell statement, you have to preface your subroutine with the word my. That's your way of telling AppleScript, "I know I'm targeting a particular program with my script, but take a break for a second and run my personal subroutine, will ya?" 5.2.5.3 Variables in subroutinesWhen defining a subroutine, you don't have to put anything between the parentheses on the subroutine's first line. If you do choose to put variable names there, however, you'll need to use the same number of values when you call the subroutine from your script. In other words, if you defined your subroutine like this: on displayGreater(a, b) --Note that there are two variables if a > b then set theResult to a else set theResult to b end if display dialog "The greater number is: " & theResult end displayGreater you'd have to run the subroutine from your code like this: displayGreater(10, 88) --You must provide two values On the other hand, your subroutines don't have to accept variables at all. AppleScript is perfectly happy to run a subroutine defined like this, for example: on displayPi( ) display dialog pi end displayPi In that case, since the subroutine doesn't expect any values (indicated by the lack of variables between its parentheses), you'd run this subroutine with this simple command: displayPi( ) --Display a dialog box showing pi
5.2.5.4 Writing the appropriate subroutineNow that you know how to write and call subroutines, you can eliminate the redundant portions of your Finder-window script. First, add this subroutine to the bottom of that script: on columnAndMinimize( ) tell application "Finder" set the current view of the front Finder window to column view set the collapsed of the front Finder window to true end tell end columnAndMinimize In writing this subroutine, you've isolated your script's redundant code. Now you can erase your script's redundant code by calling the columnAndMinimize subroutine as follows: tell application "Finder" activate open the folder "Applications" of the startup disk my columnAndMinimize( ) open the folder "Documents" of home my columnAndMinimize( ) open the folder "Music" of home my columnAndMinimize( ) open the folder "Movies" of home my columnAndMinimize( ) end tell --Here's the subroutine: on columnAndMinimize ( ) tell application "Finder" set the current view of the front Finder window to column view set the collapsed of the front Finder window to true end tell end columnAndMinimize
Now, with this final script, you've not only saved several lines of code, but you've also made it easier to change your script's behavior in the future. If you ever want to modify the code now, it's simply a matter of modifying the subroutine once, rather than modifying four separate lines. Here are a few possible tweaks:
|