Using Processing 4.2, I am trying to put the last directory as the title to Processing window. From a file I have opened. To this end I am trying to use the split function. Unfortunately I can’t get it right, I have tried several different combinations but nothing seems to do the trick. This is my code, I hope some one can spot where I am going wrong.
String[] txtFile;
void setup() {
size(400,430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
String[] dirictories split(filepath, char(/)); // not able to get this to work
surface.setTitle(filepath); // put the last directory as the title of the window - not working
// load file here
txtFile = loadStrings(filepath);
}
}
The code I am opening is actually a Gcode file but I am using just the generic name of text file at the moment.
Thanks
Mike
Thanks.
I tried the replacement and got a Syntax Error - Missing “;” I have been getting these a lot when clearly there is a ; at the end of the message. I also get the message:-
The function "split() expects parameters like: “split(String)”
The value of file path is taken from line 12 of the code where:-
String filepath = selection.getAbsolutePath();
I am getting this filepath fine, it is just that it shows all of the path and I am trying to get just the last directory.
// A dummy file to get us started
File file = new File(dataPath( "temp.txt"));
// Show absolute path
println(file.getAbsolutePath());
// Show filename
println(file.getName());
Thanks but things are getting messy now. This is the state of my code at the moment:-
String[] txtFile;
File file = new File(dataPath( "temp.txt"));
void setup() {
size(400,430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
println(file.getName());
//String[] dirictories = filepath.split('/'); // not able to get this to work
surface.setTitle(filepath); // put the last directory as the title of the window - not working
// load file here
txtFile = loadStrings(filepath);
}
}
But the code still needs to be run to get into the else stage of the “fileSelected” part of the function.
When it is I get a whole list of stuff in the console window.
objc[21752]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fffabaad3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x1318cdf50). One of the two will be used. Which one is undefined.
User selected /Users/mikecook/Documents/An_example_Cut.gcode
temp.txt
Sorry but I thing we are going backwards at the moment.
String[] txtFile;
void setup() {
size(640, 430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
String sep = "/"; // OS specific
int idx = filepath.lastIndexOf(sep);
String path = idx <= 0 ? "-" : filepath.substring(0, idx);
surface.setTitle(path); // put the last directory as the title of the window - not working
// load file here
txtFile = loadStrings(filepath);
for (int i = 0; i < txtFile.length; i++)
println(txtFile[i]);
}
}
I am using Processing 4.4.4 and Windows 10.
I use Processing 4.3.4 for error checking since it is broken for later versions.
This works:
String[] txtFile;
File file = new File(dataPath( "gcode.txt"));
void setup() {
size(400,430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
println(file.getName());
println(filepath);
// Either of these work:
String[] directories = filepath.split("\\\\"); // This works!
//String[] directories = split(filepath,'\\'); // Processing split
printArray(directories);
surface.setTitle(filepath); // put the last directory as the title of the window - not working
// load file here
txtFile = loadStrings(filepath);
}
}
objc[21954]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fffabaad3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x12f3a3f50). One of the two will be used. Which one is undefined.
User selected /Users/mikecook/Documents/An_example_Cut.gcode
gcode.txt
/Users/mikecook/Documents/An_example_Cut.gcode
[0] “/Users/mikecook/Documents/An_example_Cut.gcode”
And the window gets named as:-
instead of just “An_example_Cut.gcode”
Maybe the difference is that I am on a Mac, macOS Mojave Version 10.14.6, I am reluctant to upgrade the Processing 4.2 at the moment because a lot of my stuff in in this version.
I haven’t explored your “also works” suggestions. That is my next stage.
Thanks again for your input.
Edit
// Either of these work: sorry no they don’t.
Using the reggex utility plus changes to the setting the title of the window to
surface.setTitle(file.getName());
Goes a bit too far and the window title is just
“gcode.txt” and not “An_example_Cut.gcode”
It seems like the problem is in setting the screen title. This function requires a string and not an array.
String[] txtFile;
void setup() {
size(400,430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
surface.setTitle(selection.getName()); // put the last directory as the title of the window - not working
// load file here
txtFile = loadStrings(selection.getAbsolutePath());
}
}
My mistake missed that little gem. This works for me.
String[] txtFile;
void setup() {
size(640, 430);
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
String filepath = selection.getAbsolutePath();
println("User selected " + filepath);
String sep = "/"; // OS specific
int idx = filepath.lastIndexOf(sep);
String name = idx < 0 ? filepath : filepath.substring(idx + 1);
String path = idx <= 0 ? "-" : filepath.substring(0, idx);
idx = path.lastIndexOf(sep);
String dir = idx <= 0 ? "-" : path.substring(idx);
surface.setTitle(dir); // use last directory as the title of the window
// All done so show all the file and path data collected
println("Absolute path : " + filepath);
println("Full dir path : " + path);
println("Top directory : " + dir);
println("File name : " + name);
println("----------------------------------------------------------------");
// Show contents of text file here
txtFile = loadStrings(filepath);
for (int i = 0; i < txtFile.length; i++)
println(txtFile[i]);
}
}