Example folder action scripts

    Here are examples for each of the folder action commands defined by the Finder.

Opening a folder

    This example includes a handler for the Opening Folder command. It gets the name of the folder and displays a dialog box telling you when items have been placed in the folder.

    Show me the Opening Folder command.

Closing a folder

    This example includes a handler for the Closing Folder Window For command. It gets the name of the folder and the disk, then uses text-to-speech to tell you that the folder has closed.

    Show me a script for when a folder closes.

Moving a folder

    This example includes a handler for the Moving Folder Window For command. It gets the original position of the window and the new position of the window, and displays the information in a dialog box.

    Show me a script for when a folder is moved.

    Each of the Copy commands expresses the position of the window as a variable with four numbers. The first two numbers define the top-left corner of the window; the last two the bottom-right corner of the window. These numbers are in pixels, which are points on the screen measured from the top-left corner of the screen.

Adding items to a folder

Removing items from a folder

    This example includes a handler for the Removing Folder Items From command. It gets the name of the folder and the number of items removed, then uses the Say command to tell you this information.

    Show me a script for when folder items are removed.

    The example first changes the text delimiter used by AppleScript to the colon (:). A "text delimiter" is a character AppleScript usually recognizes as separating words. Because the file pathname contained in the parameter "this_folder" is separated by colons, the script can easily identify the last item, which is the name of the folder.

    This example also uses the Try command, which lets you handle error conditions that may occur. Because the script doesn't have any statements in the On Error part of the Try command, it will ignore error messages.

Related topic

 


Table of contents