Karim
Karim

Reputation: 402

How to read macOS desktop file in cocoa app

I want to make cocoa app execute .sh file or other Operation, this my code:

- (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 ?

Upvotes: 0

Views: 179

Answers (2)

Maarten Foukhar
Maarten Foukhar

Reputation: 359

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:

[task setCurrentDirectoryPath:NSHomeDirectory()];

Upvotes: 1

Sangram Shivankar
Sangram Shivankar

Reputation: 3573

change

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

with

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

Upvotes: 1

Related Questions