Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HiRez

macrumors 603
Original poster
Jan 6, 2004
6,250
2,576
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".

or

*** 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.

path.gif
 

cMacSW

macrumors regular
Mar 20, 2006
180
0
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];

Kevin
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.