Mac 应用程序如何确定用于启动它的方法? [英] How can a Mac app determine the method used to launch it?

查看:23
本文介绍了Mac 应用程序如何确定用于启动它的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Mac OS X 应用程序,它也是一个协议处理程序(就像,例如,Safari 是 HTTP 和 HTTPS 协议的一个协议处理程序).因此,当用户在任何应用程序中单击 myscheme://some-kind-of-info 形式的链接时,我的应用程序将启动以处理该链接.

I have a Mac OS X application that is also a protocol handler (just as, for example, Safari is a protocol handler for the HTTP and HTTPS protocols). So when a user clicks a link of the form myscheme://some-kind-of-info in any application at all, my application launches to handle the link.

现在我需要能够确定应用程序是通过这样的链接点击启动的,还是通过任何其他方法启动的.换句话说,它是通过除链接点击之外的任何方法启动的.(在这些情况下,我希望应用程序保持打开状态,但如果它是通过链接启动的,它应该退出并忽略该链接.这样它只在已经运行时运行.)

Now I need to be able to determine if the application was launched by such a link click, or if it was launched by any other method. In other words, it was launched by any method besides a link click. (In those cases, I want the app to stay open, but if it was launched by a link it should quit and ignore the link. This way it only operates when already running.)

在启动时应用程序内是否有某种方法可以自省并发现它是通过标准方法而不是通过 AppleScript GetURL 事件启动的?我想通过记录在案的方法找出答案,而不是 - 例如 - 只是让我的应用程序在运行半秒后才打开这些链接.

Is there some way within the app at startup to introspect and find out that it was launched by a standard method rather than by an AppleScript GetURL event? I'd like to find out through a documented method, rather than - for example - just have my app only open these links after it's been running for a half a second.

推荐答案

您可以为发布时可能遇到的每个 Apple Events 注册一个处理程序,并记下您首先收到哪一个.

You can register a handler for each of the possible Apple Events you'll get on launch, and make note of which one you receive first.

  • 如果应用程序在没有文档的情况下启动,您将获得 kAEOpenApplication.
  • 如果它与文档一起启动,您将获得 kAEOpenDocuments(或kAEPrintDocuments).
  • 如果它是使用 URL 启动的,那么(显然)您将获得 kAEGetURL.
  • If the application is launched without documents, you'll get kAEOpenApplication.
  • If it's launched with documents, you'll get kAEOpenDocuments (or kAEPrintDocuments).
  • If it's launched with a URL, then (obviously) you'll get kAEGetURL.

还有 kAEOpenContents,但我无法在我的测试应用程序中轻松触发它;无论如何,它可能值得支持.

There's also kAEOpenContents, but I wasn't able to trigger it easily in my test app; it's probably worth supporting no matter what.

Cocoa 应用程序如何处理 Apple 事件 记录所有这些内容.

不过,这里有一个错误;它说 AppleScript 的启动"将发送 kAEOpenApplication.它不会,它会发送 ascr/noop(kASAppleScriptSuite/kASLaunchEvent,定义在 ASRegistry.h).我无法使用通常的 Cocoa 事件处理程序机制来捕获此事件,因此您可能需要在那里进行更多挖掘.

There is one error in there, though; it says that AppleScript's "launch" will send kAEOpenApplication. It won't, it'll send ascr/noop (kASAppleScriptSuite/kASLaunchEvent, defined in ASRegistry.h). I couldn't get the usual Cocoa event handler mechanism to trap this event, so you may need to do some more digging there.

检查事件是否在启动时发送的一种方法是在应用程序委托的 applicationWillFinishLaunching: 方法中注册事件处理程序;它们应该在 applicationDidFinishLaunching: 被调用时交付.使用该方法,您可能检查kAEGetURL.

One way you can check if the event is sent at launch is to register the event handlers in your application delegate's applicationWillFinishLaunching: method; they should deliver by the time applicationDidFinishLaunching: is invoked. With that method, you could potentially only check for kAEGetURL.

这篇关于Mac 应用程序如何确定用于启动它的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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