Applying our Photoshop Thumbnail Script to Multiple Documents

by Chadwick Wood
May 25th, 2009

A reader asked if there was a way to apply the Photoshop Thumbnail script I wrote to all open documents, so I figured I'd post it up here for everyone to see. All it takes is a small addition and modification, to iterate through all of Photoshop's open documents, instead of just the active one. Here's the code, with comments:

// docs is a reference to the Documents collection, that holds all open documents
docs = app.documents;

for (i=0; i < docs.length; i++) {
    // instead of getting the active document, iterate through each open doc
    doc = docs.index(i);

    // the rest of the script is unchanged
    doc.changeMode(ChangeMode.RGB);

    var thumbDim = 60; // the dimension of the square

    // crop to a big square, conditionally, based on dimensions
    if (doc.height > doc.width) {
        doc.resizeCanvas(doc.width,doc.width,AnchorPosition.TOPCENTER);
    }
    else {
        doc.resizeCanvas(doc.height,doc.height,AnchorPosition.MIDDLECENTER);
    }

    // resize, then auto-contrast and sharpen
    // specify that our units are in pixels, via creation of a UnitValue object
    doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);
    doc.activeLayer.autoContrast();
    doc.activeLayer.applySharpen();

    // our web export options
    var options = new ExportOptionsSaveForWeb();
    options.quality = 70;
    options.format = SaveDocumentType.JPEG;
    options.optimized = true;

    var newName = 'thumb-'+doc.name+'.jpg';

    doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
}