Copy link to clipboard
Copied
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.)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
}
}
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
IMO its a strange request without knowing exactly what their stack of image look like. If any are placed images they are smart object layers to begin with. You can convert smart object layer to smart object layers but the object is no longer an images file its a smart object layer. I also do not think you would want to convert any adjustment layers to a smart object layer they would most likely not adjust anything. Converting a text layer to a smart object layer makes it necessary for you to open the object to edit the text. That script can play havoc on a document.
Copy link to clipboard
Copied
Well, I didn't detail the purpose in my question. Well, here's the situaion. (TLDR + bad English alert is ON)
I do product photography for my own Optical business and want to put each image on perfect white background to display it online. I work on my own , have many products and it's a hard to work on one image at a time. So this is my workflow:
I would also appreciate, if there's better workflow you could suggest. But only if there was some option to convert all the layers/groups to SO, it would've been so much easier for me.
Ps: I tried the script by Charu Rajput. ( Thank you Stephen for your detailed note on how to use the scripts). I pasted it on my TextEdit and saved it as jsx in the script folder. But the file is grayed out. Tried rebooting PS and Mac but no use. Any suggestions?
Thanks a lot for your patience and time.
Copy link to clipboard
Copied
I wrote such a script to create a separate SO from all top level layers/sets here, so no need for an action:
// 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.
Copy link to clipboard
Copied
(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
Copy link to clipboard
Copied
Thanks a lot . I have wrote a reply in detail to JJMac in this thread. I would love to have suggestions from you too.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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 😞
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 😄
Copy link to clipboard
Copied
Automate/Batch offers the override action save as checkbox.
Copy link to clipboard
Copied
You are right, I can just record my action and use Batch to run it across all my files instead of loading them as layers and then saving each layer again, which took ages. Thank you!