Section 11.2. Built-in Actions


11.2. Built-in Actions

To get you started on your folder actions journey, Apple included 13 predesigned scripts for you to attach to any folders you'd like. You can find these scripts in your Library Scripts Folder Action Scripts folder, and you can open them in Script Editor if you'd like to read their code.

Don't get confused between the Folder Action Scripts folder and the Folder Actions folder. Both are in your Library Scripts folder; the difference is that Folder Action Scripts contains actual folder actions you can attach to folders on your computer, whereas the Folder Actions folder contains AppleScripts for enabling, disabling, and otherwise manipulating folder actions on a system-wide level. (Also, only the Folder Actions folder appears in the global Script Menu.)

11.2.1. Attaching a Folder Action

If you're a control freak, you might want to use the add - new item alert script with your Documents folder. That script automatically shows you a dialog box when you add new files to the Documents folder, making it easy to keep an eye on your documents without impulsively switching to the Finder every 5 minutes. Here's how you'd pull off the folder-attaching trick:

  1. In the Finder, open your Home folder.

    You'll see your Documents folder inside. Whenever you want to attach an action to a folder (in this case, the Documents folder), you must first open that folder's parent folder (in this case, your Home folder).

    The Finder doesn't restrict you to attaching folder actions to just foldersyou can attach them to hard drives, CD-ROMs, disk images, and anything else that can contain a file. Note, however, that if you attach a folder action to a removable disk (such as a CD or DVD), Mac OS X will forget all about the action when you eject and reinsert the disk.

  2. Control-click the Documents folder (or right-click it, if you have a two-button mouse). Then choose "Attach a Folder Action" from the shortcut menu (Figure 11-1).

    An Open dialog box appears, taking you straight to the Library Scripts Folder Action Scripts folder.

    Figure 11-1. To attach a folder action to any folder on your Mac, Control-click the folder (in this case, Documents), and select "Attach a Folder Action" from the shortcut menu.


    You must Control-click the Documents folder itself; if you happen to keep your Documents folder in the Finder's Sidebar or in the Dock, you can't Control-click it there.

  3. Select "add - new item alert.scpt" (this should be right at the top of the Open dialog box), and click Choose to attach the script to the Documents folder.

    If you'd rather associate a different folder action with your Documents folder, simply select a different script.

You can associate more than one folder action with a single folder. That way, one script could run when the folder opens, and one could run when the folder closes, for example. Mac OS X would know which script to run by the handlers inside the scripts; for instance, a folder action script with an opening handler would run when you opened the folder, but a folder action script with an adding handler would run when you copied new files into the folder.

To attach multiple folder actions to a folder, simply Control-click the folder you have in mind, choose Attach a Folder Action from the shortcut menu, and select any additional scripts you want to attach from the dialog box.

Now whenever youor anyone on your networkadds a new item to your Documents folder, a dialog box appears on your screen to let you know (Figure 11-2).

If you're interested in keeping closer tabs on your network activity, attach the same script to your Home Public Drop Box folder. On a network, that's the only folder people can copy files to (unless they know your password, in which case they can copy files anywhere on your hard drive.)

Figure 11-2. If this dialog box appears out of nowhere, there's a good chance someone on your network just copied three files to your Documents folder. If you'd like to see the files in question, click Yes. Otherwise, click No or press Return to dismiss the dialog box.


11.2.2. The Included Folder Action Scripts

So far, you've played with one folder action scriptout of the 13 that come with your computer. That sort of record might be acceptable to a minor-league baseball player, but it's hardly a way for a power user to live. To satisfy your curiosity, therefore, here's a breakdown of the folder action scripts that come with your Mac:

  • add - new item alert is, as described on Section 11.2.1, a script to notify you whenever somebody moves files into the script's associated folder. This script's got just about as many uses as duct tape: attach it to the folder where you download files, and you'll see a dialog box whenever a download starts; attach the script to your Public folder, and you'll get a notification whenever someone on your network copies files to your Mac; or attach the script to your Applications folder, and you'll find out whenever an installer copies a new program there.

There's nothing to keep you from attaching the same folder action to multiple folders. For maximum convenience, then, attach the add - new item alert script to all three of the folders mentioned previously.

  • close - close sub-folders is a script for reducing window clutter. When attached to a folder, this script automatically closes any of that folder's subfolders when you close the attached folder itself.

    For example, say you attached this script to your Home folder and then opened up three separate windows: one for your Home folder, one for your Home Music folder, and one for your Home Pictures folder. When you closed your Home folder, the other two windows would close automatically, because they're subfolders of your Home folder. If you hadn't attached this script to your Home folder in the first place, you'd be left closing up the other two windows by hand.

  • convert - PostScript to PDF is mainly useful for graphics professionals and other geeks who deal with PostScript files (raw graphics and layout information) every day. The script converts any PostScript files you drop in its attached folder into PDFsfor sending off to a print shop, for example. The script then places the converted documents into a PDF Files subfolder, and moves the original PostScript documents to an Original Files subfolder.

    In other words, you're left with a fresh new PDF file, suitable for emailing to your friends, posting on a Web site, or printing out to post on your wall.

  • Image - Add Icon takes any image files you drop onto its associated folder (in JPEG, TIFF, or PNG format) and automatically changes the files' icons to display the images themselves. That scheme can save you a lot of time, since you can see at a glance what each image contains without actually opening it. For maximum convenience, attach this script to your desktop, so that any images you download from the Internet to your desktop automatically receive preview icons of their own.

This script automatically moves the icon-added images to an Original Files subfolder, which can be slightly confusing if you were expecting to find the images in the exact place you saved them.

Workaround Workshop
Using Folder Actions with Subfolders

If you spend a lot of time with folder actions, one thing will strike you as quite odd: actions work fine with the folder to which they're attached but don't work at all with any subfolders in them. That means that the script you attached to your Documents folder (Section 11.2.1) takes effect only when you drop files directly into your Home Documents folder; the folder action doesn't run at all when you drop files into your Home Documents Shopping Lists folder, for example.

Of course, there are some times when this one-folder-deep behavior is exactly what you want. If the Finder didn't have this safeguard in place, for instance, you'd run into problems if you attached an action to your hard drive, because the same action would trickle down to every folder on your Mac.

Still, there are plenty of circumstances when it would be better to have a folder action apply to all subfolders. If you wanted to see a dialog box whenever somebody added a file to your Home folder or any folder inside it, for example, an apply to all subfolders command would be quite helpful.

Unfortunately, there is no such command. To attach a folder action to subfolders, you must Control-click each subfolder one at a time, and select Attach a Folder Action from the shortcut menu.

The only relief you get from this painful process is that Mac OS X automatically shows you the Library Scripts Folder Action Scripts folder in each Open dialog box. That way, you don't have to locate the same script on your hard drive every time you want to attach the same folder action to a different subfolder.


  • Image - Duplicate as JPEG/PNG/TIFF are three scripts for converting image files into JPEG, PNG, or TIFF image formats. Simply drag an imageor a group of imagesonto the folder associated with one of these scripts, and watch in amazement as Mac OS X creates a new folder (called JPEG Images, PNG Images, or TIFF Images, as appropriate) for your converted images.

Unfortunately, due to a bug in the underlying graphics features of Mac OS X, you may not be able to convert EPS files using these scripts.

  • Image - Flip Horizontal/Vertical are two scripts for reversing your pictures horizontally or vertically. For example, if you had a picture of the front of an ambulance, you could drop it into a folder with the Flip Horizontal script attached, so you could read the word "Ambulance" from left to right. (In the U.S., most ambulances have the word "Ambulance" in reverse on their hoods, so the drivers in front can see that they have an ambulance behind them when they look in their rear-view mirror.)

To make your flipped images easier to find, Mac OS X moves them to a subfolder named, appropriately enough, Flipped Images.

Also note that these Flip scripts are only designed to work with TIFF, JPEG, and PNG images.

  • Image - Info to Comment is meant to take any information from a graphics fileits width, height, number of colors, and so onand automatically copy that information to the file's Comment field, which you can see by selecting the image in the Finder and choosing File Get Info. Then, whatever folder you attach this script to creates a Processed Images subfolder to hold all the newly commented pictures.

  • Image - Rotate Left/Right takes any images you drop into the associated folderat least, those images in TIFF, JPEG, or PNG formatand automatically rotates the images either left or right by 90 degrees (depending on which script you use). If you often take portrait-oriented pictures (where you rotated your camera 90 degrees to fit more of the image vertically than horizontally), this script can come in handy, since you won't have to rotate your head sideways to see the images as you originally intended.

The just-rotated pictures are placed in a Rotated Images folder, for your browsing pleasure.

  • open - show comments in dialog is a script for viewing a folder's Comments field ([click here]) without having to choose File Get Info. Whenever you double-click a folder to which this script is attached, a dialog box appears, telling you that folder's commentsand letting you change or delete them, if you so desire.

    Although it's not very obvious, you can also use this script to create a daily to-do list. For instance, after attaching this script to your Applications folder, you could enter a reminder in the folder's Comments fieldlike, say, "Remember to walk the dog." Then, whenever you double-clicked the Applications folder in the Finder, you'd see a dialog box reminding you of whatever you typed into the Comments field (Figure 11-3). It's a simple, free to-do reminder that shows up whenever you double-click that most useful of folders: Applications.

Figure 11-3. If you attached the open - show comments in dialog script to your Applications folder and then added a comment to the Applications folder, you'd see a dialog box like this whenever you opened the folder. Click Open Comments to edit the comments associated with the folder (to add a different reminder, for example). Click Clear Comments to delete the reminder entirely. Or, to leave the comment as is, simply click OK.


11.2.3. Viewing and Changing an Action's Code

Maybe you're curious how the PostScript to PDF script actually works. Or maybe you'd like to tweak the Show Comments in Dialog script, so the buttons in the dialog box have shorter names. Either way, it would sure be helpful if you could open your folder action scripts in Script Editor, so you could tweak the scripts to suit your particular needs. After all, your Mac does belong to you, so why settle for Apple's choices?

Luckily, you can tweak folder action scripts to your heart's content. Mac OS X gives you two ways to view (or edit) a folder action script's code in Script Editor:

  • Navigate to your Library Save to modify the folder action's behavior.

  • If the script you want to edit is already attached to a folder, Control-click the folder and choose the script from the Edit a Folder Action submenu. Script Editor springs to life, opening the script attached to that folder.

11.2.3.1 An example: shortening button names in a folder action's dialog box

As explained earlier, the Show Comments in Dialog script is a handy tool for reminding yourself to do chores whenever you double-click a folder. As shown in Figure 11-3, however, the buttons in the dialog box are absolutely enormous. Not only do they look ugly and un-Mac-like, the buttons also make the dialog box itself twice as wide as it has to be, obscuring windows underneath.

Luckily, there's an easy fix; since Mac OS X lets you modify the built-in folder actions, you simply have to locate the Show Comments in Dialog script and tweak the button sizes yourself. Here's how:

  1. In the Finder, open Library Scripts Folder Action Scripts, and double-click "open - show comments in dialog.scpt."

    Alternatively, Control-click a folder that already has the Show Comments in Dialog script attached, and choose the script's name from the Edit a Folder Action submenu.

  2. Edit the script as you see fit.

    For example, to get rid of the oversized buttons, edit this command:

    display dialog alert_message buttons {"Open Comments", ¬ "Clear Comments", "OK"} default button 3 giving ¬ up after dialog_timeout

    so it says this:

    display dialog alert_message buttons {"Open", ¬ "Clear", "OK"} default button 3 giving ¬ up after dialog_timeout

    Then edit these lines:

    if the user_choice is "Clear Comments" then set comment of this_folder to "" else if the user_choice is "Open Comments" then

    so they read like this:

    if the user_choice is "Clear" then set comment of this_folder to "" else if the user_choice is "Open" then

    Those simple tweaks are all it takes to shorten the button names.

  3. Save the script (File Save, or -S).

    Now, when you double-click any folder with this script attached, you'll see the much leaner dialog box shown in Figure 11-4.

Figure 11-4. Your new, smaller dialog box. In exchange for shorter button names, unfortunately, the buttons are a little harder to understand. In case you forget: Open takes the place of the Open Comments button (for editing the reminder that appears at the top of the dialog box). Along the same lines, Clear takes the place of Clear Comments (for deleting that reminder altogether).


11.2.4. Removing an Action

If you're getting tired of seeing the dialog box from Figure 11-4 whenever you double-click your Applications folder, it's time to turn off the folder action you attached back on Section 11.2.1. To remove the action, simply Control-click the Applications folder, and pick the script out of the Remove a Folder Action submenu. Your Applications folder now behaves as it would on a fresh new Macthat is, the folder doesn't display a dialog box when you double-click it.

Of course, if you want to remove a script attached to a folder besides your Applications folder, Control-click that other folder instead.

11.2.4.1 Removing all actions

If you'd like to turn off all folder actions at oncebecause the constant dialog boxes are starting to get to you, for exampleControl-click anywhere in the Finder and choose Disable Folder Actions.

If you decide to turn folder actions back on later (by Control-clicking in the Finder and choosing Enable Folder Actions), all the scripts you've previously set will be automatically reattached to the appropriate folders.

If you'd prefer not to switch to the Finder to disable all folder actions, you can simply open the Script Menu (Section 1.1) and choose the exact same menu command from the Folder Actions submenu.



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