I want to look into a folder to select every file whose name contains "abc".
Here is my AppleScript code:
set myFolder to (((path to library folder from user domain) as string) & "FOLDER") as alias
tell application "Finder"
set deleted123 to every file of folder myFolder whose name contains "abc"
repeat with oneFile in deleted123
if exists (deleted123) then set end of deleted123 to oneFile & return
--Do something
end repeat
if deleted123 ≠ {} then
--Do something else with the selected.
end if
end tell
The code works flawlessly on High Sierra, i.e., it finds out all the files whose names contain "abc", but it doesn't on Monterey.
What is the problem? How can this piece of code be improved?
Help highly appreciated.
There are two major mistakes:
myFolderis an alias specifier. You add thefolderkeyword which is a double reference, deleteas aliasin the first lineoneFileis a Finder file specifier, you cannot append the stringreturn. Maybe the file specifier is silently coerced to string in Sierra. If you want to use the file references then thereturnstatement makes no sense anyway.Another bad practice is to modify the array
deleted123while being enumerated. Create an extra variable.And the repeat loop makes no sense either because
exists (deleted123)is alwaystrueand even if you check the current item in the looponeFileit's alwaystrue.