I am trying to figure out how to access files. All the documentation I’ve seen says that loadImage() etc will only work on files in the data directory. (It seems very strange, that I can’t give an absolute pathname… but hey, ok.)
But…where is the data directory? I set up a test sketch and before I named it, it lived in a very long named place which would be super awkward to access for popping input files into it.
So I named it something sensible by doing Save. Now I can find its path by doing Sketch/ShowSketchFolder, and it looks like this:
But there’s no data directory in it. I’ve been looking at the Reference documentation and Tutorials and stack overflow and this forum for a very long time (…a few hours) and I am totally stuck. If input can only come from a data directory - where is this directory? Do I need to create it for each sketch?
When you talk about “drop a media type file onto the PDE” - I don’t understand what you mean. What kind of operation is this? Is it an “outside Processing” operation? Do you literally mean drag and drop a file while looking at the sketch directory in Finder (I’m on a Mac)? Or do you drop a file onto the code editing window (rather strange… but… )? etc
Thanks also for the help. When you say I “can” create it by hand - do you mean I “must” create it by hand? Is that how it’s intended to work?
What’s confusing me is that the documentation says that each Sketch “has” its own data folder. But I don’t see one when I look at a sketch in its containing folder. It doesn’t seem to “have” it. And it’s not obvious how to create it - it’s not obvious whether that’s something you do inside Processing somehow, or just in the normal file system (which seems clunky), or as the other poster suggested, by somehow “dropping” the file somewhere, somehow.
I think it’s one of those things that when you see it once it seems obvious afterwards.
I think it works in Sketch folder itself too but that’s kind of a bug or feature. Better use the data folder.
Just found this::::
A sketch folder sometimes contains other folders for media files and other code. When a font or image is added to a sketch by selecting “Add File…” from the Sketch menu, a “data” folder is created. Files may also be added to your Processing sketch by dragging them into the text editor. Image and sound files dragged into the application window will automatically be added to the current sketch’s “data” folder. All images, fonts, sounds, and other data files loaded in the sketch must be in this folder.
Of course you can! Try it with… dunno on an mac, on win it’s just c://hello//…
Besides look at selectFolder command in the reference to get a dialog box to select a folder in your Sketch (while your Sketch runs, that’s not the IDE)
To figure out this issue I read the processing source.
Anywhere in processing the string: sketchPath
exists.
Since the variable is private use sketchPath(), here is the source:
static protected String calcSketchPath() {
// try to get the user folder. if running under java web start,
// this may cause a security exception if the code is not signed.
// http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1159386274
String folder = null;
try {
folder = System.getProperty("user.dir");
URL jarURL =
PApplet.class.getProtectionDomain().getCodeSource().getLocation();
// Decode URL
String jarPath = jarURL.toURI().getSchemeSpecificPart();
// Workaround for bug in Java for OS X from Oracle (7u51)
// https://github.com/processing/processing/issues/2181
if (platform == MACOS) {
if (jarPath.contains("Contents/Java/")) {
String appPath = jarPath.substring(0, jarPath.indexOf(".app") + 4);
File containingFolder = new File(appPath).getParentFile();
folder = containingFolder.getAbsolutePath();
}
} else {
// Working directory may not be set properly, try some options
// https://github.com/processing/processing/issues/2195
if (jarPath.contains("/lib/")) {
// Windows or Linux, back up a directory to get the executable
folder = new File(jarPath, "../..").getCanonicalPath();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return folder;
}
public String sketchPath() {
if (sketchPath == null) {
sketchPath = calcSketchPath(); // this line if sketchPath hasn't been initialised
}
return sketchPath;
}
Now, the data directory can be located simply by adding “/data” to the sketch path and there you go!
Using this code (reference on line 7673 PApplet.java)
you can get the sketch path (java.io.File may be required)
void setup() {
println(sketchPath() /*path of the sketch*/ + File.separator /*probably slash*/ + "data" + File.separator);
}
And this piece works fine for me. Logs the data directory even if its an unsaved program and with no libraries (java.io.File not included).