I must not be searching correctly?
I am looking for a folder action script that will delete any files within that folder except for the file or folder being added to that folder? Ultimately I want to unzip a file if it is archived when dragged into that folder as well. But first I wanted to clean the contents of the file so only the new files added are there. The unzipping part will be easy.
Here is a draft which may be used as a starting point.
on adding folder items to this_folder after receiving these_items
my germaine(this_folder, these_items)
end adding folder items to
# I use this handler during tests.
on germaine(this_folder, the_items)
tell application "System Events"
# Prepare the list of zip files added
set theArchived to {}
repeat with i from 1 to count the_items
# replace the aliases available in the list by strings for future comparison with existing items
set anItem to item i of the_items as text
set item i of the_items to anItem
# Fill the list of names of zip files added
if type identifier of disk item anItem is "public.zip-archive" then set end of theArchived to name of disk item anItem
end repeat
set everyFiles to path of every file of this_folder whose visible is true
repeat with aFile in everyFiles
# delete files which were stored before the recent addition
# Here it treats only files.
# If the folder may contain subfolders some changes would be required
if aFile as text is not in the_items then delete file (aFile as text)
end repeat
# The added files are supposed to be stored in a subfolder of this_folder
# If this subfolder must be in an other location some changes would be required
set storage to "files added"
set path2storage to (this_folder as text) & storage
# Creates the subfolder if itsn't available
if not (exists folder path2storage) then
make new folder at end of this_folder with properties {name:storage}
end if
end tell
set uPath to quoted form of POSIX path of path2storage
# I know that there is a way to move a list of items in a single call
# but I forgot the correct syntax.
repeat with anItem in the_items
do shell script "mv " & quoted form of POSIX path of anItem & space & uPath
end repeat
repeat with anItem in theArchived
# Unzip a zip file
do shell script "unzip " & quoted form of POSIX path of (path2storage & ":" & anItem) & " -d " & uPath
# Delete the unzipped file
tell application "System Events" to delete file anItem of folder path2storage
end repeat
end germaine
Yvan KOENIG (VALLAURIS, France) dimanche 14 septembre 2014 11:46:36
Thank you very much Yvan.
On second thought, I am probably going to take pieces of your code and apply it to a non-folder action script. Unfortunately I am not having much luck with any folder actions. For example, I tested using one of the scripts included with the OS, i.e., “add - new item alert.scpt”. On a Mac running 10.5.8 I couldn’t get the folder action script to execute. (And the folder/script was enabled correctly) On another Mac running 10.6 .8 the folder actions seem to work sporadically and the alert prompt had a huge delay. Unless the Mac at my office behaves differently in terms of folder actions, I am not sure if I am going to take the chance with the instability aspects I am experiencing.
Nevertheless I am very appreciative of your response and code provided.