Section 11.3. Running Your Own Actions


11.3. Running Your Own Actions

You can use any of the folder actions that come with Mac OS X, or you can tweak them to your liking (Section 11.2.3.1). But after a while, you'll probably want to write your own folder actions from scratch, to truly customize what your folders do.

Luckily, writing a folder action isn't much harder than writing a normal AppleScript. As usual, you open Script Editor, create a new document (by choosing File New, or pressing -N), and enter your script's code in the big text box. The only difference, of course, is that you have to surround folder action commands in a special AppleScript handler, so the Finder recognizes your script as a bona fide folder action.

11.3.1. When a Folder Opens

The easiest kind of folder action to create is one that runs when you double-click a folder in the Finderjust like the Show Comments in Dialog script from Section 11.2.2. The following script, for example, automatically displays whatever folder it's attached to in Column view, whenever you open that folder in the Finder:

--Part 1: on opening folder theFolder     --Part 2:     tell application "Finder"         set windowName to the name of theFolder         --Part 3         set the current view of Finder window windowName to column view     end tell end opening folder

Here's how the folder action works:

  • Part 1, and the on opening folder handler in particular, is what lets the Finder know to run your script when you open a folder (as opposed to closing or adding icons to the folder, for example). The variable name following the handlerin this case, theFolderstores the path of the folder you just opened.

  • Part 2 gets the name of the folder this script is attached to, using the name property. With the folder's name in hand, you can command the Finder to adjust the view for that folder's window.

  • Part 3 locates the window that goes with the folder you just opened, and displays the folder's file list in Column view. To round out the script, this part also includes the appropriate end statements.

At this point, you've got a series of clever commandsbut nothing more. If you click the Run button in Script Editor, for example, none of the commands will run because they're all contained within an opening folder handler. A folder action runs only when it's attached to a folderand even then, only when something happens to that folder (such as double-clicking it).

To make any use of your script (besides admiring Script Editor's code coloring), you have to follow these steps:

  1. Choose File Save (-S), and give your folder action a descriptive name. Don't click Save yet, though.

    Typical names for folder actions incorporate what your action's triggered by (in this case, opening a folder) and a brief description of what the script does when it runs (here, displaying a window in Column view.) A good name for this script, therefore, might be open - display in column view.scpt.

    Make sure you choose Script for the File Format, since that's the format the Finder expects for all folder actions.

  2. For the script's location, navigate to Library Scripts Folder Action Scripts.

    It's very important that you deposit your script in the Folder Action Scripts subfolder. Now you can click Save (Figure 11-5).

  3. In the Finder, select any folder or disk, and attach your new script to it (Section 11.2.1).

    Now, whenever you double-click that folder, its window automatically adjusts to Column view for your navigating pleasure.

Attach this script to all your most often-used folders for even more Column view convenience.

Figure 11-5. The Save dialog box for your first homemade folder action. Once you click Save, you get a full-fledged folder action script, which you can attach to any folder you want.


11.3.2. When You Add Files to a Folder

Far more useful than running a script when you open a folder, however, is running a script when you add new items to a folder. That way, you can filter the incoming files as you see fitorganizing them by name, for instance, or forwarding them to another folder.

11.3.2.1 An example: Sending files to the Trash can

One of the biggest shocks to people coming from Mac OS 9 is the lack of a Trash can icon on the desktop. And even if you're not an old Mac hand, it probably comes as a surprise that you can't put a Trash can icon in the Finder's toolbar or Sidebar for easy access.

Of course, all those problems could be eliminated if you just had an alias of the Trash folder. Then, you could deposit your alias wherever you wanted, and when you dragged files there, they'd be sent straight to the Trash can.

Unfortunately, you can't create an alias of the Trash can the usual way (by choosing File Make Alias), because Apple cleverly buried the actual Trash folder deep in your Mac's invisible files. (Technically, you can access the actual Trash folder, with the info at www.macosxhints.com/article.php?story=20031222122301840, but it's not a lot of fun.) That's why the following script is so helpful: whatever folder you attach the script to instantly becomes a trash-forwarding machine.

--Part 1: on adding folder items to theFolder after receiving theItems     --Part 2:     repeat with currentItem in theItems         tell application "Finder"             --Part 3:             if the name of currentItem is not ".DS_Store" then                 --Part 4:                 delete currentItem             end if         end tell     end repeat end adding folder items to

Here's how the folder action works:

  • Part 1 (and the on adding folder items bit in particular) lets Mac OS X know your code should run whenever you add files to the script's associated folder. In the world of variables, the folder itself is stored in theFolder, and the list of files you dragged to the folder is stored in theItems.

  • Part 2 starts a repeat loop, setting currentItem to the next file in the list every time it runs. Therefore, by the time the script is finished, your folder action will have processed every file you dropped on the associated folder.

  • Part 3 checks to make sure the current item isn't named .DS_Storean invisible file stored in just about every Mac OS X folder. Deleting a .DS_Store file can bring up nasty error messages, so your script skips over such files. Otherwise, if the current file isn't named .DS_Store, the script proceeds to the next part.

  • Part 4 deletes the current item, forwarding it to the Trash can. (You hear a satisfying ka-clunk to let you know the deed's been done.)

Now simply save your script and attach it to a new, empty folder; this folder becomes the trash-forwarding machine. For total convenience, name your folder Trash, put it on the desktop, and drag a copy of the folder to your Finder's toolbar or Sidebar. You've just created a poser Trash folder, which sends all the files you drag there to the real Trash can.

If you're really into the details, you can even copy the Trash's icon to your new trash-forwarding folder. To do so, click the Trash in the Dock, choose File Get Info (-I), and select the icon in the Info window. Then choose Edit Copy (-C), select your new Trash folder, choose File Get Info, select its generic icon, and choose Edit Paste (-V).

11.3.2.2 Moving old desktop files

If you're old, nearsighted, or you just like to see images in all their glory, you may have increased the size of your desktop icons by choosing View Show View Options (-J) in the Finder. The trouble is, when your icons get bigger, you can fit fewer of them on your screen at once. And if you're the type who stashes hundreds or thousands of files on your desktopor if you just happen to have a small screenthis arrangement a recipe for stacked-icon soufflé.

Luckily, you can employ the help of folder actions in cleaning up your desktop. By attaching the following script to your desktop, any files beyond the number you specify will automatically get thrown into an Old Desktop folder:

--Part 1: on adding folder items to theFolder after receiving theItems     set maxItems to 5     --Part 2:     tell application "Finder"         if not (exists folder "Old Desktop" of home) then             make new folder at home with properties {name:"Old Desktop"}         end if         --Part 3:         set desktopFiles to every item of the desktop         --Part 4:         if (count desktopFiles) > maxItems then             move items (maxItems + 1) through (count desktopFiles) ¬                 of desktopFiles to folder "Old Desktop" of home         end if     end tell end adding folder items to

Here's how this script works its magic:

  • Part 1 sets the maxItems variable to the maximum number of icons you're willing to tolerate on the desktop. Specify any number you want.

  • Part 2 checks to see if you already have a Home Old Desktop folder. If you don't, the script creates one for you (this is where your superfluous desktop icons will be sent).

  • Part 3 gets a list of all the items on your desktopfiles, folders, and disks.

Your folder action runs after you've moved files to the desktop, so this list will also include the files you just moved.

Gem in the Rough
Folder Actions Setup

After you use folder actions for a while, you might lose track of which folders you've attached scripts to. And if you've attached more than one script to a single folder ([click here]), folder action relationships can be even harder to remember.

Luckily, Mac OS X comes with a tool specially designed for organizing, tracking, and editing your folder actions from one central location. It's called Folder Actions Setup, and you can find it in your Script Menu by selecting Script Menu Folder Actions Folder Actions Setup. Here are the key features to this powerful program:

  • To turn Folder Actions off for your entire Mac, turn off the Enable Folder Actions checkbox. In an instant, all the scripts you've attached to various folders on your Mac are disabled. The scripts aren't deleted, though; if you'd like, you can re-enable them later by simply turning this checkbox back on.

  • The left list shows every folder on your Mac that has a Folder Action attached. If a folder's name isn't enough to identify it (say, if you're not sure which Library folder you've attached a script to), click Show Folder to reveal the selected folder in the Finder.

  • If you'd like to detach all the scripts from a folder, select the folder in the left list and click the - button in the lower-left portion of the window. A dialog box asks you to confirm the deletion before proceeding.

  • To add a new folder to the left list, click the + button in the lower-left corner of the window. A standard Open sheet appears, allowing you to select the folder you'd like to attach scripts to. In the next dialog box, Folder Actions Setup lets you choose the actual scripts you want to attach to the folder; to attach more than one, -click each script individually.

  • Select a folder from the left pane to see a list of all its attached scripts in the right pane. To remove some of the attached scripts from that folder, select them and click the - button in the lower-middle of the window.

  • To attach a new script to the currently selected folder, click the + button in the lower-middle section of the window. A dialog box appears, letting you attach any of the currently available folder action scripts to the folder you chose.

  • To see the code that goes with a particular folder action, select the script from the right pane, and click Edit Script. This button opens the selected folder action script in Script Editor.

You can always launch Folder Actions Setup by double-clicking its icon in the Finder (found in Applications AppleScript), but that's the old-fashioned way. Instead, Control-click anything in the Finder and choose Configure Folder Actions from the shortcut menu. Or, if the Finder is hidden, open the Script Menu and choose Folder Actions Folder Actions Setup. No matter which method you use, Folder Actions Setup springs to life, letting you organize your sprawling collection of folder actions.


  • Part 4 checks whether your desktop is full, based on whether it has more items than you specified in part 1. If there are too many items, the script finds all the ones after the cutoff point and sends them to your Home Old Desktop folder. For example, if you had 15 icons on your desktop but decided in part 1 that there could be a maximum of 10, the last 5 icons would get moved to your Old Desktop folder when the script ran.

All you have to do now is save your script and attach it to the desktop (Control-click the desktop background and choose Attach a Folder Action). Now, when your desktop gets too full, don't fretjust sit back and relax as your Mac cleans it up for you!

For additional timesaving folder actions, visit http://www.apple.com/applescript/folderactions/05.html.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net