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 *)
if (count of f) > 0 then
repeat with fl from 1 to (length of f) (* duplicate each file to
the backup folder *)
duplicate (item fl of f) to backupFolder replacing yes
end repeat
end if
on error errmesg
display dialog "An error: " & errmesg
return -- return empty-handed if there was an error
end try
display dialog "backup complete!"
end tell
end closing folder window for
- moving folder window for
alias
You can have a script execute when a folder is moved using this
command. The syntax would be:
on moving folder window for theFolder from rec...end moving folder window for
The variable theFolder (or whatever name you give
it) receives an alias to the folder. The variable
rec receives a list of
coordinates that represent the top left and top right corners of the
screen space the window occupied before it was moved. The next code
example gets and displays the coordinates of the window (in the form
of "10 : 50 : 370 : 500") stored in
rec. The windows that have the attached scripts
have to be open in the Finder for the "moving folder
window for" and "adding folder
items to" folder actions to execute properly.
- from bounding rectangle
The from labeled parameter gives whatever variable
you supply with it a rectangle value, as in {10,50,370,500}
(basically a list of integers).
For example:
on moving folder window for tf from rec
set old_delim to text item delimiters
set text item delimiters to " : "
display dialog (rec as text)
(* set text item delimiters back to empty string default *)
set text item delimiters to old_delim
end moving folder window for
- adding folder items to alias
This command is triggered when items are added to an open window that
has one of these folder-action types attached to it (this folder
action only works when the attached folder window is open). The
following example displays a count of the number of folder items
every time a new one is added to the directory. This is just a
folder-action functionality example; you might want to log similar
folder activity, but you normally would not want to display a dialog
every time something happened with a folder, unless you want to
antagonize users:
on adding folder items to f
tell application "Finder"
activate
set fcount to (count files of f)
display dialog ("there are now " & fcount &¬
" files in the folder " & (name of f)) giving up after 10
end tell
end adding folder items to
- removing folder items from alias
This subroutine is executed when items are removed from an attached
folder. You use it in the form of:
on removing folder items from theFolder after losing alias_list...end removing folder items from
The theFolder variable (or whatever name you give
it) contains an alias of the folder. The
alias_list variable contains a list of aliases
referring to the items that were removed from the folder. This next
example admonishes the user after an item is removed from the folder.
- after losing list of aliases
This labeled parameter contains a list of aliases
representing the items that were removed from the folder. This code
demonstrates this parameter:
on removing folder items from theFolder after losing alias_list
tell application "Finder"
display dialog "Removing " & ((item 1 of alias_list) as &¬
text) & " from " & (name of theFolder) &¬
" is strictly forbidden!"
end tell
end removing folder items from
Dictionary commands for Folder Actions extension
- run
This command runs the Folder Actions server. See
quit.
- attach action to
folder alias
Attach a folder action to a folder using this command:
attach action to fol_alias using script alias
The fol_alias variable contains an
alias to a folder. The
script_alias variable is an
alias to the AppleScript that will be attached to
the folder. The Example section lets the user choose a folder to
attach actions to.
- using alias
Use this labeled parameter to specify the script that will be
attached to the folder. The script itself is stored in an
alias variable or in a literal
alias:
using script (alias "macintosh hd:desktop folder:moveApplet")
- remove action from
alias
You can script the removal of a folder action from a folder with this
command. You have to identify the folder with an
alias variable or a literal
alias.
- action number integer
This labeled parameter specifies by index number which action to
remove from the folder (if there is more than one attached action).
For example, if you want to remove the second folder action, then
use:
action number 2
|
In Folder Actions 1.5.5, an extension that installs with Mac OS 9.1,
the action number parameter has been changed to
"using action number."
|
|
- action name string
As an alternative, you can specify the name of the script to remove,
as in: action name
"moveScript". The name of the attached
script also shows up in the contextual menu (attained by
Control-clicking the folder) under the menu item
"Remove a folder action."
set f_alias to¬
(choose folder with prompt¬
"choose a folder, cleanse its action")
tell application "Folder Actions"
remove action from f_alias action number 2
end tell
- edit action of
alias
You can open up an attached script in Script Editor by using code
such as:
edit action of theFolder action name "moveScript"
Chapter 2 is devoted to Script
Editor.
- action number integer
Use this labeled parameter to specify the index number of the
attached action:
action number 2
- action name string
You can specify the name of the action to edit with this labeled
parameter:
action name "myAction"
- attached scripts alias
You can find out if a folder has any attached scripts by passing the
folder as an alias to this command:
tell app "Folder Actions" to attached scripts folder_alias
The folder_alias variable (or whatever you name
it) contains an alias to the folder you are
examining for attached scripts. This command returns a
list. Each member of the list
is a list containing a file
alias for the attached script. The return value
looks like:
{{alias "Macintosh HD:Desktop Folder:moverScript"}}
Yes, for some reason this command returns a list
inside of another list.
- quit
This command quits the Folder
Actions server. See run and the following
example.
Examples
set fol to choose folder with prompt¬
"Choose the folder to attach the action to"
set theAction to alias "Macintosh HD:Desktop Folder:moverScript" (* this script
will be attached to the folder the user chooses *)
tell application "Folder Actions"
(*start the Folder Actions server; it is not strictly necessary to use run
or quit *)
run
attach action to fol using theAction
quit
end tell
|