-->

get a specific track in itunes via ScriptingBridge

2019-07-12 02:04发布

问题:

I'm updating my OS X program to accept iTunes drops, modify the metadata in the file, then refresh (Get Info) the dropped file(s) so iTunes can update its' metadata library.

I've got the drops working, which provide a dictionary of some misc info about the file including track ID, persistent ID, and Location. I'm at the point now where I need to get a specific track, either by file location or the persistant ID provided by the drop info, so I can call the refresh method on it to force iTunes to reexamine the file and update the changes to the metadata.

I've got the iTunes header file imported and the SBApplication object created, I'm pretty stuck at this point on an efficient way to get the proper track.

回答1:

As stated, I already had access to some info about a file provided by an iTunes drop. I needed to match up info about that file based on either the file name or persistentID provided by the drop to the file in the iTunes library my solution was to use a predicate filter on the iTunes library against the library collection returned by the iTunes scripting bridge.

    iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

    SBElementArray *sources = [iTunes sources];

    SBElementArray *entireLibrary = [[[[sources objectAtIndex:0] libraryPlaylists] objectAtIndex:0] fileTracks];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"persistentID == %@", persistentID];

    [entireLibrary filterUsingPredicate:predicate];


回答2:

This is how I get the current track info from iTunes usingScriptingBridge:

iTunesApplication * iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSString * artist    = [[iTunes currentTrack] artist];
NSString * trackname = [[iTunes currentTrack] name];
/* etc. */