Loading...
Logo
Processing Forum
I've noticed on my Mac (running Lion) that if I use selectInput() or selectOutput() and hit the OK button, the file path is loaded properly, but if I hit enter/return (which I usually do) that nothing is returned.  It doesn't return a null string (as when the process is canceled) it just goes on like nothing happened.

Could this have to do with my having a keyPressed() function in my code?  Is this a bug? Just the way it is?  Workaround?

Replies(5)

Hey,

i tried to reproduce this behaviour, but for me it works fine on Leopard and Lion with processing 1.5.1 and 2.0b3.
Could you post an example-code, where the issue occurs?
Yep, should have to begin with!  Also, it appears that I didn't update Processing 2.0 to the most recent version - running 2.0a6 the method for selectInput() returns a string, but the code doesn't work at all in 2.0b.

Copy code
  1. void setup() {
  2.   size(500, 500);
  3.   smooth();
  4.   textAlign(CENTER, CENTER);
  5. }

  6. // in the draw, just display some instructions until a key is pressed
  7. void draw() {
  8.   background(0);
  9.   fill(255);
  10.   text("o  =  open file\ns  =  save file", width/2, height/2);
  11. }

  12. // letter 'o' opens file, 's' saves it
  13. void keyPressed() {
  14.   if (key == 'o') {
  15.     openFile();
  16.   }
  17.   else if (key == 's') {
  18.     saveFile();
  19.   }
  20. }

  21. void openFile() {

  22.   // load the full path to the file
  23.   String inputFilename = selectInput("Select a file to open:");

  24.   // if the load was successful, print the filename
  25.   if (inputFilename != null) {
  26.     println("File selected: " + inputFilename);    // let us know what happened
  27.   }

  28.   // if the user cancels we would normally do nothing at all...
  29.   else {
  30.     println("User did not select a file...");
  31.   }
  32. }

  33. void saveFile() {

  34.   // create a new complete path
  35.   String outputFilename = selectOutput("Save As...");

  36.   // if we've created a valid filename and path...
  37.   if (outputFilename != null) {
  38.     println("Saving to: " + outputFilename);
  39.   }

  40.   // if the save was canceled, we would normally do nothing...
  41.   else {
  42.     println("Save canceled...");
  43.   }
  44. }
Ok, now i see what you mean, i could reproduce it in 1.5.1
I can't explain why this happens, but you can avoid it by calling "noLoop()" before using "selectInput()", and "loop()" after.
Copy code
  1.   noLoop();
      String inputFilename = selectInput("Select a file to open:");
      loop();
The selectInput() in newer processing versions now uses a callback-function, an example of how to use it, is in the release-notes here.

FYI, the sketch works fine in 1.5.1 with Windows 7.
@PhiLho:
Hmm, Windows.  Perhaps it's an enter vs return thing?

@benja:
Your method seems to work, but the callback version in the release notes seems better.  The example isn't very clear, and the docs are less so.  Eventually, I realized that the callback argument isn't a string, but rather a string that is the same name as the function to handle the open/save.  I ended up with this:
Copy code
  1. File f;

  2. void setup() {
  3.   size(300,300);
  4.   smooth();
  5.   textAlign(LEFT, CENTER);
  6. }

  7. void draw() {
  8.   background(0);
  9.   fill(255);
  10.   text("o  =  open a file\ns  =  save a file", 50, height/2);
  11. }

  12. void keyPressed() {
  13.   if (key == 'o') {
  14.     selectInput("Open...", "openFile");
  15.   }
  16.   else if (key == 's') {
  17.     selectInput("Save As...", "writeFile");
  18.   }
  19. }

  20. void openFile(File f) {
  21.   if (f != null) {
  22.     println("file: " + f.getAbsolutePath());
  23.   }
  24.   else {
  25.     println("no file selected...");
  26.   }
  27. }

  28. void writeFile(File f) {

  29.   if (f != null) {
  30.     println("saving: " + f.getAbsolutePath());
  31.   }
  32.   else {
  33.     println("save file canceled...");
  34.   }
  35. }
I do prefer the File object being returned rather than a String, since I can do a lot more with it other than get the path.