[英]How to read macOS desktop file in 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"];
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許可證,我需要使用SMJobBless嗎?
更改
NSLog(@"%@",[self cmd:@"cd Desktop;ls;"]);
與
NSLog(@"%@",[self cmd:@"cd ~/Desktop; ls;"]);
沙箱應用程序不允許在應用程序文件夾之外讀取。 因此,為了使腳本正常工作,您需要關閉沙盒。
另外,您需要更改提到的Sangram S.之類的路徑,或將當前目錄設置為home文件夾:
[task setCurrentDirectoryPath:NSHomeDirectory()];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.