Why does iterating over a list in applescript with `repeat with … in` fail with error code `-1731` part way through the list? I have this script for ensuring that every track in a given smart playlist is both shufflable and bookmarkable.

The script is inteded to be launched via launchd.

All of this is set up fine except that the script exhibits strange behavior. When you run it, some of them succeed but the majority seem to fail due to error code -1731. Subsequent runs the same behavior happens until you have no more shufflable and bookmarkable tracks.

I would expect that if the code errors on the first run it would keep erroring but that's not what happens.

What's happening here?
Add an explicit get to resolve the reference to the tracks:

tell application "iTunes"
    repeat with t in (get tracks of playlist "Listen Smart" where shufflable is false)
        set shufflable of t to true
    end repeat
    repeat with t in (get tracks of playlist "Listen Smart" where bookmarkable is false)
        set bookmarkable of t to true
    end repeat
end tell


Try running a script like this:

tell application "iTunes"
    repeat with t in (tracks of playlist "Listen Smart" where shufflable is false)
        t
    end repeat
end tell


If the playlist has three tracks where shufflable is false, the result at the end will be something like item 3 of every track of playlist "Listen Smart" of application "iTunes" whose shufflable = false. If you set the shufflable of the first two tracks to true inside the repeat loop, there won't be an item 3 where shufflable is false.

If you replace repeat with t in (tracks with repeat with t in (get tracks, the result at the end will be something like item 3 of {file track id 54774 of user playlist id 54771 of source id 73 of application "iTunes", file track id 54775 of user playlist id 54771 of source id 73 of application "iTunes", file track id 54776 of user playlist id 54771 of source id 73 of application "iTunes"}.