是否可以监视 OS X 上其他应用程序的事件?

发布于 2024-11-06 22:25:40 字数 104 浏览 4 评论 0原文

本质上,我需要知道何时有人在 iTunes 中点击播放。我了解如何使用 Apple 的脚本桥控制 iTunes,但我似乎无法让 iTunes 控制我的应用程序。这可能吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-11-13 22:25:40

一般来说,不可能弄清楚其他应用程序对事件做了什么,不是。您也不可能知道 iTunes 中的播放按钮何时被按下。即使您要捕获鼠标按下事件,您也必须以某种方式确定 iTunes 的播放按钮当时是否位于该事件下方。

然而,在这种情况下,您可以做的是注册 iTunes 在曲目开始播放时发布的通知。 Dave DeLong 在 另一个答案 这里就这样。

// 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]);
}

这并不能满足你所​​希望的一切; 分布式通知很昂贵,因此 iTunes 对它们很节俭。当曲目开始播放时,您会收到通知,仅此而已;没有停止,没有音量,跳轨和从停止开始播放之间没有区别。不过,该通知确实包含有关曲目本身的大量信息:几乎所有 iTunes 元数据以及一些文件信息。

希望这有用!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文