WRITING FOLDER ACTION SCRIPTS

Folder Actions are scripts, saved as compiled script files, containing one or more of the Folder Actions handlers from the the Folder Actions suite in the Standard Additions dictionary.

These handlers correspond to each of the following manipulations that can be performed on an attached folder:
  • the attached folder is opened
  • the window of the attached folder is closed
  • the window of the attached folder is moved or resized
  • items are placed into the attached folder
  • items are removed from the attached folder
When one these actions is performed on a folder that has an attached Folder Action script, the Folder Actions architecture executes the attached script, passing to it any appropriate information about the attached folder and the items added to or removed from it.

Folder Action Examples
The following short script examples demonstrate the use of each of the five Folder Actions handlers found in the Standard Additions dictionary.

Once a script containing one or more of these handlers is saved as a script file, it can then be placed in the Folder Actions Scripts folder in the Scripts folder to be used as a folder action.

click to open script in Script Editor TIP: If you're using Mac OS X v10.3, click this icon, located above a script example, to automatically create a new script with the example code.

1) When a folder is opened. When this routine is called, the variable this_folder will contain a reference, in alias format, to the attached folder. Ex.: alias "Mac OS X:Users:sally:Desktop:Shortcuts:"


on opening folder this_folder
tell application "Finder"
set this_name to the name of this_folder
end tell
display dialog "Folder “" & this_name & ¬
"” has been opened." buttons {"OK"} default button 1
end opening folder



2) When a folder window is closed. When this routine is called, the variable this_folder will contain a reference, in alias format, to the attached folder. Ex.: alias "Mac OS X:Users:sally:Desktop:Shortcuts:"


on closing folder window for this_folder
tell application "Finder"
set this_name to the name of this_folder
end tell
display dialog "Folder “" & this_name & ¬
"” has been opened." buttons {"OK"} default button 1
end closing folder window for



3) When a folder window is moved or resized. When this routine is called, the variable this_folder will contain a reference, in alias format, to the attached folder. Ex.: alias "Mac OS X:Users:sally:Desktop:Shortcuts:". The variable original_coordinates will contain the value of the bounds of the window before it was moved, Ex: {78, 355, 853, 740}


on moving folder window for this_folder from original_coordinates
tell application "Finder"
set this_name to the name of this_folder
set the bounds of the container window of ¬
this_folder to the original_coordinates
end tell
display dialog "Window “" & this_name & ¬
"” has been returned to it's original " & ¬
"size and position." buttons {"OK"} default button 1
end moving folder window for



4) When items are added to a folder. When this routine is called, the variable this_folder will contain a reference, in alias format, to the attached folder. Ex.: alias "Mac OS X:Users:sally:Desktop:Shortcuts:". The variable these_items will contain a list of alias references to the items that were placed in the attached folder.


on adding folder items to this_folder after receiving these_items
tell application "Finder"
set this_name to the name of this_folder
end tell
display dialog ((the count of these_items) as string) & ¬
"items have been added to folder “" & this_name & ¬
"”." buttons {"OK"} default button 1
end adding folder items to



5) When items are removed from a folder. When this routine is called, the variable this_folder will contain a reference to the attached folder. Ex.: alias "Mac OS X:Users:sally:Desktop:Shortcuts:". The variable these_items will contain a list of the names of the items that were removed from the attached folder.


on removing folder items from this_folder after losing these_items
tell application "Finder"
set this_name to the name of this_folder
end tell
set the item_count to the count of these_items
display dialog (item_count as string) & "items have been removed " & ¬
"from folder “" & this_name & "”." buttons {"OK"} default button 1
end removing folder items from

Next Page
TABLE OF CONTENTS

Introduction

Contextual Finder Menu - Setting up Folder Actions in the Finder

Folder Actions Setup - A utility for administering Folder Actions

Writing Folder Actions - How to write Folder Actions scripts

Installed Scripts - A description of the installed Folder Actions scripts

Example Scripts - Other examples of Folder Actions scripts