Skip to main content
Participant
June 5, 2020
Question

Can I loop an action in Photoshop ?

  • June 5, 2020
  • 5 replies
  • 7126 views

I have over 50 images stacked as 50 layers in a document and want to convert each layer into a smart object. What is the best way to do it? I thought of creating an action to covert to smart object and run it over 50 layers. But I could't find a way to loop that action. Any help would be highly appreciated. 

Thank you. 

(Ps: I saw a simier question asked by someone else and the answer was a script to run. I have no idea about scripts or coding. So couldn't really understand the reply.)

5 replies

ShynnSup
Inspiring
November 2, 2022

Hi guys, I am in need of something similar but I added a Save step to my action. But it seems that as the action is looped in the script, the save is always with the same name, overwritting each file instead of saving each layer to a separate file 😞

Stephen Marsh
Community Expert
Community Expert
November 2, 2022
quote

Hi guys, I am in need of something similar but I added a Save step to my action. But it seems that as the action is looped in the script, the save is always with the same name, overwritting each file instead of saving each layer to a separate file 😞


By @ShynnSup

 

When recording the save step in the Action, don't change/touch the filename, just navigate to the location where you wish to save. The recorded Action step will then only include the save path, not the save path + file name.

 

ShynnSup
Inspiring
November 2, 2022

Ah this actually promts me to save each time, and I need like 200 layers.

 

But you gave me another idea. I can just run the script which loops the action without saving. And then use Export>Layer to Files 😄

kirank51547421
Participant
November 17, 2021

Here is a script, put this in a file RunActionX-Times.jsx(change the extension from .txt to .jsx) and copy to your Adobe Photoshop>Presets>Scripts folder on windows.

In Photoshop, your new script should appear under File>scripts. Just type in the number of times you want an action to run and VIOLA! Works like a charm!

 

Credit to jcombs

Link to original discussion

https://www.ozzu.com/questions/455621/photoshop-play-an-action-repeatedly#post-607170

Stephen Marsh
Community Expert
Community Expert
June 5, 2020
(Ps: I saw a simier question asked by someone else and the answer was a script to run. I have no idea about scripts or coding. So couldn't really understand the reply.)

 

I created the following guide to help users such as yourself use scripts:

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html?m=1

JicejseAuthor
Participant
June 9, 2020

Thanks a lot . I have wrote a reply in detail to JJMac in this thread. I would love to have suggestions from you too. 

Stephen Marsh
Community Expert
Community Expert
June 5, 2020

I wrote such a script to create a separate SO from all top level layers/sets here, so no need for an action:

 

https://community.adobe.com/t5/photoshop/smart-objects-in-separate-layers/m-p/11137197?page=1#M331763

 

// convert all top level layers and layer sets to smart objects.jsx

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(app.activeDocument);
    }

    function processAllLayersAndSets(obj) {

        // Process all layers and layer sets
        // Change the following 2 entries of "obj.layers" to "obj.artLayers" to exclude layer sets
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            app.activeDocument.activeLayer = obj.layers[al];

            newPlacedLayer();

        }

        // Process Layer Set Layers 
        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);

            newPlacedLayer();

        }
    }

    // Convert to smart object (Cleaned AM code)
    function newPlacedLayer() {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
    }
}

activeDocument.suspendHistory('Smart Objects from Layers & Sets', 'main()');

 

That being said, the great thing about a script that plays an action is that you can record an action to do what you want without having to rely on somebody to write you a specific script.

Charu Rajput
Community Expert
Community Expert
June 5, 2020

Hi,

You can loop through all the layers using javscript and call your actions using method app.doAction

 

 

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
    doc.activeLayer = doc.layers[i];
    app.doAction("Action 1","Set 1");
}

 

 

app.doAction will take two parameters, first is Name of the action(here "Action 1") and second is Set Name (here "Set 1") in which exists. You can change both according to your actions defined in the photoshop.

 

Let us know if this works for you.

 

 

Best regards
JJMack
Community Expert
Community Expert
June 6, 2020

That script would just process top level layers to process all layers you need to use recursion.

 

 

var allLayers = "";
processArtLayers(activeDocument) 
alert(allLayers, "Layers");
function processArtLayers(obj) {  
    for( var i = obj.artLayers.length-1; 0 <= i; i--) {/* alert(obj.artLayers[i]); */processLayers(obj.artLayers[i])}  
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {/* alert(obj.layerSets[i]); */processArtLayers(obj.layerSets[i])} // Process Layer Set Layers  
} 
function processLayers(layer) { 
	allLayers = allLayers + "ID " + layer.id + ", " + layer.name + ", " + layer.kind + "\n";
	switch (layer.kind){
	default : break; // none process layer types catch all
	}
} 
JJMack
Charu Rajput
Community Expert
Community Expert
June 6, 2020

That's 100% correct. The above snippet will only convert the top most layers.

Here is the new version, that will convert all layers inside the groups into smart objects 🙂

 

 

#target photoshop

var doc = app.activeDocument;

function covertLayersToSmartObjects(_layers){
    for (var i = 0; i < _layers.length; i++) {
        if(_layers[i].layers && _layers[i].layers.length)
            covertLayersToSmartObjects(_layers[i].layers)
        else{
            doc.activeLayer = _layers[i];
            app.doAction("Action 1","Set 1");
        }
     }
}

covertLayersToSmartObjects(doc.layers);

 

Best regards