Skip to main content
Participant
June 3, 2022
Answered

Script to run action on all smart objects

  • June 3, 2022
  • 2 replies
  • 582 views

Hi, 

 

I am brand new to scripting and I've pieced together the below from other forum posts. I am trying to open all smart objects in a document and clear guides with one click (or shortcut).

 

It is currently working when I run it with a layer selected, but I am having trouble with how to make it run for all of the layers in the document, not just the selected/active layer. I appreciate any help or suggestions!

 

if (app.activeDocument.activeLayer.kind == "LayerKind.SMARTOBJECT") {
    app.doAction("ClearGuides", "SmartObjects");
}

 

Thank you!

This topic has been closed for replies.
Correct answer Stephen Marsh

@EliseClaire – welcome to scripting!

 

Try this:

 

/*
Remove Guides From All Smart Object Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-run-action-on-all-smart-objects/td-p/12983602
Stephen Marsh, 4th June 2022 - v1.0
*/

#target photoshop

var myDocument = app.activeDocument;
myDocument.suspendHistory("processAllSmartObjects", "processSOLayers(myDocument)");

// processSOLayers function courtesy of c.pfaffenbichler
function processSOLayers(theParent) {
    // Loop over all the layers
    for (var i = theParent.layers.length - 1; i >= 0; i--) {
        var theLayer = theParent.layers[i];
        // Apply the function to smart object layers
        if (theLayer.typename === "ArtLayer") {
            if (theLayer.kind === LayerKind.SMARTOBJECT) {
                var theVisibility = theLayer.visible;
                myDocument.activeLayer = theLayer;

                // Run the function to remove guides
                removeGuidesFromSOlayers();
                
                // Reset visibility; 
                theLayer.visible = theVisibility;
            }
            // Run the function on the smart object layers inside layerSets (implied, not explicitly defined)
        } else {
            processSOLayers(theLayer);
        }
    }
    return;

    function removeGuidesFromSOlayers() {
        app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
        var idclearAllGuides = stringIDToTypeID("clearAllGuides");
        executeAction(idclearAllGuides, undefined, DialogModes.NO);
        activeDocument.close(SaveOptions.SAVECHANGES);
    }
}

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 4, 2022

@EliseClaire – welcome to scripting!

 

Try this:

 

/*
Remove Guides From All Smart Object Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-run-action-on-all-smart-objects/td-p/12983602
Stephen Marsh, 4th June 2022 - v1.0
*/

#target photoshop

var myDocument = app.activeDocument;
myDocument.suspendHistory("processAllSmartObjects", "processSOLayers(myDocument)");

// processSOLayers function courtesy of c.pfaffenbichler
function processSOLayers(theParent) {
    // Loop over all the layers
    for (var i = theParent.layers.length - 1; i >= 0; i--) {
        var theLayer = theParent.layers[i];
        // Apply the function to smart object layers
        if (theLayer.typename === "ArtLayer") {
            if (theLayer.kind === LayerKind.SMARTOBJECT) {
                var theVisibility = theLayer.visible;
                myDocument.activeLayer = theLayer;

                // Run the function to remove guides
                removeGuidesFromSOlayers();
                
                // Reset visibility; 
                theLayer.visible = theVisibility;
            }
            // Run the function on the smart object layers inside layerSets (implied, not explicitly defined)
        } else {
            processSOLayers(theLayer);
        }
    }
    return;

    function removeGuidesFromSOlayers() {
        app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
        var idclearAllGuides = stringIDToTypeID("clearAllGuides");
        executeAction(idclearAllGuides, undefined, DialogModes.NO);
        activeDocument.close(SaveOptions.SAVECHANGES);
    }
}

 

Participant
June 7, 2022

This worked perfectly!

I have some researching to do to fully understand how it works but I hope to get there.... Thank you so much!

Legend
June 3, 2022
layerUpdate();

function layerUpdate(){
    if(documents.length > 0){
        var originalDialogMode = app.displayDialogs;
        app.displayDialogs = DialogModes.ERROR;
        var originalRulerUnits = preferences.rulerUnits;
        try{
            var docRef = activeDocument;
            for(var i = 0; i < docRef.artLayers.length; i++){
                var LayerRef = docRef.artLayers[i];
                if(LayerRef.kind == LayerKind.SMARTOBJECT){
                    //do stuff
                    }
                }
            preferences.rulerUnits = originalRulerUnits;
            app.displayDialogs = originalDialogMode;
            }
        catch(e){
            }
        }
    }