Exclude type file from selectInput()?

Is it possible to exclude a specific type file when we use selectInput() as seen in this image?

Have you already google it? Second link shows this thread:

Thanks I don’t see this specific post, but I don’t understand, where I must pass this filter and how ?

void setup() {
  String prompt = "Select a file to process:";
  String callback = "fileSelected";
  selectInput(prompt, callback);
  // selectInput(prompt, callback)
  // selectInput(prompt, callback, file)
  // selectInput(prompt, callback, file, callbackObject)
  // selectInput(prompt, callbackMethod, file, callbackObject, parent, sketch)
  //selectInput(prompt, callbackMethod, file, callbackObject, parent)
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Nothing");
  } else {
    println("Input default path is:" +selection.getAbsolutePath());
    println(selection.getAbsolutePath());
  }
}

import java.io.FilenameFilter;
 
static final String[] exts = {
  ".gif", ".png", ".jpeg", ".jpg"
};
 
static final FilenameFilter pictsFilter = new FilenameFilter() {
  boolean accept(File dir, String name) {
    name = name.toLowerCase();
    for (int i = exts.length; i-- != 0;)  if (name.endsWith(exts[i]))  return true;
    return false;
  }
};
 
static final File[] getFolderContent(File dir) {
  return dir.listFiles(pictsFilter);
}

there is a good example:

Thx, but it’s not what I want. I need to open a window to select the file with selectInput() but excludes the files not concerned like on the pics show on the top of topics, where the files is gray light when it’s not possible to select.
By example when I use Photoshop and open file i cannot selected file from Indesign.
Make sens ?
Or maybe i don’t understand the example you show me ?

I believe that someone once implemented a file filter for selectInput, but I think it was never merged. Perhaps use that code?

You can also make your own java swing FileChooser with a filter as per this tutorial:

https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

2 Likes

So i continue my investigation on stackoverflow
because I’ve a feeling the solution to exclude type of file work only for input folder.

void setup() {
  selectFolder("Select a folder to process:", "folderSelected");
}

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

import java.io.FilenameFilter;
static final String[] exts = { ".gif", ".png", ".jpeg", ".jpg", ".tiff", ".tif"};

static final FilenameFilter pictsFilter = new FilenameFilter() {
  boolean accept(File dir, String name) {
    name = name.toLowerCase();
    for (int i = exts.length; i-- != 0;) {
        if (name.endsWith(exts[i]))  {
            return true;
        }
      }
    return false;
  }
};

static final File[] getFolderContent(File dir) {
  return dir.listFiles(pictsFilter);
}

Hello, sorry i try to wakeup the subject.

i try to dev an app for playing with collage on picture for newbies and looking for a way to select extension file selectable in selectInput().

I do not see any implementation, in my SearchEngines, of the third parameter file in the selectInput(prompt, callback, file)!
Does anyone have an answer to this old topic?

the first java code open ugly java windows, not OS specific windows.
So is there a way and an example code, in Processing, to select extension of inputSelect() as OS purpose since 40 years?

regards

I work on your subject and I develop something can help you… I hope

Well Stan, i’m not sure to understand how to use your code.
do i have to copy the selectInput function (i think i need more)
or do i import your code with an url?
sorry, for this question.
Im’ not as good as you think :wink:

regards

1 Like

@Stanlepunk ok i downloaded the rope.zip and add it to my Librairies
I tried the select_input.pde example but the sketch block without error.

i’m working on macos ventura 13.5.2 with procesisng 4.2 and 4.3

i tried the folder_explore.pde and if i understand well it suppose to show only jpeg file… the jpeg file are greyed too.

The full function header for Processing’s selectInput method is
selectInput(prompt, callback, file, callbackObject)

Although the first two parameters are well documented the optional 3rd and 4th are not :disappointed:

Digging into the source code I believe that the following is true

  • file :- is the default file selection. So if provided the dialog box will open with this file selected
  • callbackObject: the object that contains the callback method to be executed when the dialog box closes. If none provided then it will look inside your sketch for it.

Processing does not appear to provide a mechanism to filter by file type which again is disappointing.

Th easiest solution would to use the G4P library. For instance the statement -

fname = G4P.selectInput("Input Dialog", "png,gif,jpg,jpeg", "Image files");

opens a dialog box where files with the extension png gif jpg or jpeg can be selected.

Where fname is of type String and will hold the full path and filename for the selected file. If the dialog is cancelled then fname is null

Unlike Processing the select methods in G4P are modal which means that the sketch execution stops until the dialog box closes, so no callback function needed.

4 Likes

Hello Quark

thanks for the answer

import g4p_controls.*;
String fname;

void setup() {
  fname = G4P.selectInput("Bonjour", "png,gif,jpg,jpeg", "Image files");
  println(fname);
}

void draw() {
  
}

it works well as expected, but i do not find any doc for this lib.
The three parameters are “strings”, where can i place the start point for file looking?
the first and third params are invisible in MacOS (ventura) system window…
It seems to remember my last folder path but not the default skechtPath from where is my sketch.

1 Like

Hi @mrbbp,

http://www.lagers.org.uk/g4p/ref/classg4p__controls_1_1_g4_p.html#a34b5f1cceb46ea8f23de801c7aced033

Cheers
— mnse

2 Likes

Thanks to @mnse for beating me to it :grinning:

What actually gets shown in the dialog box depends on the OS, sometimes these are shown and sometimes not.

This is the default situation in most OSs and what you would expect in most applications e.g. MS Word

The method takes a fourth parameter for the starting point - see documentation in link provided by mnse

2 Likes

thanks for @mnse for pointing the doc and particulary @quark for the works.

i complete the script to start in the sketch folder.

import g4p_controls.*;
String fname;

void setup() {
  fname = G4P.selectInput("unseen in MacOS", "png,gif,jpg,jpeg", "unseen in MacOS", sketchPath("") );
  println(fname);
}

void draw() {
  
}

regards

Can I set also the path the dialog opens in?

Yes, by startFolder parameter.