skip to Main Content

I’m very new to Photoshop scripting, so please bear with me.

So far I have the below script, but I keep getting an error that my doc object reference is invalid. Can anyone help me properly assign my documents so I can work with them in my conditional statement?

Also need help saving the document with the same file name and extension as it already has, ie even if the filename is horribly formatted like “horrible spaces.JPEG”

THANK YOU!

 var inputFolder = Folder.selectDialog("Select a folder to process");

 var fileList = inputFolder.getFiles("*.*"); //Use whatever extension you want or no extension to select all files

 for(var i=0; i<fileList.length; i++) {

 if (fileList[i] instanceof File && fileList[i].hidden == false) {
    // get a reference to the new document
    var doc = open(fileList[i])
 }    

// do the resizing.  if the width of the document is already less than 500, then don't do anything. Otherwise resize too 500 wide, keep the aspect ratio, and change the resampling.
if (doc.width < "500px") {
    // don't do anything
}
else {
    doc.resizeImage(UnitValue(500,"px"),null,null,ResampleMethod.BICUBIC);
}

    app.activeDocument.close();

}

2

Answers


  1. Chosen as BEST ANSWER

    Here's the final working script. Hope this might help someone else looking to resize images using an image width condition. Thank you!

    var inputFolder = Folder.selectDialog("Select a folder to process");
    var fileList = inputFolder.getFiles("*.*"); //Use whatever extension you want or no extension to select all files
    
    for(var i=0; i<fileList.length; i++) {
        open(fileList[i]);
    
        // do the resizing.  if the width of the document is already less than 500, then don't do anything. Otherwise resize to 500 wide, keep the aspect ratio, and change the resampling.
        if (activeDocument.width > "500") {
            activeDocument.resizeImage(UnitValue(500,"px"),null,72,ResampleMethod.BICUBIC);
            app.activeDocument.save();
        }
    
        app.activeDocument.close();
    }
    

  2. Are you running this in the Extendscript toolkit? Make sure you are targeting photoshop – otherwise your app.activeDocument object will be null. When I target photoshop your script runs fine.

    To save the document – if you want to save over the original it is as simple as calling app.activeDocument.save() before you close the document. If you want to save it to a new location, modify the snippet below. More details about the options object is available in your JavaScript Ref document in your photoshop installation directory.

    var options = new PhotoshopSaveOptions();
    options.embedColorProfile = true;
    options.alphaChannels = true; 
    
    saveFile = new File( "/some/other/folder/" + app.activeDocument.name + ".psd")
    app.activeDocument.saveAs(saveFile, options, false, Extension.LOWERCASE); 
    app.activeDocument.close();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search