I am trying to intercept unwanted downloaded files by setting up a Folder Action script attached to Downloads folder, which is - in a simplified version - pasted below:
property FileBlackList : {"Adobe Flash Player", "FlashPlayer"}
set myFileName to choose file
FilterFilesNames(myFileName)
on FilterFilesNames(myFileForFilter)
tell application "Finder"
tell file myFileForFilter
if name is in FileBlackList then
display notification "The file " & name & " is in the list."
delete myFileForFilter
else
display notification "The file " & name & " is NOT in the list."
end if
end tell
end tell
end FilterFilesNames
This script is working, but I would like to replace the line:
“if name is in FileBlackList”
with something like:
“if name starts with (any item of FileBlackList)”, if possible without looping into all items of FileBlackList
No. But the code for looping isn’t very long. The thing to remember is to get the name from the file first and then loop. It could get quite slow if you get the name from the file every time round the repeat.
property FileBlackList : {"Adobe Flash Player", "FlashPlayer"}
set myFileName to choose file
FilterFilesNames(myFileName)
on FilterFilesNames(myFileForFilter)
tell application "Finder" to set fileName to name of myFileForFilter
repeat with thisItem in FileBlackList
if (fileName begins with thisItem) then
display notification "The file " & fileName & " is in the list."
tell application "Finder" to delete myFileForFilter
return -- Exit the repeat and the handler immediately after deleting the file.
end if
end repeat
-- If we're here, the file wasn't deleted.
display notification "The file " & fileName & " is NOT in the list."
end FilterFilesNames
Thanks Nigel.
In the meantime I have developed the script below. But your is not only more efficient but also more elegant.
Thanks.
property FileBlackList : {"Adobe Flash Player", "FlashPlayer"}
set myFileName to choose file
FilterFilesNames(myFileName)
on FilterFilesNames(myFileForFilter)
tell application "Finder"
tell file myFileForFilter
get every item of FileBlackList
repeat with a in result
if name starts with a then
display notification "The file " & name & " is in the list."
delete myFileForFilter
return
else
display notification "The file " & name & " is NOT in the list."
end if
end repeat
end tell
end tell
end FilterFilesNames