使用 NSTask 将系统分析器信息保存在 .spx 文件中

发布于 2024-08-22 17:21:22 字数 866 浏览 2 评论 0原文

在 Cocoa 中,我试图实现一个按钮,当用户单击该按钮时,将捕获系统分析器报告并将其粘贴到桌面上。

代码

 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] 有效并且它会在控制台窗口中删除整个系统分析器输出。

关于为什么这不起作用或如何更好地实施这一点有什么建议吗?我试图避免使用 shell 脚本或 AppleScript 来获取系统分析器。如果没有任何作用,那将是我的最终选择。 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-08-29 17:21:22
NSArray *args = [NSArray arrayWithObjects:@"-xml",@"-detailLevel",@"full",@">", @"~/Desktop/Profiler.spx",nil] ;

这是行不通的,因为您没有通过 shell,而 > 是一个 shell 操作符。 (此外,~ 并不特殊,除非您使用 stringByExpandingTildeInPath 展开它。)

创建一个 NSFileHandle 以写入该分析器 .spx 文件,确保使用完整的绝对路径,而不是波浪号缩写的路径。然后, 将 NSFileHandle 设置为任务的标准输出。这本质上就是当您在 shell 中使用 > 运算符时 shell 所做的事情。

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.

南笙 2024-08-29 17:21:22

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

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];
坐在坟头思考人生 2024-08-29 17:21:22

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

Create an NSPipe, send [taskDebug setStandardOutput: myPipe] and read from the pipe's file handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文