Search code examples
automationphotoshopextendscript

How to automate Photoshop?


I am trying to automate the process of scanning/cropping photos in Photoshop. I need to scan 3 photos at a time, then use Photoshop's Crop and Straighten Photos command, which creates 3 separate images. After that I'd like to save each of the newly created images as a PNG.

I looked at the JSX scripts and they seem to a lot of promise. Is what I described possible to automate in Photoshop using JavaScript or VBScript or whatever?


Solution

  • I actually got the answer on the Photoshop forums over at adobe. It turns out that Photoshop CS4 is totally scriptable via JavaScript, VBScript and comes with a really kick-ass Developer IDE, that has everything you'd expect (debugger, watch window, color coding and more). I was totally impressed.

    Following is an extract for reference:

    you can run the following script that will create a new folder off the existing one and batch split all the files naming them existingFileName#001.png and put them in the new folder (edited)

    #target Photoshop
    app.bringToFront;
    var inFolder = Folder.selectDialog("Please select folder to process"); 
    if(inFolder != null){
        var fileList = inFolder.getFiles(/\.(jpg|tif|psd|)$/i);
        var outfolder = new Folder(decodeURI(inFolder) + "/Edited");
        if (outfolder.exists == false) outfolder.create();
        for(var a = 0 ;a < fileList.length; a++){
        if(fileList[a] instanceof File){
            var doc= open(fileList[a]);
            doc.flatten();
            var docname = fileList[a].name.slice(0,-4);
            CropStraighten();
            doc.close(SaveOptions.DONOTSAVECHANGES); 
            var count = 1;
            while(app.documents.length){
                var saveFile = new File(decodeURI(outfolder) + "/" + docname +"#"+ zeroPad(count,3) + ".png");
                SavePNG(saveFile);
                activeDocument.close(SaveOptions.DONOTSAVECHANGES) ; 
                count++;
                }
            }
        }
    };
    function CropStraighten() {
        function cTID(s) { return app.charIDToTypeID(s); };
        function sTID(s) { return app.stringIDToTypeID(s); };
        executeAction( sTID('CropPhotosAuto0001'), undefined, DialogModes.NO );
    };
    function SavePNG(saveFile){
        pngSaveOptions = new PNGSaveOptions(); 
        pngSaveOptions.embedColorProfile = true; 
        pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
        pngSaveOptions.matte = MatteType.NONE; 
        pngSaveOptions.quality = 1; 
        pngSaveOptions.PNG8 = false; //24 bit PNG
        pngSaveOptions.transparency = true; 
        activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
    }
    
    function zeroPad(n, s) { 
        n = n.toString(); 
        while (n.length < s) n = '0' + n; 
        return n; 
    };
    

    Visit here for complete post.