0

I'm working with a script in Photoshop that will automatically resize all smart objects to their original sizes. I got the script from here. Kudos to Barbara_Ash for directing me to it. The script was made by LeafArrow20 for Photoshop CC but I want to use it in CS6. I believe that the script needs some function/method translation but I don't know what CS6 uses. Any help translating this would be a huge help. Here is my refined version of the script:

#target photoshop
var doc = app.activeDocument;
function resizeSmartObject() {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
var layerType = executeActionGet(ref).getInteger(stringIDToTypeID("layerKind"));
if (layerType == "5") {
    var obj = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObjectMore"));
    with (obj) {
        var _tmp = getObjectValue(stringIDToTypeID("size"));
        var size = new Object({
            width:  _tmp.getDouble(stringIDToTypeID("width")),
            height: _tmp.getDouble(stringIDToTypeID("height")),
        });
    }
    var bounds = app.activeDocument.activeLayer.bounds;
    var w_new = (size.width/(bounds[2].value - bounds[0].value))*100;
    var h_new = (size.height/(bounds[3].value - bounds[1].value))*100;
    app.activeDocument.activeLayer.resize(w_new, h_new);
    return;
} else { return; }
}
function cycleLayers (set){
     for (var i = 0; i < set.layers.length; i++){
        doc.activeLayer = set.layers;
        if(doc.activeLayer.typename == 'LayerSet'){
            if(doc.activeLayer.layers.length > 0) {
                cycleLayers(doc.activeLayer);
            }
        } else {
            resizeSmartObject();
        }
     }
}
cycleLayers(doc);

I'm getting errors specifically on line 25 (doc.activeLayer = set.layers;).

1

There's an error in the original code, it's not supposed to work. Maybe the author forgot to post the correct answer? The good news that it's easy to fix: the line #25 should be like this:

    doc.activeLayer = set.layers[i];

Otherwise it was trying to assign a whole set to an active layer which doesn't make sense.

However, the bad news is that this script uses a variable smartObjectMore only available from Photoshop CC: this variable contains data about the original dimensions of the Smart Layer. So this script won't work in CS6.

But then again, there's a good news. There's a different approach to resetting smart objects which basically includes unpacking it first and then repacking again: this will work in CS6. This script is included in a free pack of some other scripts I made. By default the script works on selected layers, but you can use the same cycleLayers() you have now to run it on every smart layer in the document, the code will be like this:

function main() {

    //determines path to the current script
    var thisScriptPath = ($.fileName).split('/');
    thisScriptPath.pop();
    thisScriptPath = thisScriptPath.join('/')

    var cycleLayers = function(set)
    {
        for (var i = 0; i < set.layers.length; i++)
        {
            activeDocument.activeLayer = set.layers[i];
            if (activeDocument.activeLayer.typename == 'LayerSet')
            {
                if (activeDocument.activeLayer.layers.length > 0)
                {
                    cycleLayers(activeDocument.activeLayer);
                }
            }
            else
            {
                // runs sk_resetsmarts.jsx, make sure current script is in the same folder as sk_resetsmarts.jsx or manually provide a path here
                $.evalFile(thisScriptPath + "/sk_resetsmarts.jsx");
            }
        }
    };

    cycleLayers(activeDocument);
}
//main()
app.activeDocument.suspendHistory("reset all smart layers in the document", "main()");
  • Thanks for the reply, but I'm having some trouble setting the reference path for the script. I have mine located here: %PROGRAMFILES%\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts\Scriptorator. I set /Scriptorator/sk_resetsmarts.jsx on line 23 but I get an error each time. – Mr. Mendelli Sep 22 at 18:48
  • 1
    I don't think you can use environment variables in Photoshop scripts. Just use a full path and make sure you have sk_libs.jsx in the Scriptorator folder – Sergey Kritskiy Sep 22 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.