views:

48

answers:

1

The following code should show a certain track in iTunes:

NSString* iTunesPath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.iTunes"];

iTunesApplication *iTunes = nil;
if ( iTunesPath ) {
 iTunes = [[SBApplication alloc] initWithURL:[NSURL fileURLWithPath:iTunesPath]];
 [iTunes setDelegate:self];
}

iTunesSource *librarySource = nil;
NSArray *sources = [iTunes sources];
for (iTunesSource *source in sources) {
 if ([source kind] == iTunesESrcLibrary) {
  librarySource = source;
  break;
 }
}
SBElementArray *libraryPlaylist = [librarySource libraryPlaylists];
iTunesLibraryPlaylist *iTLibPlaylist = nil;
if ([libraryPlaylist count] > 0) {
 iTLibPlaylist = [libraryPlaylist objectAtIndex:0];
}


SBElementArray *fileTracks = [iTLibPlaylist fileTracks];

iTunesFileTrack *track = [fileTracks objectAtIndex:4];
NSLog(@"Try to reveal track: %@ at path :%@",[track description],[[track location] path]);
[track reveal];

Output:

Try to reveal track: <ITunesFileTrack @0x1364ed20: ITunesFileTrack 4 of ITunesLibraryPlaylist 0 of ITunesSource 0 of application "iTunes" (2474)> at path :/Users/...

But absolutely noting happens. What am I doing wrong? (iTunes Version: 9.0.3)

+1  A: 

The Library playlist doesn't exist anymore in the UI; it's there in the model, so it shows up in AppleScript, but trying to reveal it or anything in it won't do anything in the UI, as you saw. You can reproduce this in AppleScript as well (reveal track 5 of library playlist 1 of source 1).

The solution is to talk to the Music playlist, not the Library playlist. “Music” is the second playlist—playlist 2 in AppleScript, [[librarySource playlists] objectAtIndex:1] in Cocoa.

If you want to reveal a playing item in whatever playlist it's playing in, use reveal current track (which should be [[iTunes currentTrack] reveal], although I haven't tested that).

Peter Hosey
solved my problem. Thx a lot!
V1ru8