Open file selector while programm is running?

i was trying to make a programm where if i klick a button, i can select an image! but if i try it just opens the selector over and over until my pc crashes!

void setup() {
  size(960,540);
}

void draw() {
  if(mousePressed) {
    selectOutput("Select a file to write to:", "fileSelected");
  }
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
}

edit cause i wanted to make the code highlight and didnt got it 1st time!

1 Like

The point with using a function mousePressed() instead of the variable mousePressed is
very important.

remark

  • please note that with selectOutput you choose a file to save to.

  • maybe to load you want selectInput which is a similar command

Sketch

Anyway, you can use state to distinguish the different phases:

int state = 0; 

String pathMy=""; 

void setup() {
  size(960, 540);
  background(111);
}

void draw() {
  background(111); 

  switch(state) {
  case 0:
    text("please click the mouse to choose a file", 66, 66);
    break; 

  case 1:
    text("please choose a file", 66, 66);
    break; 

  case 2:
    text ("You chose "
      + pathMy
      + "\n\n\n\n\n\n\n\n\nClick the mouse to start again.", 66, 66); 
    break; 

  default:
    //Error
    exit(); 
    break;
  }//switch
}//func 

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
    state = 0;
  } else {
    println("User selected " + selection.getAbsolutePath());
    pathMy=selection.getAbsolutePath(); 
    state = 2;
  }
}

void mousePressed() {
  switch(state) {
  case 0:
    selectOutput("Select a file to write to:", "fileSelected");
    state=1;
    break; 

  case 1:
    //ignore 
    break; 

  case 2:
    // reset 
    state = 0;  
    break;

  default:
    //Error
    exit(); 
    break;
  } //switch
} //func 
//

1 Like

i have a question if its possible to make it say open file and not save! thank you so much!

1 Like

Exactly!

As I said check out selectInput() command.

See reference please : selectInput() / Reference / Processing.org

Chrisir

1 Like

im sorry, i oversaw it! thank so much! you made it so much easier for me!!

1 Like