Sortorder of items in iTunes playlist

Hi,

I have a question about AppleScript and iTunes again. On earlier iTunes versions you could with


tell application "iTunes"
	return name of every track of item 2 of playlists
end tell

ask for the names of all tracks in a playlist or library. The answer was just as ordered, as shown in iTunes. With iTunes 12, I get always a different sort. What should I do to get the information in the desired order.

Kind regards

Berthold

With iTunes 12.2.2.25 (the late one), here the script returns exactly what is displayed in list mode according to the setting applied in the palette available on the right of the window : entitled [ Liste des vidéos ] in French.
iTunes dictionary define six sort options but my understanding is that they apply to music tracks, not to videos.
So, to rule the wanted list you will have to rely - o,ne more time - to GUI Scripting.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 16 août 2015 17:50:36

You are right. With iTunes 12.2.25 the sorting is OK. But, not for very large playlists.
There is no problem with any playlist with up to 23,000 entries. With a playlist with more than 100,000 entries, it is not working.
Any idea (OS X 10.10.5, iTunes 12.2.25, playlists for music)?

Kind regards

Berthold

I assumed that I was clear.
If you want to sort according to a precise setting, you must use GUI Scripting to select the key which must be used to sort.
You wrote that when the playlist contain more than 100,000 entries the list returned by the script is not the one displayed in the graphical interface. I must trust you because at this time, I have “only” 11135 tunes installed and 13 videos installed.
It would be interesting to know which list is correct.
If the list displayed in the UI is right and the one returned by the script is not, you may apply a sort handler to the list returned by the script.
Shane Stanley posted efficient handler using ASObjC to achieve the task.

If it’s the list displayed by the UI which is wrong you have two solutions :
(a) split your playlist into several shorter ones
(b) file a message to Apple as a bug report thru the dedicated channel or file a comment thru the dedicated menu item available in iTunes’s iTunes menu.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) dimanche 16 août 2015 21:09:46

Hi Yvan,

take a look at the screenshoots. Then you know what I mean.
23.000 tracks Sortorder OK
115.000 tracks Sortorder not OK

I need the result for an objective C program with database. It would be easiest to query the iTunes database directly. Because it is encrypted this is not possible. Therefore, I have taken the detour via AppleScript. It would of course also possible to play the playlist track by track and query then each track individually. With the large number of tracks this takes hours. The query with - of every track - takes just minutes.
Is there any way else to solve this issue?

Kind regards

Berthold

Your screenshots don’t help me. I don’t know which is the tool whose window appears on the right edge of them.

The only info which I get is that you are displaying the playlist by tunes.

In this mode, the sort used by the app to display the library is not reflected by AppleScript which - at least on my machine - always returns the library sorted by manual order.
I’m surprised that you didn’t saw that which is what I get with a library containing only 433 tunes.

To be sure that my statements are clear :

If the app is displaying Libraries (with albums covers) when I trigger this script,

tell application "iTunes"
	return name of every track of item 21 of playlists
end tell

it always return the list of tunes sorted by manual order
If covers aren’t displayed the script returns le list sorted alphabetically

If the app is displaying “My music” when I trigger this script :

tell application "iTunes"
	return name of every track
end tell

it always return (with or without covers) the list sorted by album, each album being sorted by track index.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 17 août 2015 10:56:22

Sorry for the bad explanation. On the right side you can see the Explorer of ScriptDebugger. As this is for me the only known possibility to debug AppleScript, I’ve assumed that this is known.
The result of ScriptDegugger is identical to the result which I get with Xcode.

[theScript setString:@"tell application \"iTunes\"\n"]; // [theScript appendString:@"return name of every track of item 2 of playlists\n"]; [theScript appendString:@"return name of every track of playlist \"---\"\n"]; [theScript appendString:@"end tell"]; NSAppleScript *scriptURL10= [[NSAppleScript alloc] initWithSource:theScript]; NSAppleEventDescriptor *result10 = [scriptURL10 executeAndReturnError:nil]; NSArray *names = [self arrayFromDescriptor];
You can see that in a playlist with only 23,000 entries (20 other small playlists equally tested) sorting between iTunes and AppleScript is equal. With a playlist with 115,000 entries, the output is different sorted as shown in iTunes. I think this in an error in iTunes.

I will now try to transfer the data from the Mediathek.xml file in a database. Then I can sort the database correctly. Finally, I can get the missing data such as for example the cover via AppleScript.
Not nice but should work.

Kind regards

Berthold

It seems that you didn’t read carefully what I posted so I repeat :

If the app is displaying Libraries (with albums covers) when I trigger this script,

tell application "iTunes"
   return name of every track of item 21 of playlists
end tell

it always return the list of tunes sorted by manual order
If covers aren’t displayed the script returns the list sorted alphabetically

In your case, the album covers are displayed so AppleScript returns the list sorted by manual order which seems to match what Script Debugger display.

I apologize but I’m not earning my life with AppleScript.
I am a retired potter playing with Applescript so I don’t wish to spend $199 just to try to help users here and there.

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) lundi 17 août 2015 16:54:32

I am glad about the help here and I read correctly and also tried it. Unfortunately without success. Other parameters such as shuffle, repeat, I have also tried.
The advice described work for me with all Playlists (max. 23,000 items) except the large (115,000 titles) one. So I’m pretty sure that this is a general problem with iTunes.

Maybe there is another solution. Therefore, I will describe what I intend to do.

I have an app written (client and server) with which I can control iTunes and other multimedia apps. So that scrolling through the albums can be done without delay, I have created an offline database of my iTunes library. This also has the advantage that I can see in the store, if a CD already exists.

To generate the offline database I go through a loop with all the titles (playlist with all titles). To avoid permanent database queries, I’m assuming that if changes the album title a new album begins. Therefore, the entries in the database carried out for reasons of speed without further verification. Therefore sorting the iTunes library is absolutely necessary.

Now I try out the parameters fixed indexing. Although it has impact on the sorting, but not the desired.

If someone has an idea, I will like to try it again. Otherwise I write next week a parser for Mediathek.xml with Objective C.

Kind regards

Berthold

Wouldn’t it be better to decipher the iTunes’s file : iTunes Library Genius.itdb which is probably a sqlite database ?

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mercredi 19 août 2015 10:31:37

What makes you think it’s encrypted? Assuming that Mediathek.xml is the file known as iTunes Library.xml in English-language systems, it’s easy enough to read:

use AppleScript version "2.3.1"
use framework "Foundation"

set thePath to current application's NSString's stringWithString:"/Volumes/General/iTunes/iTunes Library.xml" -- change path to suit
set theData to current application's NSData's dataWithContentsOfFile:thePath
set theDictionary to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)) as record

Of course it will be much quicker directly in Objective-C; most of the time is taken converting the dictionary to an AppleScript record.

Or you can read it as XML:

use AppleScript version "2.3.1"
use framework "Foundation"

set thePath to current application's NSString's stringWithString:"/Volumes/General/iTunes/iTunes Library.xml" -- change path to suit
set theDoc to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:(current application's class "NSURL"'s fileURLWithPath:thePath) options:(current application's NSXMLDocumentTidyXML) |error|:(missing value)

Again, easy enough to do in Objective-C.

Hello Shane.

My library is a small one but when I run the first code (the one ending with : set theDictionary to.) I get :
error “Saturation de la table interne.” number -2707

When I run the second one, I get : «class ocid» id «data optr00000000F0C04C2D80610000»

It seems that I missed something.
My boot volume is an external SSD but my library is on the internal mechanical HD, so its path is :
“/Volumes/Macintosh HD/Users//Musique/iTunes/iTunes Library.xml”
May this explain what I get ?

Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mercredi 19 août 2015 10:58:43

It’s possibly still too big to make a record of, though. Keeping it a dictionary is likely to be less problematic.

Yes, sorry – that’s an XML document. I was assuming the OP would know how to parse that, which depends on what he wants to extract. I’m guessing he would do it in Objective-C, given that’s where he’s doing his other coding.

My boot volume is an external SSD but my library is on the internal mechanical HD, so its path is :
"/Volumes/Macintosh HD/Users/<anUserAccount>/Musique/iTunes/iTunes Library.xml" 

No, I don’t think that makes any difference.