如何使用通过 NSTask 启动的应用程序打开文档?

发布于 2024-08-26 19:21:02 字数 550 浏览 5 评论 0原文

我已经厌倦了内置的 open Mac OS X 命令,主要是因为它使用您的实际用户 ID 而不是有效用户 ID 运行程序;这导致 sudo open Foo 使用您的帐户而不是 root 帐户打开 Foo 及其关联应用程序,这让我很烦恼。所以我决定进行某种替代。

到目前为止,我已经成功了:我可以在 open -aopen -b 方式下打开任何程序,并且支持选择性等待。我正在使用 NSTask 来实现此目的。

但是,我也希望能够打开文档。据我所知,您需要使用 NSWorkspace 来执行此操作,但是使用 NSWorkspace 启动程序会导致使用您帐户的凭据而不是命令行程序的凭据启动它们证书。这正是默认的 open 工具所做的,也是我不想要的。

那么,如何在不使用 NSWorkspace 的情况下让程序请求另一个程序打开文档呢?从 NSTask 对象中,我可以获得进程 ID,但仅此而已。

I've grown tired of the built-in open Mac OS X command, mostly because it runs programs with your actual user ID instead of the effective user ID; this results in the fact sudo open Foo opens Foo with its associated application with your account instead of the root account, and it annoys me. So I decided to make some kind of replacement.

So far I've been successful: I can open any program under the open -a or open -b fashion, and support optionally waiting. I'm using NSTask for that purpose.

However, I'd like to be able to open documents too. As far as I can see, you need to use NSWorkspace for that, but using NSWorkspace to launch programs results in them being launched with your account's credentials instead of your command line program's credentials. Which is precisely what the default open tool does, and precisely what I don't want.

So, how can I have a program request that another program opens a document without using NSWorkspace? From the NSTask object, I can have the process ID, but that's about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2024-09-02 19:21:02

希望这能解决问题:

- (void)openFile:(NSString *)filePath withTask:(NSTask *)task {
    int pid = [task processIdentifier];
    NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeKernelProcessID bytes:&pid length:sizeof(pid)];

    const char *urlUTF8 = [[[NSURL fileURLWithPath:filePath] absoluteString] UTF8String];
    NSAppleEventDescriptor *urlDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeFileURL bytes:urlUTF8 length:strlen(urlUTF8)];

    NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kEventParamAppleEvent eventID:kAEOpen targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    [event setParamDescriptor:urlDescriptor forKeyword:keyDirectObject];

    OSStatus err = AESendMessage([event aeDesc], NULL, kAENoReply | kAENeverInteract, kAEDefaultTimeout);

    if (err != noErr) {
        // Error handling goes here
    }

    // Activate the application

    event = [NSAppleEventDescriptor appleEventWithEventClass:kAEMiscStandards eventID:kAEActivate targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    err = AESendMessage([event aeDesc], NULL, kAENoReply | kAENeverInteract, kAEDefaultTimeout); 
}

您可能需要启动该应用程序
使用 NSTask 然后发送
适当开放Apple Event。

实际上,您可以使用
NSTask 然后通过打开文件
NSWorkspace 一旦你知道它是
跑步?或者这会推出一个新的
您的应用程序实例
用户?

原回复:

<块引用>

open -a SomeApplication SomeFile

工作?

Hopefully this will do the trick:

- (void)openFile:(NSString *)filePath withTask:(NSTask *)task {
    int pid = [task processIdentifier];
    NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeKernelProcessID bytes:&pid length:sizeof(pid)];

    const char *urlUTF8 = [[[NSURL fileURLWithPath:filePath] absoluteString] UTF8String];
    NSAppleEventDescriptor *urlDescriptor = [NSAppleEventDescriptor descriptorWithDescriptorType:typeFileURL bytes:urlUTF8 length:strlen(urlUTF8)];

    NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kEventParamAppleEvent eventID:kAEOpen targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    [event setParamDescriptor:urlDescriptor forKeyword:keyDirectObject];

    OSStatus err = AESendMessage([event aeDesc], NULL, kAENoReply | kAENeverInteract, kAEDefaultTimeout);

    if (err != noErr) {
        // Error handling goes here
    }

    // Activate the application

    event = [NSAppleEventDescriptor appleEventWithEventClass:kAEMiscStandards eventID:kAEActivate targetDescriptor:target returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
    err = AESendMessage([event aeDesc], NULL, kAENoReply | kAENeverInteract, kAEDefaultTimeout); 
}

You may have to launch the application
using an NSTask and then send it the
appropriate open Apple Event.

Actually, can you launch using an
NSTask and then open the file via
NSWorkspace once you know it's
running? Or does that launch a new
instance of the application under your
user?

Original reply:

Does

open -a SomeApplication SomeFile

work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文