Sunday, October 17, 2021

#1 2021-09-18 01:58:31 pm

TheKrell
Member
Registered: 2020-03-08
Posts: 14

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?


Applescript:

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


Filed under: sort, music, tracks, playlists

Offline

 

#2 2021-09-19 07:42:21 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Sorting a Music playlist

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

Applescript:


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

Last edited by KniazidisR (2021-09-19 07:44:10 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-09-20 01:03:50 pm

TheKrell
Member
Registered: 2020-03-08
Posts: 14

Re: Sorting a Music playlist

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?

Offline

 

#4 2021-09-20 11:33:25 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Sorting a Music playlist

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.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-09-21 01:57:30 pm

TheKrell
Member
Registered: 2020-03-08
Posts: 14

Re: Sorting a Music playlist

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.

Offline

 

#6 2021-09-21 07:52:34 pm

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Sorting a Music playlist

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.

Offline

 

#7 2021-09-21 08:45:25 pm

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Sorting a Music playlist

Doug has a script for that. But no code info available.
https://dougscripts.com/itunes/scripts/ … ewsmanager

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

Offline

 

#8 2021-09-21 08:47:38 pm

technomorph
Member
Registered: 2017-12-14
Posts: 201

Re: Sorting a Music playlist

Offline

 

#9 2021-09-22 10:17:25 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Sorting a Music playlist

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:

Applescript:


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

Last edited by KniazidisR (2021-09-22 10:24:02 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#10 2021-09-24 01:43:33 pm

TheKrell
Member
Registered: 2020-03-08
Posts: 14

Re: Sorting a Music playlist

@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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)