0

I'm trying to use NSTask to convert a binary plist to xml, although run into an error that I don't quite understand. If I take the command NSTask fails on and copy it to the command line it works just fine. Hopefully someone can tell me what is wrong.

NSString *defaultPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.defaults.plist"];

task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/plutil"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-convert xml1", defaultPath, nil];

// using `@"-convert", "xml1", defaultPath, nil` doesn't seem to work either.

[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

[task waitUntilExit];
[task release];

NSLog

/usr/bin/plutil (
    "-convert xml1",
    "/Users/Mira/Library/Preferences/com.defaults.plist"
)
unrecognized option: -convert xml1

2 Answers2

1

the way NSTask formats its commands for the shell can be a bit tricky. Here is a suggestion that should help.

[task setLaunchPath:@"/bin/bash"];

NSMutableArray *commandLine = [NSMutableArray new];
[commandLine addObject:@"-c"];
[commandLine addObject:@"/usr/bin/plutil -convert xml1"];
[commandLine addObject:defaultPath]; 

If this doesn't work, then make a long string of the plutil command and the file path and add it to the command line as the second object after -c.

ex:

NSString *cmd = [NSString stringWithFormat:@"/usr/bin/plutil -convert xml1 %@",defaultPath];

then add it to the array

[task setLaunchPath:@"/bin/bash"];

NSMutableArray *commandLine = [NSMutableArray new];
[commandLine addObject:@"-c"];
[commandLine addObject:cmd];

hope that helps. best of luck!

CocoaEv
  • 2,984
  • 20
  • 21
0

"-convert" and "xml1" need to be seperate strings

Jeff Laing
  • 913
  • 7
  • 13
  • I stand by my answer. The comment in the code does not have an @ in front of @"xml1". As such, you probably passed the wrong data type in (passing char* instead of NSString*). – Jeff Laing May 02 '13 at 08:28