Incrementing PDF filenames

edited February 2017 in Questions about Code

Hello, I'm trying to make my program save a new pdf file every time i press "s". I'm having trouble incrementing filenames. i would like them to be timestamped with the date and time. This currently just names the file after the frame i save it on, which works, but isn't ideal.

I found a sugested solution by user Koogs, but I have absolutely no idea how to implement it.

maybe put the date and time in the filename so ensure the filenames are unique.

use yyyymmdd_hhmmss format to preserve the creation order.

As you can probably see from the mess of the code, I tried to do some stuff with adding one number to the filename after each save, but to no avail.

Any comments would be appreciated

import processing.pdf.*;  // Import PDF code

boolean saveOneFrame = false;
float x;
float y;
int value;
int number=0;
MouseEvent event;
void setup()
{
  size(1920, 1080);
  background(200);

}
void draw() {
    if (saveOneFrame == true) {            // When the saveOneFrame boolean is true,
    beginRecord(PDF,"stringz###.pdf");    // start recording to the PDF
    }

if (mousePressed) { 
 x = lerp(x, mouseX, 0.01); //što je bliže nuli, to više interpolira. i obrnuto ofc
  y = lerp(y, mouseY, 0.01); //što je bliže nuli, to više interpolira. i obrnuto ofc

     int a = int(random(width-200, width+800));
     int b = int(random(height)); 

  noFill();
  stroke(value);
  beginShape();
  curveVertex(x, y); 
  curveVertex(x/2, y/2);
  curveVertex(x*2, y*2);
  curveVertex(a, b);
  curveVertex(a-b, b-a);
  curveVertex(y-200, 500);
  endShape();}
  if (saveOneFrame == true) {             // If the PDF has been recording,</em>
    endRecord();                          // stop recording, 
    saveOneFrame = false;                 // and set the boolean value to false
  }
}



void keyPressed(){
  if (key == 'F' || key == 'f') // ako stisneš F ili f, izbrišeš sve
    background(200);
  if (key == 'S' || key == 's') { 
    beginRecord(PDF, "stringz###.pdf"); 
    number+=1;
  } else if (key == 'D' || key == 'd') { 
    endRecord();
    exit();
}
}

Answers

  • Answer ✓
    beginRecord(PDF, "stringz###.pdf"); 
    number+=1;
    

    the #### expansion uses the framecount. this will keep your frames in order but they won't be contiguous (there'll be gaps in the numbers)

    or you can use the number by using "stringz" + nf(number, 3) + ".pdf" as your filename

    nf() zero-pads numbers to the given length
    https://processing.org/reference/nf_.html

  • Thanks for the answer! the nf() command sounds useful for my application :)

    However, when i replace the code you wrote with my filename, it runs but only saves one file. every subsequent save seemingly does nothing. Any ideas? (the code is the same as above, no changes besides that)

  • Answer ✓

    S starts recording but D ends the recording and terminates the program so, yes, you'll only get one file.

    Maybe you need to remove the exit from D. That'll let you press S again to record to another file.

    You can delete event, you don't use it.

  • Thanks very much for the answer, everything works now :D just another question that popped up, is there a way to save to pdf what is currently on the screen, and not what just happened after the record command was issued?

  • Answer ✓

    Not easily, the beginRecord doesn't know what's gone before.

    You can saveFrame(), which will save the pixels, but it won't be a PDF and therefore won't be scalable.

    Or you can store, somehow, everything and redraw it between the beginRecord and the end.

    Life is all compromises.

  • Yeah I guess as you said, I have to compromise.

    If there was a way to save the PDF without stopping the record function that would be useful.

    But I'll manage with this :)

    Thanks again for the help, I appreciate it!

Sign In or Register to comment.