Passing arguments to Processing sketches

I recently received an email asking about how to pass arguments when launching a processing sketch from the command line. I hadnt ever done such a thing, but after some poking came up with an easy, though not exactly robust, solution. This example sets the value of the r, g, and b variables and uses them to set the background color.

The main method just clones the array of arguments into aglobalvariable of the sketch. That variable needs to be declared static so it can be accessed by the main method, which is also static. Also, make sure that the string in PApplet.main(new String[] {passing_arguments}); matches the name of your sketch. Next, just parse the array of arguments like you would anything else inside setup. Its a good idea to make sure that you have any arguments at all (by checking for != null) and that youve got the number of arguments you expect.

Next you export the sketch (File > Export) then you can execute the .jar file from the command line using: java -jar passing_arguments.jar 255 255 1 where passing_arguments.jar is the name of the jar file, and 255 255 1 are the values you want to use for the r, g, and b variables.

If you need something more robust, or need to use named switches, check out this article on using the Apache Commons CLI Library .

Originally posted at http://brysonian.com/2009/04/19/passing-arguments-to-processing-sketches/

0 comments