提供OSX服务而不启动应用程序? [英] Provide an OSX Service Without Launching the App?

查看:173
本文介绍了提供OSX服务而不启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功实施了匿名服务(仅限后台应用,扩展名为.service),并使其正常工作(请参见此问题),基于 Apple的文档和其他网络教程。



现在,我想从一个现有的,窗口GUI应用程序。



我设置了我的应用程序的Info.plist文件来宣传服务,并在我构建应用程序时安装。 p>

但是当我从上下文菜单中调用服务(例如)TextEdit.app(我的服务根据一定的标准对选定的文本着色) ,我的应用程序被启动,主窗口和一切。更糟糕的是,我右键单击一个TextEdit窗口在辅助显示器,所以我的应用程序的主窗口出现一瞬间在辅助监视器,然后快速重新定位到主监视器(这可能与我的窗口居中逻辑有关,但永远不会...)。



我想提供服务(即,在我的应用程序中提供服务的类
被实例化并响应请求执行它的方法),而我的应用程序不显示在Dock上或显示其窗口和主菜单。 >

这可能吗? Safari广告使用Google搜索,因此应该可以...



考虑一下,使用Google搜索必须每次都启动Safari才能正常工作,因此此评论不适用。



也许我可以在 -applicationWillFinishLaunching / -applicationDidFinishLaunching 以确定应用程序是否正在响应服务而启动,并跳过创建窗口在OSX中缺少 withOptions:)?



但是,这还是不对劲。

解决方案

它有一个不完整的版本的 withOptions: - NSApplicationLaunchIsDefaultLaunchKey 告诉你是否启动您的应用程序: p>


打开或打印文件,执行服务操作,如果应用程序保存了要恢复的状态,或者应用程序启动时一些其他意义不是默认启动


因此,在 applicationDidFinishLaunching 如果该键位于通知中并设置为 NO 。不幸的是,告诉它是除服务之外的其他可能性之一的主要方式,你必须检测和记录是否还有一个应用程序:openFile:等等。


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.

Now, I want to advertise a service from an existing, single-window GUI app that I have.

I have setup the Info.plist file of my app to advertise the service, and it gets installed when I build the app.

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. 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...).

I would like to provide the service (i.e., 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.

Is this possible? Safari advertises "Search With Google", so it should be possible...

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.

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)?

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:

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. 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.

这篇关于提供OSX服务而不启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆