Sorting a Music playlist

I am using AppleScript to create a playlist in Music and add tracks from the library. However, the track order in the final playlist is not always in the order I prefer - which would be by track number.
Can I alter my code to put the tracks in the correct order, or better yet, add another section which would sort the playlist by track number?

tell application "Music"
		make new user playlist with properties {name:toPlaylist}
	end tell
	
	
	tell application "Music"
		set MySelections to (every track of library playlist 1 whose (artist contains ArtistName) and ((name contains BWVB) or (name contains BWVC) or (name contains BWVCO) or (name contains BWVP)))
		repeat with aTrack in MySelections
			duplicate aTrack to playlist toPlaylist
		end repeat
	end tell

I can help you if you post here, what returns following script on your Music.app:


tell application "Music" to track number of tracks of library playlist 1

My code has always worked before in that the playlists created were always in track number order - as they are in every album.
The problem I had was recent and after I ran a Doug’s script to export playlists in batch, the problem appeared. The script was very buggy in other ways as well, so I deleted it, rebooted, and now the sort order of created playlists is correct again.

Nevertheless, it might be helpful in the future if I learned how to sort playlists remotely using AppleScript. Therefore:
@KniazidisR,
What does that script do? Do I place it in my script; if so where?

I asked you to run my script as is. To make sure that your tracks already have track numbers. Because by default, they are 0, unless you number them yourself.

Hi KniazidisR,

I know that all my tracks have track numbers because I always add them that way. In addition, I have verified track numbers by opening various library files in the an encoder app which reads the tags - it always verified when a track number was present as well as a real title.

I believe all newly created playlist will default
To the views you have on your music library playlist or Songs view.
It may default to the sorting order that you have as well.

Doug has a script for that. But no code info available.
https://dougscripts.com/itunes/scripts/ss.php?sp=mxplaylistviewsmanager

I think in order to do this you have to access the application’s UI Elements
Which are not documented anywhere and some are even hidden (like the search field)
Check out the UIbrowser app. It allows you to investigate and peek at the UIElements and
What there names or levels are.
It even has the ability to create AppleScript code for you.
It will take a bit of experimenting to get it right.

The other option may be to use ASObjC and put
Your foundTracks into a NSArray then create a new Array but
By using sortedArrayWithSortDescriptors
Then convert that back to a list.
Then add that to your playlist

UIBrowser

https://pfiddlesoft.com/uibrowser/

Hi, again, @TheKrell.

I am not sure it is what you asked for, but following script duplicates the tracks to new playlist sorted with track number order:


tell application "Music" to make new user playlist with properties {name:toPlaylist}

tell application "Music"
	set MySelections to (tracks of library playlist 1 whose (artist contains ArtistName) and ((name contains BWVB) or (name contains BWVC) or (name contains BWVCO) or (name contains BWVP)))
	repeat with aTrack in MySelections
		duplicate (get contents of aTrack) to playlist toPlaylist -- THIS
	end repeat
end tell

@KniazidisR
Hi again,
As I said, after I rebooted, the difficulty (which I suspect was caused by one of Doug’s scripts) went away.
I plan the following:
I will resort an album track listing in some arbitrary order and see if my code fails to put it right.
Then I will use your modification and see if it resorts it correctly by track order.
Whatever happens I will let you know.
Thank you for your suggestion.