Using the NSTask class, your program can run another program as a subprocess and can monitor that program’s execution. An NSTask object creates a separate executable entity; it differs from NSThread in that it does not share memory space with the process that creates it.
I think it is possible to get info from the running process (beyond just the fact that it's still running) through
stdout
, but I haven't tried that yet. Here we just launch a task in three different ways, and use Objective-C for a change of pace. The question is more about permissions, and paths and such.First, pass
/usr/bin/python
as the launchPath and an array with two arguments: script0.py
(on my Desktop), and the string "to me". Notably, the script file is not executable or even readable by anyone but the user (mode is -rwx------). We set up the task and launch it in one line.script0.py
task0.m
Notice that the argument 'to me' came in as a single value. We didn't get this:
In the second method we do the shebang or hashbang thing,
script1.py
is the same as script0.py
except we add this as the first line: #! /usr/bin/env python
and we make it executable first..The code for
task1.m
The third method is similar to the first, except we set up the task before launching. This could be useful to set the environment, for example, or change StandardError, etc. We grab the user's environment (post here) and use that. You could change the working directory with
setCurrentDirectoryPath
, but I didn't try it yet. We re-use script0.py
I see I posted about this before (here). It's so long ago I forgot! (Thanks, Google). In that case, we were in PyObjC and called a C program, so it's a little different. I haven't checked to see that the old code still runs with the current PyObjC. I'll try to do that.