How can I run a command in terminal from within processing?

edited October 2017 in How To...

I have a simple command I need to run in my mac terminal:
python /Users/J/Desktop/sketch1/data/scripts/segmenting.py
How can I make processing run this command in the terminal instead of me opening the terminal and tying it in?
I thought about saving the terminal output as a file to call it, but not sure about that.
Thanks for the help

Answers

  • edited October 2017

    From the launch reference:

    If you're trying to run command line functions directly, use the exec() function instead (see below).

    exec() In the Processing API:

    Pass a set of arguments directly to the command line. Uses Java's Runtime.exec() method. This is different from the launch() method, which uses the operating system's launcher to open the files. It's always a good idea to use a full path to the executable here.

    exec("/usr/bin/say", "welcome to the command line");
    

    Edit: removed voice argument from example above as per https://github.com/processing/processing/pull/5282

  • So for example in my mac terminal I am writing:
    python /Users/J/Desktop/sketch1/data/scripts/segmentation_pipeline.py /Users/J/Desktop/sketch1/data/images/test2.jpg
    How do I call terminal to write this from processing then?

  • edited October 2017

    It should be like in the example:

    exec("python", "/Users/J/Desktop/sketch1/data/scripts/segmentation_pipeline.py ", "/Users/J/Desktop/sketch1/data/images/test2.jpg");
    

    Depending on how your path is set up you may need to provide the full path to the python executable (check with which python) e.g. /usr/bin/python

  • edited October 2017

    I get this little grey box that pops up, but I dont think the script worked because the script creates a folder and its nowhere to be found..

    /Users/J/anaconda3/bin/python3.6
    is where my python is at...how do I include the full path ?

  • /Users/J/anaconda3/bin/python3.6/python (or python3, or whatever the binary is called)

  • Yeah im getting nothing at all. Is there a way of opening terminal instead of python? If I can open terminal and automatically put this line in, it runs perfectly in my terminal.

  • Try using the "if you want to wait until it is completed" code example from the exec() JavaDoc so that you can read the return text in the console.

    Here is a combined example that attempts both test2 output ( no console capture) and test3 output (console capture):

    void setup(){
      noLoop();
    }
    void draw(){
      doExec();
      exec("python", "segmentation_pipeline.py ", "test2.jpg");
    }
    
    void doExec(){
    Process p = exec("python", "segmentation_pipeline.py ", "test3.jpg");
     try {
       int result = p.waitFor();
       println("the process returned " + result);
     } catch (InterruptedException e) { }
    }
    

    I have not included full paths for any of the exec arguments.

    You also might want to consider simplifying things by making the script file and output local to your sketch, at least for the purposes of testing.

  • This works....but can I not just say in processing "open this .txt file in terminal.app?" How do you open external applications and processes?

  • edited October 2017

    If you want to open TextEdit.app, use Processing's launch().

    If you want to run a shell command (like in Terminal.app), use exec().

    I don't believe you can "open this .txt file in terminal.app" ... because Terminal.app doesn't work that way -- it isn't a document editor. For example, it doesn't have an open command in its menu, and dragging a text file onto it won't do what you think.

  • Ok, I have a different idea...I looked up:https://macosx.com/threads/text-file-executable.312801/
    and made it an executable file, so now its just one file called "image" which contains the same: /Users/J/Desktop/sketch1/data/scripts/segmentation_pipeline.py /Users/J/Desktop/sketch1/data/images/test2.jpg
    how can I call this file?

  • edited October 2017

    launch("/Users/J/Desktop/image");
    Works! Success!

Sign In or Register to comment.