Beloved "Add to Music.app in Reverse Order" script broken in Big Sur

This script is suddenly misbehaving. I used it in iTunes for years, then I tweaked and renamed it when Music.app replaced iTunes, and it worked fine until I installed Big Sur. Now it’s gone wonky.

The script imports tracks into Music.app in reverse alphanumeric order, so that when displayed in Song view, sorted by Date Added, the tracks appear in the correct order.

So, if your tracks are named…

01 Great Song
02 My Baby
03 New York, New York

…the script imports track 03 first and works backward.

But in Big Sur, for some reason, the last track is now skipped, then the remaining tracks are imported as expected. So, using the examples above, running the script now produces this:

03 New York, New York
01 Great Song
02 My Baby

Or if I import 5 songs in reverse order, I get this:

05
01
02
03
04

I’m not experienced with scripting, so I’m wondering if someone here might know why it has stopped working. Here’s the script:

tell application "Finder"
    set selectedFiles to selection
    repeat with i from (length of selectedFiles) to 1 by -1
        my addFile(item i of selectedFiles)
        delay 1
    end repeat
end tell

to addFile(aFile)
    tell application "Music"
        try
            add aFile as alias
        end try
    end tell
end addFile[/AppleScript]

Thanks!

In “Music”, what are the view options of the view that you open Music to? What’s the sorting?

Does the switching to Sort by name put it in desired order?

Other than that, the script itself appears as legit and correct. I’d recommend you to try creating a reverse list using the reverse property of a list object and defining aFile as a script’s property, like so:


property aFile: missing value

tell application "Finder"
    set selectedFiles to reverse of (get selection)
    repeat with anObj in selectedFiles
set Obj_Alias to (anObj's contents) as alias
        my addFile(Obj_Alias)
        delay 1
    end repeat
end tell

to addFile(aFile)
    tell application "Music"
        try
            add aFile
        end try
    end tell
end addFile

Another version of this script would skip one step to make it even more streamlined and elegant: a Finder’s object known as alias list. It converts Finder references to AppleScript’s aliases in batch eliminating the need of looping through the entire list of items.


property selectedFiles: {}

tell application "Finder"
    set selectedFiles to reverse of ((get selection) as alias list)
    
        my addFiles(selectedFiles)
        
end tell

to addFiles(selectedFiles)
    tell application "Music"
        try
            add selectedFiles
        end try
    end tell
end addFile

Does any of the variants make a difference?

Model: MacBook Pro 9,1 (mid-2012 15") Core i7 2.3 GHz, 16 GB RAM, 1TB SSD
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

I don’t have Big Sur for testing, but the issue you describe can occur as a result of the order in which the files are selected in the Finder. To see if that’s the case, change the second line of your script to:

set selectedFiles to sort (selection) by name