Automate Your Folders

How many times a day do you open and close folders, drag files in and out of folder windows, or move and resize the windows on your desktop? Consider this: every time you perform one of those mundane tasks, you could be tapping into the timesaving power of folder actions–a feature introduced with Mac OS 8.5.

Folder actions let you attach specific AppleScripts to your folders, training them to perform tasks whenever they're opened, closed, moved, and so on. Imagine creating folders smart enough to open just where you want them or to clean up after themselves, closing their own subfolders. While hard-core AppleScript pros can have a field day with the stuff, you don't need to have a programming language under your belt to pull this stunt off.

The first trick is to find the Mac's folder-action features–you won't find a single menu command in the Finder, and there's nothing in Apple's Script Editor that indicates you can attach a script to a folder. Even the ten sample scripts Apple provides to get you started are tucked away inside a folder buried two levels deep within the System Folder. We'll show you a couple of additional tricks involved in bringing folder actions to life, along with a sampling of the cool things you can make your Mac do. For most of the commands we describe, you can use Script Editor's built-in Record feature to create scripts by simply recording your actions in the Finder (see Secrets, November 1999).


To create a folder action, control-click on any folder (or disk icon) to which you want to attach an action. At the very bottom of the contextual menu that pops up, you'll see a command called Attach A Folder Action. Choose this command, then select the AppleScript you want to use. Once you've selected a script, a contextual pop-up menu provides commands for editing and removing folder actions (see "Finding Folder Actions"). A tiny AppleScript badge appears on the lower left corner of a folder's icon once you've attached a script to it.

Not every AppleScript is eligible for use as a folder action, though. Folder actions work only with scripts saved out of Script Editor as compiled scripts, not with those saved as applications. If you've saved a script as an application, open it in Script Editor and resave it in the compiled-script format by selecting that option from the Kind pop-up menu in the Save As dialog box.

Furthermore, saving an AppleScript as a compiled script with the Attach A Folder Action command won't work if your script doesn't start and end with certain action-specific commands (see "Magic Words"). The required scripting isn't terribly complicated, but these little snippets of code are essential for your folder actions to work. The examples that follow demonstrate the words you need to have in place.

Finally, folder actions get triggered only when the folders to which you've attached them are open. In other words, you can't trigger a folder action by dropping a file into a closed folder.


If you don't like it when other users mess with your carefully organized on-screen workspace, here's a cool trick: create a folder window that always springs back into place–at the exact size and position you prefer–whenever anyone attempts to move it.

First, record an AppleScript that specifies the position and size of the window you want to control. Open that window; launch Script Editor; click on the Record button; and while recording, position and size the window. When you're done, click on the Stop button. The script that results will look something like this:

	tell application "Finder"
	activate
	set position of container window of folder
	"Your Folder" of startup disk to {405, 105}
	set size of container window of folder
	"Your Folder" of startup disk to {409, 279}
	end tell
	

Now, to turn it into a folder action, add the following lines to the beginning and end of the script. It will look like this:

	on moving folder window for this_folder from original_bounds
	tell application "Finder"
	activate
	set position of container window of folder
	"Your Folder" of startup disk to {405, 105}
	set size of container window of folder
	"Your Folder" of startup disk to {409, 279}
	end tell
	end moving folder window for
	
Finding Folder Actions   Control-clicking on a folder reveals all the commands needed to attach, remove, and edit folder-action scripts. Notice the folder icon's small AppleScript badge, which indicates that the folder has a script attached to it.

The first and last lines of code perform the enclosed tasks any time someone tries to move or resize the window. Save this code as a compiled script, and then attach it to a folder using the steps outlined previously. From now on, that folder's window will instantly jump back into the position you've set whenever anyone tries to move it.


Similarly, you can create smart folders that always pop open exactly as you specify. Suppose you want a project folder that always opens at the left side of your screen, with items sorted by date. Again, you'll first record a script in Script Editor: click on Record, open the window, put it where you want it, and set the list view according to your preferences. The script will record all your actions. When you're finished, click on Stop.

To transform this script into a folder action, you'll add two lines to the recorded script, one at the top and one at the bottom:

	on opening folder this_folder
	tell application "Finder"
	activate
	set size of container window
	of folder "Projects" of startup disk to {293, 275}
	set position of container window
	of folder "Projects" of startup disk to {12, 478}
	end tell
	end opening folder
	

The two opening folder commands tell your Mac to run this script whenever a user opens the folder to which it's attached. This guarantees that the folder will always open in the same spot, with the same settings.

Magic Words
Folder-action scripts must start and end with commands specifying the particular action that triggers them. Here are the five different commands for making folder-action-savvy scripts. As is always the case with scripting, exact wording and syntax counts when you're including these commands in your scripts.
To Create Folder Actions Triggered by: Start Your Script with This Command: End Your Script with This Command:
Opening a folder on opening folder this_folder end opening_folder
Closing a folder on closing folder window for this_folder end closing folder window for
Adding items to a folder on adding folder items to this_folder end adding folder items to
Removing items from a folder on removing folder items from this_folder end removing folder items from
Moving or resizing an open folder window on moving folder window for this_folder from original_bounds end moving folder window for

If you want to dig deeper into folder actions, check out the sample scripts that Apple keeps nestled away in the Scripts folder inside your System Folder. (There's no special reason to store these scripts in the System Folder, by the way. You can place saved folder-action scripts in any convenient location on your system.) Though you may not find the samples that compelling, they provide a good model for developing more-sophisticated folder actions.

JOSEPH SCHORR, a Macworld writer since 1991, enjoys tinkering with AppleScript, though he is the first to admit that, at heart, he's a severely code-writing-impaired journalist.

July, 2000 page: 103

1 2 Page 1
Shop Tech Products at Amazon