Objective c 如何在cocoa应用程序中读取macOS桌面文件
我想让cocoa app执行.sh文件或其他操作, 这是我的代码:Objective c 如何在cocoa应用程序中读取macOS桌面文件,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我想让cocoa app执行.sh文件或其他操作, 这是我的代码: - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]); } - (NSString *)cmd:(NSString *)cmd { NSTask *task = [[NSTask alloc] init]; [task setLaunchPath: @"/bin/bash"];
- (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];
}
但日志错误:
ls: .: Operation not permitted
怎么做?我搜索macOS许可证,我需要使用吗?更改
NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]);
与
改变
NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]);
与
不允许沙盒应用程序读取应用程序文件夹之外的内容。因此,为了让脚本正常工作,您需要关闭沙盒 您还需要更改上述Sangram S.之类的路径,或将当前目录设置为主文件夹:
[task setCurrentDirectoryPath:NSHomeDirectory()];
不允许沙盒应用程序读取应用程序文件夹之外的内容。因此,为了让脚本正常工作,您需要关闭沙盒 您还需要更改上述Sangram S.之类的路径,或将当前目录设置为主文件夹:
[task setCurrentDirectoryPath:NSHomeDirectory()];
谢谢,但不工作,日志错误:
ls::不允许操作
谢谢,但不工作,日志错误:ls::不允许操作