[英]Provide an OSX Service Without Launching the App?
I have successfully implemented a "faceless service" (background-only app with .service extension) and get it to work (see this question ), based on Apple's documentation and other tutorials on the web. 基于Apple的文档和网络上的其他教程,我已经成功实现了“匿名服务”(具有.service扩展名的纯后台应用程序)并使其正常工作(请参阅此问题 )。
Now, I want to advertise a service from an existing, single-window GUI app that I have. 现在,我想从我现有的单窗口GUI应用中发布服务。
I have setup the Info.plist file of my app to advertise the service, and it gets installed when I build the app. 我已经设置了应用程序的Info.plist文件来发布服务,并且在构建应用程序时安装了该文件。
But when I invoke the service from the context menu in (say) TextEdit.app (my service colours the selected text based on a certain criterion), my app gets launched, main window and everything. 但是,当我从 (例如)TextEdit.app中的上下文菜单调用服务时 (我的服务根据特定条件为选定的文本着色), 我的应用程序将启动,主窗口以及所有内容。 To make things worse, I am right-clicking on a TextEdit window that is in a secondary monitor, so my app's main window appears for an instant in the secondary monitor, then quickly repositions into the main monitor (this might have something to do with my window-centering logic, but nevermind...).
更糟糕的是,我右键单击辅助监视器中的TextEdit窗口,因此应用程序的主窗口会在辅助监视器中显示一会儿,然后快速重新定位到主监视器中(这可能与我的窗口居中逻辑,但没关系...)。
I would like to provide the service (ie, have the class that provides the service in my app instantiated and execute its method in response to the request), without my app appearing on the Dock or showing its window and main menu. 我想提供服务(即,实例化在我的应用程序中提供服务的类,并响应请求而执行其方法),而我的应用程序未出现在Dock上或未显示其窗口和主菜单。
Is this possible? 这可能吗?
Safari advertises "Search With Google", so it should be possible... Safari宣传“ Google搜索”,因此应该可以...
EDIT: Now that I think about it, "Search With Google" must launch Safari every time in order to work, so this remark does not apply. 编辑:现在,我想一想,“使用Google搜索”必须每次都启动Safari才能正常工作,因此此注释不适用。
Perhaps I can put some logic in -applicationWillFinishLaunching
/ -applicationDidFinishLaunching
to determine if the app is being launched in response to a service, and skip creating the window(notice the lack of withOptions:
in OSX)? 也许我可以在
-applicationWillFinishLaunching
/ -applicationDidFinishLaunching
添加一些逻辑,以确定是否响应服务启动应用程序,然后跳过创建窗口的步骤(注意OSX中缺少withOptions:
?
But still, that doesn't feel right. 但是,那仍然感觉不对。
It does have a lame version of withOptions:
-- NSApplicationLaunchIsDefaultLaunchKey
tells you if your application was launched to either: 它确实有跛脚版本
withOptions:
- NSApplicationLaunchIsDefaultLaunchKey
告诉你,如果你的应用程序的启动要么:
open or print a file, to perform a Service action, if the app had saved state that will be restored, or if the app launch was in some other sense not a default launch
打开或打印文件,以执行服务操作,如果应用程序已保存了要恢复的状态,或者应用程序在某种意义上不是默认启动
So in your applicationDidFinishLaunching
you can see if that key is in the notification and set to NO
. 因此,在
applicationDidFinishLaunching
您可以查看该键是否在通知中,并将其设置为NO
。 Unfortunately, the main way to tell that it is one of the possibilities other than the Service, you have to detect and record whether or not you also got an application:openFile:
, etc. 不幸的是,要知道这是除Service之外的一种可能的主要方式,您必须检测并记录是否也有一个
application:openFile:
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.