Skip to main content
Inspiring
January 24, 2019
Question

Script Timeout?

  • January 24, 2019
  • 2 replies
  • 3101 views

Im seeing a strange issue with this script.  When selecting more than 75 or so of images, the script appears to simply stall out.  It fails to process any of the images nor replace the smart object with any of the selected images.  very strange.

any ideas on what is causing this behavior?

// replace smart object’s content and save psd;

// 2011, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

    var myDocument = app.activeDocument;

    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

    var thePath = myDocument.path;

    var theLayer = myDocument.activeLayer;

    // psd options;

    psdOpts = new PhotoshopSaveOptions();

    psdOpts.embedColorProfile = true;

    psdOpts.alphaChannels = true;

    psdOpts.layers = true;

    psdOpts.spotColors = true;

    // check if layer is smart object;

    if (theLayer.kind != "LayerKind.SMARTOBJECT") {

        alert("selected layer is not a smart object")

    } else {

        // select files;

        if ($.os.search(/windows/i) != -1) {

            var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)

        } else {

            var theFiles = File.openDialog("please select files", getFiles, true)

        };

        if (theFiles) {

            // work through the array;

            for (var m = 0; m < theFiles.length; m++) {

                // replace smart object;

                theLayer = replaceContents(theFiles, theLayer);

                var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

                //save jpg;

                var jpegOptions = new JPEGSaveOptions();

                jpegOptions.quality = 10;

                jpegOptions.embedColorProfile = true;

                jpegOptions.matte = MatteType.NONE;

                myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);

            }

        }

    }

};

////// get psds, tifs and jpgs from files //////

function getFiles(theFile) {

    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {

        return true

    };

};

////// replace contents //////

function replaceContents(newFile, theSO) {

    app.activeDocument.activeLayer = theSO;

    // =======================================================

    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID("null");

    desc3.putPath(idnull, new File(newFile));

    var idPgNm = charIDToTypeID("PgNm");

    desc3.putInteger(idPgNm, 1);

    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

    return app.activeDocument.activeLayer

};

This topic has been closed for replies.

2 replies

Participating Frequently
January 25, 2019

Bonjour

I'm not sure I understand everything




But you should take a look HERE!

Legend
January 24, 2019

Try this version.

What is the OS and version of Photoshop?

I have everything is working.

try

{

    if (app.documents.length > 0) {

        var myDocument = app.activeDocument;

        try { var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; } catch(e) { alert(e); }

        var thePath = myDocument.path;

        var theLayer = myDocument.activeLayer;

        // psd options;

        psdOpts = new PhotoshopSaveOptions();

        psdOpts.embedColorProfile = true;

        psdOpts.alphaChannels = true;

        psdOpts.layers = true;

        psdOpts.spotColors = true;

   

        // check if layer is smart object;

        if (theLayer.kind != "LayerKind.SMARTOBJECT") {

            alert("selected layer is not a smart object")

        } else {

            // select files;

            if ($.os.search(/windows/i) != -1) {

                var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)

            } else {

                var theFiles = File.openDialog("please select files", getFiles, true)

            };

alert("theFiles.length="+theFiles.length);

   

            if (theFiles) {

                // work through the array;

                for (var m = 0; m < theFiles.length; m++) {

                    // replace smart object;

alert("try to process file:\n"+theFiles);

                    theLayer = replaceContents(theFiles, theLayer);

                    var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

                    //save jpg;

                    var jpegOptions = new JPEGSaveOptions();

                    jpegOptions.quality = 10;

                    jpegOptions.embedColorProfile = true;

                    jpegOptions.matte = MatteType.NONE;

                    myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);

                }

            }

        }

    };

}

catch(e) { alert(e); }

alert("Done!");

////// get psds, tifs and jpgs from files //////

function getFiles(theFile) {

try

{

    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {

        return true

    };

}

catch(e) { alert(e); }

};

////// replace contents //////

function replaceContents(newFile, theSO) {

try

{

    app.activeDocument.activeLayer = theSO;

    // =======================================================

    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID("null");

    desc3.putPath(idnull, new File(newFile));

    var idPgNm = charIDToTypeID("PgNm");

    desc3.putInteger(idPgNm, 1);

    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);

    return app.activeDocument.activeLayer

}

catch(e) { alert(e); }

};

Inspiring
January 24, 2019

Windows 10

i7, 32GB Ram, 1TB hd

 

Photoshop CC 20.0.2 release

 

thank you, i'll give your script a try and let you know the results

 

 

ran modified script.

 

Error 21:null is not an object.

Line:29

-> alert("theFiles.length=" + theFiles.length);

 

 

thank you again for posting your code with alerts added.

 

in running it, i did see the issue occur at ~750 image files.   it did work at 725 image files.

is there a limit setting somewhere?   this is very odd

Legend
January 24, 2019
Known problem of Windows. The system FiliOpenDialog cannot return a file list if it does not fit in a string (I don’t remember exactly) about 64k characters. You probably have long file names. It is easier for you to rewrite the script to handle the whole folder in this case than to select such a large number of files.