查找启动OS X应用程序的文件名 [英] Find the filename that launched your OS X app

查看:164
本文介绍了查找启动OS X应用程序的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置文件信息,您可以将应用程序与特定文件类型相关联,以便在单击该应用程序时启动. 我的问题是应用程序如何发现导致其启动的文件的完整文件名.

Setting the info of a file you can associate an app with a particular file type so that when clicked the application is launched. My question is how the app can discover the full filename of the file that caused it to be launched.

推荐答案

每次打开关联文件时,不会启动Mac应用程序.它们可能已启动,但是如果它们已经在运行,则只要求它们打开另一个文件.因此,路径不会通过命令行到达应用程序.它作为消息发送到应用程序,该应用程序需要处理一个运行循环(NSRunLoop)才能接收它.

Mac apps are not launched every time an associated file is opened. They may be launched, but if they're already running, then they're just asked to open another file. So the path doesn't come to the app via the commandline. It's sent as a message to the application, which needs to process a run loop (NSRunLoop) to receive it.

在常规的Cocoa程序中,您实现一个NSApplicationDelegate并实现该方法(以ObjC命名):

In a regular Cocoa program, you implement an NSApplicationDelegate, and implement the method (in ObjC naming):

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

如果您仅支持10.13+,则首选方法已更改为:

If you only support 10.13+, the preferred method has changed to:

- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls;

如果正在运行一个NSApplication对象来接受文件,则当您的应用程序需要打开文件时,操作系统将调用此命令.通常,您可以通过调用NSApplicationMain()创建NSApplication对象,但是如果需要,可以实现自己的NSApplicationMain()版本(请参见

The OS will call this when your application needs to open a file if there's an NSApplication object running to accept it. Typically you create an NSApplication object by calling NSApplicationMain(), but you can implement your own version of NSApplicationMain() if you need to (see the NSApplication docs for details).

可以通过自己实现Apple Events并响应odoc(打开文档; { kCoreEventClass, kAEOpenDocuments })消息来在没有NSApplication或任何Objective-C的情况下响应这些打开的请求.要对此进行攻击,请参见 Apple事件编程指南.

It is possible to respond to these open requests without an NSApplication, or any Objective-C, by implementing Apple Events yourself and responding to the odoc (open document; { kCoreEventClass, kAEOpenDocuments }) message. To attack that, see The Apple Events Programming Guide.

您应该期望编写一些代码来安装事件处理程序,例如:

You should expect to write some piece of code that installs an event handler like:

err     = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
            NewAEEventHandlerUPP(OpenDocumentsAE), 0, false);
require_noerr(err, CantInstallAppleEventHandler);

然后您将实际处理OpenDocumentsAE中的消息(摘自"Open Document Apple Event的处理程序")

And then you would actually handle the message in OpenDocumentsAE (taken from "A Handler for the Open Documents Apple Event"):

static pascal OSErr OpenDocumentsAE(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon)
{
    AEDescList  docList;
    FSRef       theFSRef;
    long        index;
    long        count = 0;
    OSErr       err = AEGetParamDesc(theAppleEvent,
                            keyDirectObject, typeAEList, &docList);// 1
    require_noerr(err, CantGetDocList);// 2

    err = AECountItems(&docList, &count);// 3
    require_noerr(err, CantGetCount);

    for(index = 1; index <= count; index++)// 4
    {
        err = AEGetNthPtr(&docList, index, typeFSRef,
                        NULL, NULL, &theFSRef, sizeof(FSRef), NULL);// 5
        require_noerr(err, CantGetDocDescPtr);

        err = OpenDocument(&theFSRef);// 6
    }
    AEDisposeDesc(&docList);// 7

CantGetDocList:
CantGetCount:
CantGetDocDescPtr:
    if (err != noErr)// 8
    {
        // For handlers that expect a reply, add error information here.
    }
    return(err);// 9
}

这篇关于查找启动OS X应用程序的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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