开发者

Is it possible to watch for events from other applications on OS X?

Essentially, I need to know when someone hits play within iTunes. I understand how I can control iTunes using Apple's Scripting Bridge, but I can'开发者_运维问答t seem to make iTunes control my app. Is this possible?

Thanks!


In general, it's not possible to figure out what other apps are doing with events, no. It is also not possible for you to find out when the play button in iTunes is pressed. Even if you were to catch the mouse down event, you'd have to somehow figure out whether iTunes's play button was under it at the time.

What you can do in this case, however, is register for the notifications that iTunes posts when a track starts playing. Dave DeLong has laid it out, in another answer here on SO.

// Register for notifications, perhaps in awakeFromNib
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self 
        selector:@selector(iTunesTrackDidChange:) 
            name:@"com.apple.iTunes.playerInfo" 
          object:nil];

- (void)iTunesTrackDidChange:(NSNotification *)note {
    NSLog(@"%@", [note userInfo]);
}

This doesn't give you everything you might hope for; Distributed Notifications are expensive, and so iTunes is frugal with them. You get a notification when a track starts playing, and that's it; no stop, no volume, no distinction between jumping tracks and starting playing from a stop. The notification does have plenty of info about the track itself, though: pretty much all the iTunes metadata, and some of the file info.

Hope that's useful!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜