Indesign CS6 Scripting - Exporting images

2019-04-01 20:46发布

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);

           }
       }

 }

2条回答
Animai°情兽
2楼-- · 2019-04-01 21:00

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);
    }
}
查看更多
Viruses.
3楼-- · 2019-04-01 21:05

Just adding my verbose version of this, which works from the current selection in InDesign and provides console feedback. It renames the images with the prefix "crop_" and saves them to ~/temp

exportSelectedImages();

function exportSelectedImages() {
    // configure export settings
    app.jpegExportPreferences.exportResolution = 72;
    app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH;

    // collect selected objects
    var selected = app.activeDocument.selection;
    $.writeln("Got " + selected.length + " selected objects...");

    // process selected objects
    for (var i = 0; i < selected.length; i++) {
        var cursor = selected[i];
        var img = cursor.images;

        $.writeln("Processing #" + (i+1) + "/" + selected.length);
        $.writeln("\t Type: " + cursor.constructor.name);

        // verify if object contains an image or not
        if (cursor.images.length > 0) {     
            var img = cursor.images[0];
            $.writeln("\t Contains image of type " + img.imageTypeName);
            var imageFileName = cursor.images[0].itemLink.name;
            $.writeln("\t File Name: " + imageFileName);
        } else {
            $.writeln("\t Not an image");
        }

        // save the object to a jpeg in path specified below
        var myFile = new File('~/temp/' + "crop_" + imageFileName + '.jpg');
        cursor.exportFile(ExportFormat.JPG, myFile);

     }

    $.writeln("Done.");
}
查看更多
登录 后发表回答