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

◇◆丶佛笑我妖孽 提交于 2019-12-11 15:13:38

问题


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.


回答1:


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.




回答2:


Just found the error in the original script:

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

ItemByPersistentID is a method of Tracks collections.



来源:https://stackoverflow.com/questions/21058852/retrieve-an-itunes-track-object-from-its-high-low-persistent-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!