1

This question is in reference to my earlier post (which was put on hold). https://stackoverflow.com/questions/24091606/push-text-from-java-applet-to-processing-sketch

I have developed my own text editor with multiple text editing areas in javaFX.

I would like to allow the user to write multiple Processing code/sketches, then hit a "Run All" button to run all sketches in parallel.

I am trying to find a way to automate the steps of :

for each text area in my editor
 { 
   -copy text
   -make a new processing sketch
   -paste into the processing sketch just created
   -run the process sketch
 }

This is probably not the right approach to do it, but even if it is, I am not sure how to implement it. I looked at the examples here on using Robot. Can a java program "type" into another windows program like notepad

but it does not seem to work for me.

Any ideas on how to achieve this?

I am currently on Windows 7, but would like to develop a solution that is cross platform if possible.

Community
  • 1
  • 1
melkhaldi
  • 899
  • 2
  • 19
  • 40

3 Answers3

1

Your idea about having your editor GUI interact with the Processing GUI is... messy. And unreliable. GUIs are meant for human interaction, not automatic interaction, and there's usually a better way to do what you want.

The processing-java program included in the Processing download can be run from the command line to compile and run Processing sketches. A Processing sketch is a bit more than just a source file, though-- it should be in a certain folder structure with some metadata. From an automatic perspective, the process goes something like:

  1. Have your editor create a folder called, for example, Bubbles inside your sketchbook folder.
  2. Have your editor create and fill a text file called, in this example, Bubbles.pde. It should be in the Bubbles folder alongside a file called sketch.properties that contains information about the sketch. Check some sketches on your computer for its contents. Presumably you'd use FileWriter, etc.
  3. Compile and run the newly created file. The command will be something like:

    /path/to/processing-java --force --run --sketch=/path/to/sketchbook/Bubbles --output=/path/to/working/folder/Bubbles

--force causes it to overwrite older versions of the compiled bytecode

--run causes the compiled sketch to be executed after compilation

--sketch specifies the name of the sketch to compile. This should be the path to the Bubbles folder, not the sketch itself

--output specifies where to put the compiled bytecodes. This doesn't need to be anywhere in particular unless you wish to get at the executables later. You probably want to keep track of how big this working folder gets, as compiling tons of sketches will fill it up fast.

This is the shell command that needs to be run. To do this from your java app, you'd probably do something like

Runtime.getRuntime().exec(pjPath + "--force --run --sketch=" + bubblesPath + " --output=" + outputPath);

I've left out some details (the file writing/folder creation, etc) because that's somewhat specific to your app and can be found elsewhere on the internet. If you're on Windows, you'll need to adjust your paths accordingly (backslashes, drive letters, etc) as I've used the Linux conventions here.

kevinsa5
  • 3,301
  • 2
  • 25
  • 28
  • Thanks Kevin! from your previous post, I could run processing from a command line. will try the shell, looks straight forward. Is there a less messy solution-- one that does not involve writing code entirely in processing IDE? – melkhaldi Jun 09 '14 at 05:41
  • This solution never involves the processing IDE! Your editor invokes a shell command, which runs behind the scenes-- the processing editor is never actually open. – kevinsa5 Jun 09 '14 at 05:42
  • oh ok. You were referring to the cut copy paste approach i mentioned in the question. Thanks! I will give this a shot and report back. – melkhaldi Jun 09 '14 at 05:52
  • Hi Kevin, I created a file and a sketch properties as you suggested. but i am getting an error. It is finding the file. i used a directory chooser (and tried with a file chooser also) to get the path correctly. but it does not seem to work. this is the error: – melkhaldi Jun 09 '14 at 15:35
  • The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:386) at java.lang.ProcessImpl.start(ProcessImpl.java:137) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023) – melkhaldi Jun 09 '14 at 15:35
0

This is what I ended up doing.

to make it OS independent, I ask the user for the processing-java file and the sketch folder. As for the output folder, I create a temp folder inside the same sketch folder using a java File.separator, so this should make it OS independent I guess. Code below(I know it is very verbose variable names):

fileChooser.setTitle("Select The Processing-Java Program executable/runnable/appication!");
processingJavaProrgramFile = fileChooser.showOpenDialog(stage);
if (processingJavaProrgramFile != null) {
    processingJavaProrgramPath = processingJavaProrgramFile.getPath();
}

directoryChooser.setTitle("Select The Folder of The Processing Sketch You Want To Run!");
processingSketchDir = directoryChooser.showDialog(stage);
if (processingSketchDir != null) {
  sketchPath = processingSketchDir.getPath();
}

Runtime.getRuntime().exec(processingJavaProrgramPath + " --force --run --sketch=" + sketchPath + "  --output=" + sketchPath+File.separator+"temp");
melkhaldi
  • 899
  • 2
  • 19
  • 40
  • Cool. Just so you know, your variable names are misspelled -- processingJavaProrgramFile and processingJavaProrgramPath have an extra R :) – kevinsa5 Jun 10 '14 at 06:14
0

I came across an issue, which I finally figured out.

Basically, if the name of the output folder has a white space in it, the sketch won't run. This is happening on Windows 7. I don't know if this is a bug, or a command line issue, or if this is by design. Also, I don't know if affects OSX or Linux distributions.

What I ended up doing, is check if the user selected a folder name that has white space in it, if so, I trim the name and make a new folder in the same directory and notify the user.

So for the execute command to work, output folder should be a trimmed name. (I believe it might be true also for sketch names and sketch folder names).

melkhaldi
  • 899
  • 2
  • 19
  • 40