如何在 Mac OS X 中监听应用程序启动事件?
我编写了一个 AppleScript
来挂载 SparseBundle
图像,并且我希望它在 Time Machine
启动时准确执行。
现在,我定期使用 onidle
语句检查 Time Machine 是否正在使用 AppleScript
运行:
on idle
....
return <interval>
end idle
这不是一个可靠的方法。在我看来,为 Application Launch
事件添加事件触发器将是更好的方法。
你能帮忙吗?
非常欢迎 Objective-C
或 Python
示例代码(我更喜欢 Python
)。
I wrote an AppleScript
to mount a SparseBundle
image and I want it to be executed exactly when Time Machine
launches.
Right now, I periodically check if Time Machine is running with AppleScript
using on idle
statement:
on idle
....
return <interval>
end idle
which isn't a robust way. In my opinion adding an event trigger for Application Launch
event would be a better approach.
Could you please help?
An Objective-C
or Python
sample code (I'd prefer Python
) is more than welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
这在 Objc-C 中并不太难做到。您可以通过 NSWorkspace 和 NSNotificationCenter 访问所有应用程序的通知。创建一个对象并注册其方法之一以获取 NSWorkspaceDidTerminateApplicationNotification 类型的通知。像这样的东西:
@interface NotificationObserver : NSObject { }
- (void) applicationDidLaunch:(NSNotification*)notification;
@end
@implementation NotificationObserver : NSObject
- (void) applicationDidLaunch:(NSNotification*)notification
{
// Check the notification to see if Time Machine is being launched.
}
@end
void watch(void)
{
NSNotificationCenter* notificationCenter
= [[NSWorkspace sharedWorkspace] sharednotificationCenter];
NotificationObserver* observer = [[NotificationObserver alloc] init];
[notificationCenter addObserver:observer
selector:@selector(applicationDidTerminate:)
name:@"NSWorkspaceDidTerminateApplicationNotification"
object:nil];
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您正在寻找的是 NSDistributedNotificationCenter 或 NSWorkspace ,这些可可类发布应用程序事件的通知,对于工作区,例如应用程序启动、驱动器安装等。
要在 python 中执行此操作,您需要PyObjC,它基本上是苹果可可类的 python 绑定。他们网站上的文档很少,这是有原因的,因为文档基本上与 Apple 文档相同,所以它们只包含 pyobjc api 和 cocoa API 之间的差异。如果您了解目标 c api 是如何转换为 python 的,那么您就可以开始了。检查此处: http://pyobjc.sourceforge.net/documentation/pyobjc-core/ intro.html
我在下面提供了一个示例,它使用 python 监听分布式通知。下面的代码基本上添加了一个观察者并监听 itunes 通知。您可以遵循类似的结构,但为 NSWorkspace 添加一个观察者。为了弄清楚您应该听什么,有一个应用程序可以显示通过您系统的所有通知。它称为 通知观察程序 。用它来确定你应该听什么。您还可以将 Objective C 代码转换为 Python。
下面的代码在做什么
有一件事会让您感到困惑,访问属性(目标 c 属性)与访问 python 属性的工作方式不同。即在 python 中,您在 python 中为目标 c 执行
class_name.att
,您必须像函数一样调用它,即从我下面的示例中:song.userInfo()
这是一个示例实际输出...(是的布兰妮摇滚!,不是!;)
What you are looking for is, NSDistributedNotificationCenter or NSWorkspace , these cocoa classes post notifications of application events, For workspace, things like application launches, mounting of drives etc.
To do this in python, you need PyObjC, which is basically python bindings for apple's cocoa classes. The documentation is sparse on their website, and there's a reason, as the documentation would be basically be the same as the Apple docs, so they only include the differences between the pyobjc api, and the cocoa API. If you understand how the objective c api is converted to python you are good to go. Check here: http://pyobjc.sourceforge.net/documentation/pyobjc-core/intro.html
I have included an example below which listens for Distributed notifications using python. The code below basically adds an observer and listens for itunes notifications. You could follow a similar structure, but instead add an observer for NSWorkspace. To figure out what you should be listening to, there is an application that will display all notifications going through your system. It's called notification watcher . Use this to figure out what you should be listening to. You could also convert the objective c code to python.
What the code below is doing
One thing that will trip you up, accessing attributes (objective c attributes) do not work the same as accessing python attributes. i.e in python you do
class_name.att
for objective c in python you have to call it like a function i.e from my example below:song.userInfo()
Here's an example of the actual output... (YES BRITNEY ROCKS!, NOT! ;)