Skip to main content
MikaelRav
Participant
January 25, 2021
Answered

Simple script for exportingall pages as PNG with page dimensions in filename

  • January 25, 2021
  • 3 replies
  • 1423 views

I'm looking for a script that saves all pages as PNGs with the dimensions in the filename.

All export settings except for the destination can be specified inside the script file.

All i want to do is:

  1. Run the script
  2. Select destination folder
  3. Done

Each files dimensions should appear in the filename, DocumentFilename_[width]x[height]

I realise that I might run into problems if multiple pages have the same dimension.

This topic has been closed for replies.
Correct answer rob day

Try this where the file name starts with a page number prefix in case the bounds are the same:

 

 

//resolution and anti-aliasing?
var res = 72;
var aa = true;

var pngFolder = Folder.selectDialog("Select the PNG Save folder", ""); 
var doc = app.documents.item(0);
var n = fileName(doc.name);
var nPrefs = app.generalPreferences.pageNumbering;
app.generalPreferences.pageNumbering = PageNumberingOptions.ABSOLUTE;
var dPages = doc.pages;

for (var i = 0; i < dPages.length; i++){
    var pn = dPages[i].documentOffset + 1;
    var bnds = dPages[i].bounds;
    var expName = exportName(n, pn, bnds);
   
    var thePath = pngFolder + "/" + expName + ".png"
    app.pngExportPreferences.properties = {antiAlias:aa, exportingSpread:false, exportResolution:res, pngExportRange:1785742674, pageString: pn.toString()}
    doc.exportFile(ExportFormat.PNG_FORMAT, File(thePath), false);    
};   
app.generalPreferences.pageNumbering = nPrefs;




/**
* Strip Extension 
*  the file name 
*  value file name with no ext
* 
*/
function fileName(n){
    var na = n.split( "." )
    if ( na.length > 1 ) {
        na.length--;
    }
    na = na.join(".");
    return na
}

/**
* The export file name 
*  document name 
*  page number 
*  page bounds 
*  file name as string 
* 
*/
function exportName(n, pn, b){
    var pre = pn.toString();
    if (pre.length < 2) {
        pre = "0" + pre;
    } 
    var w = b[2]-b[0];
    var h = b[3]-b[1];
    var s = pre + "_" + n + "_" + "[" + w + "]X[" + h + "]";
    return s
}

 

3 replies

willcampbell7
Legend
November 8, 2023
rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
January 25, 2021

Try this where the file name starts with a page number prefix in case the bounds are the same:

 

 

//resolution and anti-aliasing?
var res = 72;
var aa = true;

var pngFolder = Folder.selectDialog("Select the PNG Save folder", ""); 
var doc = app.documents.item(0);
var n = fileName(doc.name);
var nPrefs = app.generalPreferences.pageNumbering;
app.generalPreferences.pageNumbering = PageNumberingOptions.ABSOLUTE;
var dPages = doc.pages;

for (var i = 0; i < dPages.length; i++){
    var pn = dPages[i].documentOffset + 1;
    var bnds = dPages[i].bounds;
    var expName = exportName(n, pn, bnds);
   
    var thePath = pngFolder + "/" + expName + ".png"
    app.pngExportPreferences.properties = {antiAlias:aa, exportingSpread:false, exportResolution:res, pngExportRange:1785742674, pageString: pn.toString()}
    doc.exportFile(ExportFormat.PNG_FORMAT, File(thePath), false);    
};   
app.generalPreferences.pageNumbering = nPrefs;




/**
* Strip Extension 
*  the file name 
*  value file name with no ext
* 
*/
function fileName(n){
    var na = n.split( "." )
    if ( na.length > 1 ) {
        na.length--;
    }
    na = na.join(".");
    return na
}

/**
* The export file name 
*  document name 
*  page number 
*  page bounds 
*  file name as string 
* 
*/
function exportName(n, pn, b){
    var pre = pn.toString();
    if (pre.length < 2) {
        pre = "0" + pre;
    } 
    var w = b[2]-b[0];
    var h = b[3]-b[1];
    var s = pre + "_" + n + "_" + "[" + w + "]X[" + h + "]";
    return s
}

 

MikaelRav
MikaelRavAuthor
Participant
January 25, 2021

Thanks!

Legend
January 25, 2021

Hello,

re; "I'm looking for a script that saves all pages as PNGs with the dimensions in the filename."

Are you wanting to export each Page itself as a PNG file? not the page items for each page correct?

 

re; "Each files dimensions should appear in the filename, DocumentFilename_[width]x[height]

I realise that I might run into problems if multiple pages have the same dimension."

Should you inclue the page# in the file naming to prrevent the files from over writing each other?

 

Regards,

Mike

 

 

 

MikaelRav
MikaelRavAuthor
Participant
January 25, 2021

– Yes i want to export full pages no page items.

– I don't think that will be a problem. I guess will be overwritten with the last page with the same dimension.