How can I pass arguments to an exported application on OSX?
This is my sketch:
void setup() {
size(500, 500);
}
void draw() {
background(0);
fill(255);
if (args != null) {
for (String s : args) {
text(s, 50, 50);
}
}
}
How can I pass arguments to an exported application on OSX?
This is my sketch:
void setup() {
size(500, 500);
}
void draw() {
background(0);
fill(255);
if (args != null) {
for (String s : args) {
text(s, 50, 50);
}
}
}
I have seen those but none of them give an answer.
Hmm. I know how to do this for a .pde sketch from the command line with processing-java
, using --run
to pass arguments. Are you already able to pass command line arguments to your non-exported sketch when you run it from processing-java in this way?
I do not know how to do this with an exported application. When exporting your application (MacOS, Java included) to TempArgTest, open
does not work:
open application.macosx/TempArgTest.app --args foo bar
open -a “pwd
/application.macosx/TempArgTest.app/” --args foo bar
…and neither does directly invoking the binary:
./application.macosx/TempArgTest.app/Contents/MacOS/TempArgTest foo bar
In the past I have used a sketch (non-exported) with two different behaviors – one if run in “manual mode” from Processing PDE, one if run in “automatic mode” from the command line, with arguments. I’m not sure what your use case is, but I am including a demo of that below in case it is useful.
/**
* CommandLineDetect
* 2017-09-21 Jeremy Douglass - Processing 3.3.6
* Was a sketch run from PDE, or from the command line?
* If from the command line, what are its arguments and their values?
*
* Invoke it:
* 1. from the command line above the sketch folder, with no arguments:
* processing-java --sketch=`pwd`/CommandLineDetect --run
* 2. ...or with arguments:
* processing-java --sketch=`pwd`/CommandLineDetect --run foo bar=baz lorem="ipsum dolor sit amet, consectetur adipiscing elit" etc
* 3. or form PDE
*
* The sketch relies on the --external flag to detect a PDE launch, as per:
* - processing.github.io/processing-javadocs/core/processing/core/PApplet.html#main-java.lang.String:A-
*
* The command line passes in a String[] of args. If you wish to arguments of the:
* - form foo=bar
* - lorem="ipsum dolor"
*
* Related forum discussions:
* - forum.processing.org/two/discussion/23999/detect-command-line-invocation-of-a-sketch
* - forum.processing.org/two/discussion/23924/handling-command-line-arguments-in-a-sketch
* - forum.processing.org/two/discussion/6457/how-to-use-arguments-with-an-application-built-with-processing
*/
boolean PDE;
StringDict argDict = new StringDict();
void setup() {
// get invocation string
String cmd = System.getProperty("sun.java.command");
// check if launched from PDE
if (cmd.contains("--external")) {
PDE=true;
} else {
PDE=false;
}
// if there are command line arguments, print them:
println(args == null ? "null" : args);
// load command line arguments
if (args != null) {
for (String arg : args) {
String[] entry = split(arg, "=");
if (entry.length == 1) {
argDict.set(entry[0], "");
}
if (entry.length == 2) {
argDict.set(entry[0], entry[1]);
}
}
}
// set default if specific arg not present
if (!argDict.hasKey("xyzzy")) {
println("Setting default for xyzzy");
// ...do something
}
// demonstrate using arguments
for (String arg : argDict.keyArray()) {
switch(arg) {
case "foo":
// key only arg
println("mode:", arg);
// ...do something
break;
case "bar":
// key=value arg
println("Setting", arg, "=", argDict.get(arg));
// ...do something
break;
case "lorem":
// parsed value
String parsed = join(sort(argDict.get(arg).split(" ")), " ");
println("Sorting", arg, "=", parsed);
// ...do something
break;
}
}
exit();
}
Thanks, I forgot why I needed it but It will come back some day.