Saving System Profiler info in a .spx file using NSTask
Question
In Cocoa, I am trying to implement a button, which when the user clicks on will capture the System profiler report and paste it on the Desktop.
Code
NSTask *taskDebug;
NSPipe *pipeDebug;
taskDebug = [[NSTask alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(taskFinished:) name:NSTaskDidTerminateNotification object:taskDebug];
[profilerButton setTitle:@"Please Wait"];
[profilerButton setEnabled:NO];
[taskDebug setLaunchPath: @"/usr/sbin/system_profiler"];
NSArray *args = [NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",@">", @"
~/Desktop/Profiler.spx",nil];
[taskDebug setArguments:args];
[taskDebug launch];
But this does not save the file to the Desktop. Having NSArray *args = [NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",nil] works and it drops the whole sys profiler output in the Console Window.
Any tips on why this does not work or how to better implement this ? I am trying to refrain from using a shell script or APpleScript to get the system profiler. If nothing work's that would be my final option. Thanks in advance.
Solution 2
This got it done ( thanks to Peter and Costique)
[taskDebug setLaunchPath: @"/usr/sbin/system_profiler"];
NSArray *args = [NSArray arrayWithObjects:@"-xml",@"- detailLevel",@"full",nil];
[taskDebug setArguments:args];
[[NSFileManager defaultManager] createFileAtPath: [pathToFile stringByExpandingTildeInPath] contents: nil attributes: nil];
outFile = [ NSFileHandle fileHandleForWritingAtPath:[pathToFile stringByExpandingTildeInPath]];
[taskDebug setStandardOutput:outFile];
[taskDebug launch];
OTHER TIPS
NSArray *args = [NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",@">", @"~/Desktop/Profiler.spx",nil];
That won't work because you aren't going through the shell, and >
is a shell operator. (Also, ~
isn't special except when you expand it using stringByExpandingTildeInPath
.)
Create an NSFileHandle for writing to that Profiler.spx file, making sure to use the full absolute path, not the tilde-abbreviated path. Then, set that NSFileHandle as the task's standard output. This is essentially what the shell does when you use a >
operator in it.
Create an NSPipe, send [taskDebug setStandardOutput: myPipe] and read from the pipe's file handle.