I’m trying to figure out how to make the selectFolder() function open using a select path eg instead of opening up with the documents folder selected open with the sketch paths one?
1 Like
Hello,
References:
- https://processing.org/reference/selectFolder_.html
- Java.io.File Class in Java - GeeksforGeeks
- https://processing.github.io/processing-javadocs/core/processing/core/PApplet.html#selectFolder-java.lang.String-java.lang.String-java.io.File-
I was able to use the File datatype for file using this syntax for selectFolder():
selectFolder(prompt, callback, file)
And set the default path to c:\test:
:)
1 Like
//https://gist.github.com/marwie/eb3b7f204f8592628907
// This is the Filetype we're gonna look for
String extensionToLookFor = ".jpg";
// ArrayList of loadedFiles
ArrayList<File> loadedFiles;
int fileToLoad = 0;
PImage loadedImage;
void setup()
{
size(400,300);
background(0);
//selectFolder(String, String);
selectFolder();
}
void draw()
{
if(loadedImage == null || loadedFiles == null || loadedFiles.size() <= 0)
return;
background(0);
image(loadedImage, 0, 0, width, height);
}
void keyPressed()
{
switch(key)
{
case ' ':
fileToLoad ++;
if(fileToLoad >= loadedFiles.size())
fileToLoad = 0;
String imgPath = loadedFiles.get(fileToLoad).getAbsolutePath();
println("Load: " + imgPath);
loadedImage = loadImage(imgPath);
break;
case 'l':
selectFolder();
break;
}
}
void selectFolder()
{
// Opens Window to select a folder or file from
// then Calls a Method with the selected File as Parameter
selectFolder("Select a Folder", "loadFiles");
}
void loadFiles(File selection)
{
// if nothing was selected
if(selection == null)
{
println("User hit cancel");
// open the Window again
selectFolder();
// and skip all code below by returning
return;
}
// this is the absolutePath (just for Debugging)
String selectedPath = selection.getAbsolutePath();
println("Path selected: " + selectedPath);
// now get all Files with the right Extension
loadedFiles = getFilesFromFolder(selection, extensionToLookFor);
println("Found Files: " + loadedFiles.size());
fileToLoad = 0;
loadedImage = loadImage(loadedFiles.get(fileToLoad).getAbsolutePath());
//loadedImages = loadImagesFromFiles(loadedFiles);
}
// Method that returns all Files with a specific Extension in a Folder
ArrayList<File> getFilesFromFolder(File selectedFolder, String extension)
{
// all files
File[] allFiles = selectedFolder.listFiles();
// only Files with the extension:
ArrayList<File> files = new ArrayList<File>();
// loop all Files
for (int i = 0; i < allFiles.length; i++)
{
File f = allFiles[i];
// skip files without the right extension
if( !hasExtension(f, extension) ) continue;
// if not skipped -> add it to arraylist
files.add(f);
}
// return found files
return files;
}
// Helper Method to check for a files Extension
boolean hasExtension(File file, String extension)
{
// the absolute path to the file
String filePath = file.getAbsolutePath();
// make the Path and the Extension to LowerCase (because ".JPG" == ".jpg" !!!)
filePath.toLowerCase();
extension.toLowerCase();
// get the last characters of the path as they should contain the extension of the file
// like for example: /someFolder/someFile.jpg
// last character minus the length of the extension = index of first extension char
int extensionStart = filePath.length() - extension.length();
// index of last char in filePath
int extensionEnd = filePath.length();
// just the last chars of the filePath:
String filePathExtension = filePath.substring( extensionStart, extensionEnd );
// if the filePathExtension = is the extension we're looking for
if(filePathExtension.equals(extension))
{
// end here with true
println("Right Extension: " + extension);
return true;
}
else
println("Wrong Extension: " + filePathExtension);
// file has not the extension we looked for
return false;
}
// Unused Methods:
// Method for Loading all Files in PImage ArrayList
// Beware: might be very memory consuming
ArrayList<PImage> loadImagesFromFiles(ArrayList<File> files)
{
ArrayList<PImage> imgs = new ArrayList<PImage>();
for(File f : files)
{
PImage img;
String imgPath = f.getAbsolutePath();
println("Load Image: " + imgPath);
img = loadImage(imgPath);
imgs.add(img);
}
return imgs;
}
// to be checked out:
// https://processing.org/discourse/beta/num_1212084677.html
/*
// have a look in the data folder
java.io.File folder = new java.io.File(dataPath(""));
// this is the filter (returns true if file's extension is .jpg)
java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}
};
// list the files in the data folder
String[] filenames = folder.list(jpgFilter);
*/
Yeah, well, under Win it’s not reliable.
I think there’s a bug
It stores the last folder but when you open another folder inbetween it won’t return to the folder from the actual command
So you first think it works until you open another folder
I did not observe any issues.
If there is a bug can you open an issue on Github please?
:)