3

I'm trying to run a shell script that is in an NSTextView. I know I can use NSTask to do this, but I was wondering what the best way would be, if the script only exists in memory (the text view) but not on disk.

Should I temporarily write it to disk or somehow run it directly as string?

And what parameters do I pass to NSTask? Do I have to extract the shebang line and pass it as launchPath?

1
  • You can pass the parameters using a NSArray, see the setArguments method (or directly use launchedTaskWithLaunchPath:arguments:). Dec 5, 2012 at 23:33

1 Answer 1

5

You can execute /bin/bash as your NSTask, and setStandardInput to an NSPipe. Then write the NSString containing your script to the pipe via its fileHandleForWriting to avoid creating a file on disk.

1
  • does the NSSString has to be UTF-8 encoded? NSFileHandleForWriting takes an NSData. Apr 6, 2014 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.