Mac OS X - 监控应用程序启动?
我想为 Mac OS X 编写一个简单的菜单栏应用程序。用户只想在打开 Safari 时使用该应用程序。为了避免不必要地混乱菜单栏,我想根据 Safari 是否打开来隐藏和显示菜单栏图标。
我的应用程序是否可以注册一些通知?我能想象的唯一解决方法是轮询正在运行的进程并查看 Safari 是否已启动,但这似乎不是解决我的问题的优雅方法......
I want to write a simple menubar app for Mac OS X. The user will only want to use that app when Safari is opened. To not clutter the menubar unnecessarily, I want to hide and show the menubar icon depending on whether Safari is open or not.
Is there maybe some notification that my app could register for? The only workaround I can imagine is poll the running processes and see if Safari is launched, but that doesn't seem to be an elegant way to solve my problem...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSWorkspaceDidLaunchApplicationNotification
和NSWorkspaceDidTerminateApplicationNotification
。 (有等效的碳事件。)NSWorkspaceDidLaunchApplicationNotification
andNSWorkspaceDidTerminateApplicationNotification
. (There are equivalent Carbon Events.)在 Carbon 事件管理器中使用 kEventAppFrontSwitched 来获取通知另一个应用程序变为活动状态。
Use kEventAppFrontSwitched in Carbon Event Manager to get notifications when another application becomes active.
使用此代码: http://cl.ly/2LbB
这将运行选择器
-doStuff< /code> 当 Safari 运行时。如果出现错误,请取消注释
usleep()
行。Use this code: http://cl.ly/2LbB
This will run the selector
-doStuff
when Safari runs. If you get an error, uncomment theusleep()
line.遇到了同样的问题,但感谢 JWWalker、文档和谷歌编写了以下代码:
Got same problem, but thanks to JWWalker, documentation and google wrote this code: