Мы не будем его тупо опрашивать из AppleScript, мы будем получать его нотификации.
1. Подпишемся на сообщения в момент загрузки приложения.
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
// ...
[[NSDistributedNotificationCenter defaultCenter] addObserver: self selector: @selector(receivediTunesNotification:) name: @"com.apple.iTunes.playerInfo" object: @"com.apple.iTunes.player" ] ;
// ...
}
2. Не забываем отписаться в момент завершения
- (void) dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self] ;
[super dealloc] ;
}
3. Обрабочик сообщений
- (void) receivediTunesNotification:(NSNotification *)theNotification {
NSDictionary *info = [theNotification userInfo] ;
// NSEnumerator *en = [info keyEnumerator] ;
// NSString *key ;
// while (key = [en nextObject]) {
// NSString *val = [info objectForKey:key] ;
// NSLog(@"%@ :: %@", key, val) ;
// }
NSString *state = [info objectForKey:@"Player State"] ;
if ([state compare:@"Playing"] == NSOrderedSame) {
NSString *artist = [info objectForKey:@"Artist"] ;
NSString *album = [info objectForKey:@"Album"] ;
NSString *trackTitle = [info objectForKey:@"Name"] ;
}
}
Ой.

1 коммент.:
P.S.
A Mac OS X application that monitors all workspace and distributed notifications
http://github.com/kballard/NotificationWatcher
Отправить комментарий