Problem passing paths with spaces to NSTask

Discussion in 'Mac Programming' started by HiRez, Nov 20, 2006.

  1. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Hi guys, I'm trying to launch an app from Cocoa using NSTask, but I have a bedeviling problem. The application contains spaces (in this case I cannot change the name of the app so that is not an option) and NSTask is crashing when I pass a path containing spaces to it. I've tried everything I can think of including:

    [serverTask setLaunchPath:mad:"/Applications/MAXON/CINEMA 4D R9/C4D Server PPC"];
    [serverTask setLaunchPath:mad:"/Applications/MAXON/CINEMA\ 4D\ R9/C4D\ Server\ PPC"];
    [serverTask setLaunchPath:mad:"/Applications/MAXON/CINEMA%204D%20R9/C4D%20Server%20PPC"];

    and all those variations with

    [serverTask setLaunchPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:mad:"C4D Server PPC"]];

    The errors I get are either

    *** NSTask: Task create for path /Applications/MAXON/CINEMA 4D R9/C4D Server PPC failed: 8, "Exec format error".


    *** Uncaught exception: <NSInvalidArgumentException>
    *** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value

    which I don't understand at all since I'm not even using any dictionaries or collections of any kind. The code is extremely simple, it creates a task, sets the launch path, and launches it, that's it. Additionally, I know the rest of my code is fine because when I call

    [serverTask setLaunchPath:mad:"/bin/ls"];

    instead, it works fine. And I am quite sure I have the correct path and file name to the application (I started by dragging the app into Terminal and copied the path) -- see attachment below. Finally, launching the app from Terminal using "open" works fine as well. What do I need to do to format this path correctly? I know there has to be a way but I'm fresh out of ideas and I can't find any answers in the docs or from Google.

  2. cMacSW macrumors regular

    Mar 20, 2006
    Try defining your path like this:

    NSString *appPath = [NSString stringWithFormat:mad:"\"/Applications/MAXON/CINEMA 4D R9/C4D Server PPC\""];

    This will create the path with quotes around it, so the spaces will be inside the quoted string.

    Then call setLaunchPath like this:

    [serverTask setLaunchPath:appPath];


Share This Page