使用NSTask在.spx文件中保存系统概述文件信息

时间:2010-02-26 17:09:09

标签: cocoa nstask

在Cocoa中,我正在尝试实现一个按钮,当用户点击该按钮时,将捕获System Profiler报告并将其粘贴到桌面上。

代码

 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];

但是这不会将文件保存到桌面。有 NSArray * args = [NSArray arrayWithObjects:@“ - xml”,@“ - detailLevel”,@“full”,nil] 有效,它会在控制台窗口中删除整个sys Profiler输出。

有关为什么不起作用或如何更好地实现此功能的任何提示?我试图避免使用shell脚本或APpleScript来获取系统探查器。如果没有任何工作,那将是我的最终选择。 提前谢谢。

3 个答案:

答案 0 :(得分:1)

NSArray *args = [NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",@">", @"~/Desktop/Profiler.spx",nil];

这不起作用,因为你没有通过shell,>是一个shell运算符。 (另外,~并不特殊,除非使用stringByExpandingTildeInPath展开它。)

Create an NSFileHandle for writing到该Profiler.spx文件,确保使用完整的绝对路径,而不是波浪号缩写的路径。然后,set that NSFileHandle as the task's standard output。这基本上是shell在其中使用>运算符时所执行的操作。

答案 1 :(得分:1)

这完成了(感谢Peter和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];

答案 2 :(得分:0)

创建一个NSPipe,发送[taskDebug setStandardOutput:myPipe]并从管道的文件句柄中读取。

相关问题