# selectOutput feature

**URL:** https://discourse.processing.org/t/selectoutput-feature/5606
**Category:** Coding Questions
**Created:** [November 16, 2018, 5:37pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606 "2018-11-16T17:37:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![FerrariLuigi](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@FerrariLuigi](https://discourse.processing.org/u/FerrariLuigi)
#### Post date: [November 16, 2018, 5:37pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/1 "2018-11-16T17:37:15Z")

</div>

Dear all,  
I developed a very simple sketch in which I use the selectOutput function.  
It works but, as soon as the select output file window is open, the sketch graphic disappear and the desktop is shown.  
Did you have any similar problem ? How did you fix it ?  
Thank you very much for your help and cooperation  
regards

you can download the sketch from [here](https://drive.google.com/file/d/13uyz7L2WYuaqZb6v14JSSWx4sMnSr-Gc/view?usp=sharing)

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [November 16, 2018, 9:38pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/2 "2018-11-16T21:38:34Z")

</div>

Please post your code in the form of a [MCVE](https://stackoverflow.com/help/mcve).

More info here:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<div class="post-metadata">

### Author: ![FerrariLuigi](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@FerrariLuigi](https://discourse.processing.org/u/FerrariLuigi)
#### Post date: [November 18, 2018, 4:13pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/3 "2018-11-18T16:13:01Z")

</div>

Dear all,  
I developed a very simple sketch in which I use the selectOutput function.  
It works but, as soon as the select output file window is open, the sketch graphic disappear and the desktop is shown.  
Did you have any similar problem ? How did you fix it ?  
Thank you very much for your help and cooperation  
regards

This is the source code of TestFile.pde

```auto
PImage myImage;
cButton myButton;

void setup()
{
  fullScreen(P2D, 2);
  myImage=loadImage("img.jpg");
  myButton=new cButton("","button",(displayWidth-myImage.width)/2,(displayHeight-myImage.height)/2,100);
}

void draw()
{
   image(myImage,(displayWidth-myImage.width)/2,(displayHeight-myImage.height)/2);
   myButton.drawBottone();
   int x = myButton.testMouse();
   if(x>=0) {
     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());
  }
}

```

This is the source code of cButton class buttonClass.pde file:

```auto

    final static String RELEASED
            = "Released";
    final static String OVER
            = "Over";
    final static String PRESSED
            = "Pressed";

    class cButton {

        PImage released;
        PImage over;
        PImage pressed;
        int status;
        int left;
        int right;
        int top;
        int bottom;
        int x;
        int y;
        int mouseStatus;
        float res;
        int sx;
        int sy;

        cButton(String path, String name, int lx, int ly, int resize) {
            released = loadImage(path + name + RELEASED + ".png");
            over = loadImage(path + name + OVER + ".png");
            pressed = loadImage(path + name + PRESSED + ".png");
            status = 0;
            x = lx;
            y = ly;
            res = (float) resize;
            sx = (int) ((float) released.width * (res / 100));
            sy = (int) ((float) released.height * (res / 100));
            right = x + sx;
            bottom = y + sy;
            top = y;
            left = x;

            mouseStatus = 0;
        }

        int mouse() {
            int x = testMouse();
            drawBottone();
            return x;
        }

        public void drawBottone() {
            switch (status) {
                case 0: // released
                    image(released, x, y, sx, sy);
                    break;
                case 1: // over
                    image(over, x, y, sx, sy);
                    break;
                case 2: // pressed
                    image(pressed, x, y, sx, sy);
                    break;
            }
        }

        public void drawBottone(int x, int y) {
            switch (status) {
                case 0: // released
                    image(released, x, y, sx, sy);
                    break;
                case 1: // over
                    image(over, x, y, sx, sy);
                    break;
                case 2: // pressed
                    image(pressed, x, y, sx, sy);
                    break;
            }
        }

        public int testMouse() {
            int flg;
            int ret;
            flg = 0;
            ret = -1;
            // println("X: "+mouseX+" Y: "+mouseY);
            if (mouseX >= left && mouseX <= right) {
                if (mouseY >= top && mouseY <= bottom) {
                    if (mousePressed == true && mouseButton == LEFT) {
                        flg = 2;
                         ret = 1;

                    } else {
                        flg = 1;
                    }
                }
            }
            
            status = flg;
            if(ret==1) {
              if( mouseStatus<0) {
                mouseStatus=ret;
              } else
                ret=-1;
            }else
              mouseStatus=ret;
            // println(ret);
            return ret;
        }
    }

```

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [November 18, 2018, 5:08pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/4 "2018-11-18T17:08:23Z")

</div>

You can simplify your example even further. Try to get rid of images and classes. Like this:

```auto
void setup() {
  size(400, 400);
}

void draw() {

  if (mouseX >= width/2 && 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());
  }
}

```

This code is roughly equivalent to yours, without any extra code or dependencies.

The problem with this code is that you’re calling selectInput() from inside the `draw()` function. This is causing 60 dialogs per second to display, which is breaking your sketch.

To fix this, you need to prevent multiple dialogs from popping up: you could add a `boolean` variable that tracks this, or you could move your logic into the `mousePressed()` function.

---

<div class="post-metadata">

### Author: ![FerrariLuigi](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@FerrariLuigi](https://discourse.processing.org/u/FerrariLuigi)
#### Post date: [November 18, 2018, 5:34pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/5 "2018-11-18T17:34:46Z")

</div>

Dear Kevin,  
thank you for your suggestion. I modified the code:

```auto
int status;

void setup() {
  size(400, 400);
  status=0;
}

void draw() {

  if (mouseX >= width/2 && mousePressed) {
    if(status==0) {
      status=1;
      selectOutput("Select a file to write to:", "fileSelected");
    }
  }
}

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

```

The status variable is set before open the file selection window.  
It works but the system varaible mousePressed remains always true…  
How to reset it ?  
Had you a similar problem ? How did you fix it ?  
Thank you very much for your help and cooperation.  
regards

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [November 18, 2018, 6:06pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/6 "2018-11-18T18:06:36Z")

</div>

like @Kevin suggested, could move it to mousePressed

```auto
// _________________________________________________________________ 
void setup() {
  size(100, 100);
}

// _________________________________________________________________ 
void draw_menu() {
  fill(255);
  rect(50, 80, 49, 19);
  fill(0);
  textAlign(CENTER, BOTTOM);
  text("File", 75, 97);
}

// _________________________________________________________________ 
void draw() {
  background(200, 200, 0);
  draw_menu();
}

// _________________________________________________________________ 
void mousePressed() {
  if ( overRect(50, 80, 50, 20) ) 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());
}

// _________________________________________________________________ mouse position over rectangle yes/no
boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) return true;
  else return false;
}

```

---

<div class="post-metadata">

### Author: ![FerrariLuigi](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@FerrariLuigi](https://discourse.processing.org/u/FerrariLuigi)
#### Post date: [November 18, 2018, 6:16pm UTC](https://discourse.processing.org/t/selectoutput-feature/5606/7 "2018-11-18T18:16:04Z")

</div>

Ok. I understand. Thank you.
