简体   繁体   English

提供OSX服务而不启动应用程序?

[英]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.

相关问题 如何在 OSX 中调试 XPC 服务和客户端应用程序之间的通信 - How to debug communication between XPC service and client app in OSX 如何在没有Facebook Kit的情况下在OSX应用程序内的Facebook上共享? - how to share on facebook within OSX app, without facebook Kit? 在不使用iOS应用程序中的任何特定URL的情况下启动Safari App - Launching Safari App without using any specific url from an iOS App osx-如何在没有可见UI的情况下实施OSX服务? - osx - How to implement a OSX service with no visible UI? 将数据从watchKit发送到iPhone并获得回调,而无需启动iOS App - Send data from watchKit to iPhone and got callback without launching iOS App 在不启动应用程序的情况下显示主屏幕操作,3DTouch主屏幕快速操作 - Display a home screen action without launching the app, 3DTouch Home Screen Quick Actions 从JSON Web服务下载图像时下载的图像时出现OSX App无法识别的选择器错误 - OSX App Unrecognized Selector error when downloaded image from a when downloaded image from a JSON web service OSX应用程序中的“不允许打印” - “Printing Not Allowed” in OSX app 从C ++在Mac OSX上启动一个打开的对话框 - Launching an open dialog on Mac OSX from C++ 数据加载-在tableview上启动应用程序 - data loading - app launching on tableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM