-3

I know this is a gimme, but I'm trying to make the filenames serialized with four digits instead of one. This function is for exporting PNG files from layers within Adobe Illustrator. Let me know if you ever need icons - much respect.

var n = document.layers.length;
    hideAllLayers ();
    for(var i=n-1, k=0; i>=0; i--, k++)
    {
        //hideAllLayers();
        var layer = document.layers[i];
        layer.visible = true;


        var file = new File(folder.fsName + '/' +filename+ '-' + k +".png");

        document.exportFile(file,ExportType.PNG24,options);
        layer.visible = false;
    }
Marz
  • 19
  • 2

2 Answers2

1

Use util.printf (see the Acrobat API, page 720):

var file = new File(util.printf("%s/%s-%04d.png", folder.fsName, filename, k));
rampion
  • 87,131
  • 49
  • 199
  • 315
1

You can pad your number to the left and take the last four characters like this:

var i = 9;
var num = ("0000"+i);
var str = "filename"+(num.substring(num.length-4));  //filename0009

Or shorter

str = ("0000" + i).slice(-4)

Thanks to this question

Community
  • 1
  • 1
  • Note that this truncates numbers of 5 or more digits to the last four digits. This isn't bad, but is something to be aware of. – rampion Apr 21 '14 at 15:00
  • @rampion Read the question: the requirement was to serialise filenames with four digits. We must assume that the OP never has more than 10000 files (0 - 9999) –  Apr 21 '14 at 15:03
  • I agree, I just figured it was worth pointing out. – rampion Apr 21 '14 at 17:07