Grab sketch/window name from within sketch for copy-and-paste screenshot method?

I make a lot of short sketches from scratch. I’m attempting to write a short bit of code that I can copy and paste into any sketch so I can get full resolution screenshots with as little effort as possible. Currently I have this:

void keyPressed() {
  if (key == '`') {
    String t = str(year())+str(month())+str(day())+str(hour())+str(minute())+str(second());
    String imgfiletype = ".png";
    saveFrame("[SCREENSHOT FOLDER ABSOLUTE PATH]\\[SKETCH NAME]-" + t + imgfiletype);
  }
}

I know it seems silly and will probably save less time in the longrun than how long it’s taking me to come up with a solution but I’m neurotic about my code. I am trying to avoid using any external libraries just because having to write the import statement at the beginning of every sketch feels like it defeats the purpose. Is it possible to grab the file name of the sketch OR the name of the display window within the file? Alternatively, I am open to suggestions of any other way to grab a snapshot of the window that fits these criteria:

  • Guaranteed unique file name (I figure system time is the best way to accomplish that without any libraries, unless I take several shots in the same second but I’ll arbitrarily say I don’t mind ignoring that case).
  • All save to an absolute path so I can view all of them just by opening the one folder.
  • Includes title of sketch or the window it’s displaying in (the two of which would be the same) for searchability within folder.
  • Can be pasted at the end of a sketch with no other modifications to the code (unless I’m using keyPressed() for something else but, again, doesn’t cross my arbitrary line) or, ideally, doesn’t need to alter the sketch at all.
  • Only runs in response to user input.
  • Bonus if it works in all renderers as well

TL;DR how fetch sketch/display window name on keyPressed() without any import statements? i have brain problems please indulge me

Thanks to scudly, going forward with this.

void keyPressed() {
  if (key == '`') {
    String name = getClass().getSimpleName();
    String t = str(year())+str(month())+str(day())+str(hour())+str(minute())+str(second());
    String imgfiletype = ".png";
    saveFrame("[SCREENSHOT FOLDER ABSOLUTE PATH]\\" + name + t + imgfiletype);
  }
}

It looks like this works:

String str = Thread.currentThread().getStackTrace()[1].getClassName();
println("Program: " + str);

Thanks for asking. I may start using that.

EDIT:
In browsing through the Processing source, it looks like it’s even easier:

println( getClass().getSimpleName() );

The first one I posted above should work with any general Java program.

2 Likes

both of these work like a charm, god bless! lord knows I would not have thought to check the source code (im stupid)