简体   繁体   中英

AppleScript code works on High Sierra, but not on Monterey

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, ie, 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:

  1. myFolder is an alias specifier. You add the folder keyword which is a double reference, delete as alias in the first line
  2. oneFile is a Finder file specifier, you cannot append the string return . Maybe the file specifier is silently coerced to string in Sierra. If you want to use the file references then the return statement makes no sense anyway.

Another bad practice is to modify the array deleted123 while being enumerated. Create an extra variable.

And the repeat loop makes no sense either because exists (deleted123) is always true and even if you check the current item in the loop oneFile it's always true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM