# Open file selector while programm is running?

**URL:** <https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512>\
**Category:** Coding Questions\
**Created:** [January 8, 2023, 1:29pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512 "2023-01-08T13:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [January 8, 2023, 1:29pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/1 "2023-01-08T13:29:42Z")

</div>

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!

```auto
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!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 8, 2023, 2:10pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/2 "2023-01-08T14:10:39Z")

</div>

> **[mousePressed() / Reference](https://processing.org/reference/mousePressed_.html)**
>
> The mousePressed() function is called once after every time a mouse button is pressed. The mouseButton variable (see the related reference entry) can be used to determine which button ha…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 8, 2023, 2:47pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/3 "2023-01-08T14:47:43Z")

</div>

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:

```auto
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 
//

```

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [January 11, 2023, 8:41pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/4 "2023-01-11T20:41:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 11, 2023, 9:06pm UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/5 "2023-01-11T21:06:13Z")

</div>

> [@Chrisir](#):
>
> selectInput

Exactly!

As I said check out selectInput() command.

See reference please : [selectInput() / Reference / Processing.org](https://processing.org/reference/selectInput_.html)

Chrisir

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [January 12, 2023, 11:57am UTC](https://discourse.processing.org/t/open-file-selector-while-programm-is-running/40512/6 "2023-01-12T11:57:06Z")

</div>

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