O'Reilly logo

AppleScript in a Nutshell by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Folder Actions

Syntax

(* 
display a dialog whenever a certain folder is opened. The f variable 
contains an alias to the folder that was opened.
 *)
on opening folder f
   tell application "Finder"
      activate
      display dialog ("You opened " & (name of f)) giving up after 10
   end tell
end opening folder

Dictionary commands for Folder Actions Suite

opening folder alias

This command is used as a subroutine or handler, in the form of:

on opening folder theFolder...end opening folder

The theFolder variable contains an alias to the folder. The subroutine definition

on opening folder...

can then access elements of the folder by using Finder commands.

closing folder window for alias

Use this command as part of a subroutine definition for handlers that trigger when attached folder windows are closed:

on closing folder window for theFolder...end closing folder
window for

The theFolder variable contains an alias to the attached folder. This code example backs up all files in a folder to a backup disk when the folder window is closed:

on closing folder window for theFolder tell application "Finder" try activate (* make the backup folder if it doesn't exist *) if not (exists (folder "mybackup" of disk "backup")) then set backupFolder to (make new folder at disk "backup"¬ with properties {name:"mybackup"}) else set backupFolder to (folder "mybackup" of disk "backup") end if (* get a list of the files of the attached folder *) set f to (files of theFolder) (* only do this if the folder is not empty ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required