Folder Actions are potentially very useful, although their current implementation leaves a little to be desired.
For example, if the target folder of a folder action is moved, it is assumed that the folder’s entire contents have again been added to it - so they are all processed once more. Also, if an invisible file is automatically added (such as when Finder decides to add an invisible “.DS_Store” file to the folder), the addition is processed as if added by the user - although he/she has no direct control over the event or its timing. (Even a renamed item is regarded as having been newly added.)
This behaviour seems somewhat buggy, and hopefully a fix will be forthcoming in due course. But in the meantime, what can we do about it?
The workarounds I’ve considered have included storing (in a property) a folder’s path or its current contents - and then comparing the values to those in force at the time the folder action is triggered. However, while such techniques might work tolerably well, they have a couple of significant drawbacks - namely:
An alternative method might be to check for invisible items in the added list. Since these can’t be added by the user manually, their presence indicates that the current list of additions is the result of some other action. (While this technique doesn’t address the file renaming issue, it should fix the other anomalies mentioned.)
There are a couple of points about the following workaround worth mentioning. If the target folder initially contains no invisible files, then the script can’t really distinguish between items being added and the folder itself being moved. In this event, the script resorts to asking the user to confirm the action taken. However, this should occur only once, since the folder is simultaneously ‘marked’ with an invisible file (“.FA_Marker”) - which should then aid the accurate identification of subsequent actions.
To incorporate the suggestion in your script, simply insert the ‘itemsAdded’ script object at the beginning of your script. Then wrap the contents of your ‘adding folder items to’ subroutine in a ‘validList of itemsAdded’ conditional block - something like this:
script itemsAdded
property utxt_return : return as Unicode text
property an_invisible : utxt_return & "."
on firstAction for this_folder
tell application "Finder"
make new file at folder this_folder with properties {name:".FA_Marker"}
with timeout of weeks seconds
button returned of (display alert "Please confirm the action you are perfoming:" buttons ¬
{"Adding Items", "Moving Folder"} message "Since this is the first folder action for \"" & this_folder's name & ¬
"\", please clarify which action you are currently taking. (This message should not appear again.)")
end timeout
end tell
end firstAction
on validList from items_added to this_folder
set tid to text item delimiters
set text item delimiters to utxt_return
set item_list to utxt_return & items_added
set text item delimiters to utxt_return & this_folder
set item_list to item_list's text items
set text item delimiters to utxt_return
set item_list to item_list as Unicode text
set folder_list to utxt_return & (list folder this_folder)
set text item delimiters to tid
considering case
an_invisible is not in item_list and (an_invisible is in folder_list or (firstAction for this_folder) is "Adding Items")
end considering
end validList
end script
on adding folder items to this_folder after receiving this_list
if validList of itemsAdded to this_folder from this_list then
(* add your required processing here - this example for demonstration purposes only *)
tell application "Finder" to repeat with this_item in this_list
display dialog ((this_item's kind as Unicode text) & " \"" & this_item's name & "\"") giving up after 2
end repeat (* end of example *)
end if
end adding folder items to