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

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

问题描述

我有一个Mac OS X的应用程序,这也是协议处理程序(就像,例如,Safari浏览器是HTTP和HTTPS协议的协议处理程序)。因此,当用户点击一个形式myscheme的链接:在任何应用//一些实物-OF-信息可言,我的应用程序启动处理链接

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.

推荐答案

您可以为每个可能的苹果事件的注册处理程序,你会得到推出,并注意您收到第一其中之一。

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.

<一个href=\"http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/ScriptableCocoaApplications/SApps_handle_AEs/SAppsHandleAEs.html\"相对=nofollow> Cocoa应用程序如何处理Apple事件记录所有的这些东西。

How Cocoa Applications Handle Apple Events documents all of this stuff.

有一个错误在那里,虽然;它说的AppleScript的发射将发送 kAEOpenApplication 。它不会,它会发送 ASCR /空操作 kASAppleScriptSuite / kASLaunchEvent ASRegistry.h )。我可能不是一般的可可事件处理机制,让捕获该事件,所以你可能需要做更多的一些挖在那里。

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天全站免登陆