Search code examples
objective-cmacoslaunching-application

Launch Application Using Target File


Using NSWorkSpace launchApplication and all is fine if using an application file, but what if one would like to launch an application using a target file like untitled.rtf

[[NSWorkspace sharedWorkspace] launchApplication:selection]

Solution

  • Use -[NSWorkspace openFile:WithApplication:] like so:

    [[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"
        withApplication:@"TextEdit"];
    

    Or, if one just wants to open a file with the default application for that file, use -[NSWorkspace openFile:] like so:

    [[NSWorkspace sharedWorkspace] openFile:@"/Myfiles/untitled.rtf"];
    

    The be sure to check the documentation for NSWorkspace for details and other related methods.