Indesign CS6 Scripting - Exporting images

Alex picture Alex · Jun 11, 2012 · Viewed 8k times · Source

I'm having trouble writing a js script in indesign cs6 to export my formatted images. the code below (found on this website and slightly modified) only opens the document.

ideally the script would loop through all of the formatted/cropped images in my document and export them into a new folder on the desktop, but with the original file names.

any help would be much appreciated:

test();
function test(){

var myDoc = app.open('/Users/StudioA/Desktop/file.indd'); 
var myGroups = myDoc.groups;

//for each group...
for (var i = 0;i < myGroups.length; i++){
    // for each rectangle in the group...
    for(var r = 0; r< myGroups[i].rectangles.length; r++){

         var myRect = myGroups[i].rectangles[r];
           app.jpegExportPreferences.exportResolution = 300;
           app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;

           //give it a unique name
           var myFile = new File('/Users/StudioA/Desktop/Export/' + myRect.name + '.jpg');

           myRect.exportFile(ExportFormat.JPG, myFile);

           }
       }

 }

Answer

Loic Aigon picture Loic Aigon · Jun 11, 2012

The file name isn't located on the rectangle but on the link related to the placed graphic. This should do what you want given an open document:

test();



function test() {

    var myDoc = app.activeDocument, apis = myDoc.allPageItems, rect, fileName;


    while ( rect = apis.pop() )
    {
        if ( !(rect instanceof Rectangle) || !rect.graphics[0].isValid ){ continue; }

        fileName = File ( rect.graphics[0].itemLink.filePath ).name;
        fileName = fileName.replace( /\.[a-z]{2,4}$/i, '.jpg' );

        app.jpegExportPreferences.exportResolution = 300;
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;

        //give it a unique name
        var myFile = new File (Folder.desktop+"/"+ fileName);

        rect.exportFile(ExportFormat.JPG, myFile);
    }
}