OSX equivalent of ShellExecute?

前端 未结 3 1739
北荒
北荒 2020-12-06 12:09

I\'ve got a C++ app that I\'m porting from Win32 to OSX. I\'d like to be able to launch arbitrary files as if the user opened them. This is easy on windows using ShellExec

3条回答
  •  借酒劲吻你
    2020-12-06 12:29

    Another suggestion if you're working with cocoa:

    [[NSWorkspace sharedWorkspace] openFile:@"pathToFile"];
    

    There are other similar methods in NSWorkspace as well. For example to open an application or a URL:

    [[NSWorkspace sharedWorkspace] launchApplication:@"pathToApplication"];
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"URL"]];
    

    Working through [NSWorkspace sharedWorkspace] can give you a bit more control than the standard C system() call.

    Edit: Note that you can use Objective-C++ to mix C++ code with Objective-C code and thereby call cocoa methods.

提交回复
热议问题