How can a Mac app determine the method used to launch it?

不羁岁月 提交于 2019-11-30 21:52:53

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.

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

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.

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!