SelectInput: I like to tell it the folder to use?

Hello all,

I‘d like to tell selectInput() which folder and file extension to use:

File file1= new File( sketchPath(”“) + ”*.txt“);

selectInput(”...“, ”...“, file1);

Doesn’t work. It starts with the Desktop Folder instead (and not in sketch path folder).

(Sorry for the wrong „“, my iPhone…)

What do I have to change?

Win 10.

Thank you!

Chrisir

2 Likes

Limited research but probly these next links could help:

Kf

3 Likes

Actually

selectInput ( "Select a file to process:" , "fileSelected" , dataFile( "*.txt" ));

did the trick.

Remaining problem is that the load dialog is not in the foreground in Win 10 but hidden.

Any ideas?

3 Likes

This sounds like a bug worth reporting. Is that behind a PDE sketch window, or is it from an exported application?

2 Likes

in Win 10 / processing 3.5.3. /
called from a keyPressed()
no problem

but yes usually thing like
JOptionPane.showInputDialog
tend to be behind the canvas window GRRR

different example:

// https://discourse.processing.org/t/file-listing-order/7148/23
// https://discourse.processing.org/t/selectinput-i-like-to-tell-it-the-folder-to-use/13703/3

// as started from load display image i have a sub dir /data/ with a data\moonwalk.jpg

static final String PICS_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp";
File dir;
String[] imagePaths = {};
int imagesFound = 0;
boolean havefile = false;
PImage img;

void filedata() {   // @GoToLoop
  dir = dataFile("");
  println(dir);
  if (dir.isDirectory()) {
    imagePaths = listPaths(dir.getPath(), "files", "recursive", PICS_EXTS);
    imagesFound = imagePaths.length;
  }
  printArray(imagePaths);
  println("Found", imagesFound, "image(s)");
}

void setup() {
  size(300,300);
  filedata();
  println("use: key [r] for file open dlg\n  or key [0 (...x]) for load from array");
}

void draw() {
  if ( havefile ) image(img,0,0);  //,width,height
}

void keyPressed() {
  if ( key == 'r') {
    selectInput("open picture: ", "fileSelected", dataFile(PICS_EXTS));
  }
  if ( key == '0') {
    String thefile = imagePaths[0];
    println("you selected: key ["+key+"] "+thefile);
    img = loadImage(thefile);
    havefile = true;
  }
}

void fileSelected(final File sel) {
  if (sel != null) {
    String thefile = sel.getPath();
    println("you selected: "+thefile);
    img = loadImage(thefile);
    havefile = true;
  }
}

2 Likes

Thanks a lot everyone!

I have to say, sometimes it works. It seems to store the last directory implicitly. So often it looks like it works but it doesn’t.

It doesn’t really take the directory from the file I give to selectInput(). Seems to be a bug.

This is the core line for me :

selectInput("open picture: ", "fileSelected", dataFile(PICS_EXTS));

I understand this line:

static final String PICS_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp";

How would I tell it the folder as well?

Thanks!

Chrisir

1 Like

I know you posted this back in 2019, but I thought I’d add a little something to it.
I discovered it on accident. If you add “…:” to dataFile(.txt); as in
`selectInput(“Select a file to process:”, “fileSelected”, dataFile(" …:
.txt")); (2 or 3 dots/periods and a colon)
the next time you use selectInput(), it will start the folder you last used. I don’t know why this works. I can’t find any information about it, but it works

2 Likes

thanks for coming back to me!

In my experience it will always start the folder you last used (not only with your trick) - which is a bug because it should use the folder you tell it to use.

For me, selectInput(“Select a file to process:”, “fileSelected”, dataFile(" * .txt")), always defaulted to the sketch data folder each time it was called. Granted, I can hard code a default folder, but that may not work for everyone nor every sketch. For instance, choosing a music folder. I discovered this while making a video for my YT channel “Mark Loves Coding” I’ve been coding a random music player based on a question I responded to on this forum. As the sketch evolves I eventually started using the selectInput() function. That is when I started to try to figure out why that function would not remember the last folder I was looking in. As a windows user I noticed that some apps do remember the last folder you were in and some don’t. But not Processing sketches using the selectInput() function. That said, rather than call it a bug, just add what I discovered to the documentation. I mean, why not, it works. Cheers

1 Like