简体   繁体   English

如何在可可应用程序中读取macOS桌面文件

[英]How to read macOS desktop file in cocoa app

I want to make cocoa app execute .sh file or other Operation, this my code: 我想让可可应用程序执行.sh文件或其他操作,这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]);
}

- (NSString *)cmd:(NSString *)cmd
{
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/bash"];
    NSArray *arguments = [NSArray arrayWithObjects: @"-c", cmd, nil];
    [task setArguments: arguments];
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    NSData *data = [file readDataToEndOfFile];
    return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}

but log error: 但是日志错误:

ls: .: Operation not permitted

how to do that?I search about macOS permit,Do I need use SMJobBless ? 我要搜索macOS许可证,我需要使用SMJobBless吗?

change 更改

 NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]);

with

NSLog(@"%@",[self cmd:@"cd ~/Desktop; ls;"]);

Sandbox applications aren't allowed to read outside the application folder. 沙箱应用程序不允许在应用程序文件夹之外读取。 So in order for your script to work you need to turn off Sandbox. 因此,为了使脚本正常工作,您需要关闭沙盒。

Also you need to change the path like Sangram S. mentioned or set the current directory to the home folder: 另外,您需要更改提到的Sangram S.之类的路径,或将当前目录设置为home文件夹:

[task setCurrentDirectoryPath:NSHomeDirectory()];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM