“Operation not permitted” error when running grep from Mac app in Catalina

Hi everyone, after Catalina upgrade this simple grep call from a Mac app started to fail, while running it from Xcode I'm getting this error in the Xcode console: "

.../file.txt: Operation not permitted"
.


It's kind of obvious that this is because of the security changes in Catalina, I've been looking for a while, but haven't been able to make it work, any idea on how to solve it?


Thanks in advance, this is the code I'm using:


- (void)simpleGrep{
  NSPipe *pipe = [NSPipe pipe];
  NSFileHandle *file = pipe.fileHandleForReading;

  NSTask *task = [[NSTask alloc] init];
  task.launchPath = @"/usr/bin/grep";
  task.arguments = @[@"word", @"/Users/xxxxxxx/Documents/SampleFiles/file.txt"];
  task.standardOutput = pipe;

  [task launch];

  NSData *data = [file readDataToEndOfFile];
  [file closeFile];

  NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];  
  NSLog (@"Finish with output: %@", output);
}


The NSLog prints "Finish with output: (null)".

Replies

The answer here depends on whether your app is sandboxed or not (see this thread for some more background on that).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"