Retrieve an iTunes Track object from its high/low persistent ID

487 views Asked by At

I'm trying to retrieve a track object from its persistent ID using AutoHotkey (v1.1) and iTunes Windows 11. The script works well until I try to use the ItemByPersistentID method.

objITunesunesApp := ComObjCreate("iTunes.Application")
objITunesLibrary := objITunesunesApp.Sources.Item(1)
objITunesPlaylist := objITunesLibrary.Playlists.Item(1)
objITunesTrack := objITunesPlaylist.Tracks.Item(1)

; Test if objects are OK
MsgBox, % objITunesTrack.Name ; Display the song name - OK

; Get high and low IDs
intIDHigh := objITunesunesApp.ITObjectPersistentIDHigh(objITunesTrack)
intIDLow := objITunesunesApp.ITObjectPersistentIDLow(objITunesTrack)
MsgBox, %intIDHigh% %intIDLow% ; Display: "-1071797520 -947434212" OK

; Try to get the track again using the persistent IDs
objTrackByID := objITunesLibrary.ItemByPersistentID(intIDHigh, intIDLow)
; Error:  0x80020006 - Name unknown
; Specifically: ItemByPersistentID

MsgBox, % "objTrackByID.Name: " . objTrackByID.Name ; name empty

Am I calling ItemByPersistentID the right way? Thanks.

2

There are 2 answers

0
JnLlnd On BEST ANSWER

Just found the error in the original script:

objTrackByID := objITunesPlaylist.Tracks.ItemByPersistentID(intIDHigh, intIDLow)

ItemByPersistentID is a method of Tracks collections.

1
JnLlnd On

Unless someone else provide an answer about the ItemByPersistentID function, I found this other approach. More tedious because you have to save 4 IDs about a track to be able to retrieve it by ID(s), but it works.

objITunesunesApp := ComObjCreate("iTunes.Application") ; will launch iTunes if not running
objITunesLibrary := objITunesunesApp.Sources.Item(1) ; main library
objITunesPlaylist := objITunesLibrary.Playlists.Item(1) ; main playlist
objITunesTrack := objITunesPlaylist.Tracks.Item(1) ; first track

; get IDs for this track
sourceID := objITunesTrack.sourceID
playlistID := objITunesTrack.playlistID
databaseID := objITunesTrack.trackDatabaseID
trackID := objITunesTrack.trackID
; check track name to see if we can retrieve the same track by IDs
name := objITunesTrack.name

MsgBox, % ""
    . "sourceID: " . sourceID . "`n"
    . "playlistID: " . playlistID . "`n"
    . "databaseID: " . databaseID . "`n"
    . "trackID: " . trackID . "`n"
    . "name: " . name . "`n"

; retrieve the same track using IDs
objTrackByID := objITunesunesApp.GetITObjectByID(sourceID, playlistID, trackID, databaseID)

MsgBox, % "objTrackByID.name: " . objTrackByID.name ; same name as previously -> WORKS!

sourceID and playlist won't change for all tracks in the same playlist. So we only need to save 2 IDs per track to be able to retrieve a track. Finally, not much more complex than retrieving by ItemByPersistentID if whenever it is feasible.