Search code examples
objective-cmacosshellfile-association

OS X application that can run shell commands


I tried asking this question on Apple.StackExchange.com, but they redirected me back to here.

I created a custom file association with TextEdit and have it working. I need to now build an application that uses my custom file association and runs shell commands to open non proprietary applications (like TextEdit, VLC, ect). This application will be downloaded from the web.

How can I achieve all of this? It can be in any language, but preferable Objective-C


Solution

  • Looks like there is a better way to do it

    [[NSWorkspace sharedWorkspace] openFile:@"<FULL_PATH_TO_YOUR_ASSOSIATED_FILE>"];
    

    From the documentation for openFile

    Opens the specified file specified using the default app associated with its type.


    Less elegant way to do the same is following :

    Check the location of the open command on your system like whereis open and use the found location for the executable to replace in the code below

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"<FULL_PATH_TO_OPEN_COMMAND>"];
    [task setArguments:@[ @"<FULL_PATH_TO_YOUR_ASSOSIATED_FILE>"]];
    [task launch];