Copy link to clipboard
Copied
Thanks for the help.
@Delta2487 wrote:
Can anyone help me? I want to convert folders that match the first two search characters into smart objects.
For example:
search criteria: "A:" or "a:", "S.", "N."
Thanks for the help.
Try this code, it will search over all root/top-level layers or groups for names beginning with "A" or "S" or "N" (case insensitive) followed by ":" or "." – it can be easily changed to only work on layer groups if you need it more specific.
/*
Convert Specific Named Layers to Embedded S
...
Find this line in the code:
if (/^[ASN][:.]/i.test(layer.name)) {
The [ASN] bit controls the first letter (case insensitive), so add/remove as needed inside the square brackets.
The [:.] bit controls the characters after the letter, so add/remove as needed inside the square brackets.
When I have time, I'll try to make the script more user-friendly by adding a user interface prompt to enter characters, rather than you having to change the code.
Copy link to clipboard
Copied
@Delta2487 wrote:
Can anyone help me? I want to convert folders that match the first two search characters into smart objects.
For example:
search criteria: "A:" or "a:", "S.", "N."
Thanks for the help.
Try this code, it will search over all root/top-level layers or groups for names beginning with "A" or "S" or "N" (case insensitive) followed by ":" or "." – it can be easily changed to only work on layer groups if you need it more specific.
/*
Convert Specific Named Layers to Embedded Smart Objects.jsx
Stephen Marsh
v1.0 - 22nd November 2024
https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-part-of-the-folder-name-and-convert-it-to-a-smart-object/m-p/14996056
*/
#target photoshop;
try {
app.activeDocument.suspendHistory("Specific Layers to Embedded Smart Objects...", "main()");
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var layers = doc.layers;
// Select the top layer if no layer is selected, no change if layer is selected, current visibility is honoured
// by r-bin
try {
activeDocument.activeLayer.link(activeDocument.activeLayer);
} catch (e) { }
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
// Check if the active layer starts with "A" or "S" or "N" followed by ":" or "." (case insensitive)
// if (/^(A:|a:|S:|s:|N:|n:|A\.|a\.|S\.|s\.|N\.|n\.)/.test(layer.name)) {
if (/^[ASN][:.]/i.test(layer.name)) {
// Save the label color
var labelColor = getLayerColor(layer);
// Select the layer and convert to an embedded smart object
doc.activeLayer = layer;
convertLayerToSmartObject();
// Reapply the label color
setLayerColor(doc.activeLayer, labelColor);
}
}
app.beep();
//alert("Script completed!");
} else {
alert("No active document.");
}
}
} catch (err) {
alert("Main Script Error!" + "\r" + err + "\r" + 'Line: ' + err.line);
}
// Helper functions
function getLayerColor(layer) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("color"));
ref.putIdentifier(stringIDToTypeID("layer"), layer.id);
var desc = executeActionGet(ref);
return desc.getEnumerationValue(stringIDToTypeID("color"));
} catch (e) {
alert("Error getting layer color: " + "\r" + e + "\r" + 'Line: ' + e.line);
return null;
}
}
function setLayerColor(layer, color) {
try {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(stringIDToTypeID("layer"), layer.id);
desc.putReference(stringIDToTypeID("null"), ref);
var colorDesc = new ActionDescriptor();
colorDesc.putEnumerated(stringIDToTypeID("color"), stringIDToTypeID("color"), color);
desc.putObject(stringIDToTypeID("to"), stringIDToTypeID("layer"), colorDesc);
executeAction(stringIDToTypeID("set"), desc, DialogModes.NO);
} catch (e) {
alert("Error setting layer color: " + "\r" + e + "\r" + 'Line: ' + e.line);
}
}
function convertLayerToSmartObject() {
try {
var idNewPlacedLayer = stringIDToTypeID("newPlacedLayer");
executeAction(idNewPlacedLayer, undefined, DialogModes.NO);
} catch (e) {
alert("Error converting layer to Smart Object: " + "\r" + e + "\r" + 'Line: ' + e.line);
}
}
P.S. Do you really need this to work on selected layers, and retain the selected layers afterwards?
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Copy link to clipboard
Copied
@Delta2487 – Are you still out there?
Copy link to clipboard
Copied
Hi Esteban, thank you very much, it works perfectly, if I only want the highest level, but if I want to change the prefixes for another letter like V: or J: how do I do it, thank you.
Copy link to clipboard
Copied
Find this line in the code:
if (/^[ASN][:.]/i.test(layer.name)) {
The [ASN] bit controls the first letter (case insensitive), so add/remove as needed inside the square brackets.
The [:.] bit controls the characters after the letter, so add/remove as needed inside the square brackets.
When I have time, I'll try to make the script more user-friendly by adding a user interface prompt to enter characters, rather than you having to change the code.
Copy link to clipboard
Copied
Ok thanks, I'll wait for it.... 🙂
And do you have the code to do this:
Copy link to clipboard
Copied
I already replied with the answer in your other topic:
Copy link to clipboard
Copied
If I just saw it, the site didn't notify me, thank you very much
Copy link to clipboard
Copied
This 1.1 version offers prompts for the Primary and Secondary character input:
/*
Convert Specific Named Layers to Embedded Smart Objects.jsx
Stephen Marsh
v1.1 - 17th December 2024
https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-part-of-the-folder-name-and-convert-it-to-a-smart-object/m-p/14996056
*/
#target photoshop;
try {
app.activeDocument.suspendHistory("Specific Layers to Embedded Smart Objects...", "main()");
function main() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var layers = doc.layers;
// Prompt the user for the starting characters to match
var prefix = prompt("Enter multiple comma separated Primary characters to match (eg., A, S, N):", "A, S, N", "Layer Name Filter");
if (!prefix) {
alert("No characters entered. Script canceled.");
return;
}
// Prompt for the second character (e.g., ':' or '.')
var suffix = prompt("Enter multiple comma separated Secondary characters to match (eg., :, .): ", ":, .", "Layer Name Filter");
if (!suffix) {
alert("Invalid second character entered. Script canceled.");
return;
}
// Clean input and build regex pattern
var prefixCharacters = prefix.replace(/\s+/g, ""); // Remove spaces
if (prefixCharacters.length === 0) {
alert("No valid characters entered. Script canceled.");
return;
}
var suffixCharacters = suffix.replace(/\s+/g, ""); // Remove spaces
if (suffixCharacters.length === 0) {
alert("No valid characters entered. Script canceled.");
return;
}
// Construct regex pattern
var regexPattern = "^[" + prefixCharacters.split(",").join("") + "][" + suffixCharacters + "]";
var regex = new RegExp(regexPattern, "i"); // Case-insensitive regex
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
// Test the layer name against the built regex
if (regex.test(layer.name)) {
// Save the label color
var labelColor = getLayerColor(layer);
// Select the layer and convert to an embedded smart object
doc.activeLayer = layer;
convertLayerToSmartObject();
// Reapply the label color
setLayerColor(doc.activeLayer, labelColor);
}
}
app.beep();
alert("Script completed!");
} else {
alert("No active document.");
}
}
} catch (err) {
alert("Main Script Error!" + "\r" + err.message + "\r" + 'Line: ' + (err.line || 'unknown'));
}
// Helper functions
function getLayerColor(layer) {
try {
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("color"));
ref.putIdentifier(stringIDToTypeID("layer"), layer.id);
var desc = executeActionGet(ref);
return desc.getEnumerationValue(stringIDToTypeID("color"));
} catch (e) {
alert("Error getting layer color: " + "\r" + e.message + "\r" + 'Line: ' + (e.line || 'unknown'));
return null;
}
}
function setLayerColor(layer, color) {
try {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIdentifier(stringIDToTypeID("layer"), layer.id);
desc.putReference(stringIDToTypeID("null"), ref);
var colorDesc = new ActionDescriptor();
colorDesc.putEnumerated(stringIDToTypeID("color"), stringIDToTypeID("color"), color);
desc.putObject(stringIDToTypeID("to"), stringIDToTypeID("layer"), colorDesc);
executeAction(stringIDToTypeID("set"), desc, DialogModes.NO);
} catch (e) {
alert("Error setting layer color: " + "\r" + e.message + "\r" + 'Line: ' + (e.line || 'unknown'));
}
}
function convertLayerToSmartObject() {
try {
var idNewPlacedLayer = stringIDToTypeID("newPlacedLayer");
executeAction(idNewPlacedLayer, undefined, DialogModes.NO);
} catch (e) {
alert("Error converting layer to Smart Object: " + "\r" + e.message + "\r" + 'Line: ' + (e.line || 'unknown'));
}
}
Copy link to clipboard
Copied
If it is not too much to ask, can you do it with layers selected in some option, thank you.
Copy link to clipboard
Copied
If it is not too much to ask, can you do it with layers selected in some option, thank you.
By @Delta2487
The following v1.0 code will convert selected layers to embedded smart objects. It doesn't retain the layer label colour, nor does it retain the selected layers. It does what you asked, for now, it is what it is without any fancy extras! :]
/*
Selected Layers to Smart Object.jsx
Stephen Marsh
v1.0 - 3rd February 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/find-part-of-the-folder-name-and-convert-it-to-a-smart-object/m-p/15128286
Related script:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-use-quot-new-smart-object-via-copy-quot-in-bulk/td-p/14096358
*/
#target photoshop
// Single history stage undo
activeDocument.suspendHistory("Randomly rotate selected layers", "main()");
function main() {
// Capture the initial layer visibility and layer selection
var currentLayersState = getLayersVisiblity();
// Get the selected layers: courtesy of jazz-y
var s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
// Loop over the selected layers: courtesy of jazz-y
for (var l = 0; l < lrs.count; l++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
(r = new ActionReference()).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
//d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t('select'), d, DialogModes.NO);
// Create a new embedded smart object from the selected layers
executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
}
// Restore the initial layer visibility and selection
setLayersVisiblity(currentLayersState);
}
///// Functions /////
function getLayersVisiblity() {
// by jazz-y
var s2t = stringIDToTypeID,
t2s = typeIDToStringID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var targetLayers = executeActionGet(r).getList(p),
seletion = [],
visiblity = {};
for (var i = 0; i < targetLayers.count; i++) seletion.push(targetLayers.getReference(i).getIdentifier());
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(r).getInteger(p);
for (var j = 1; j <= len; j++) {
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerSection'));
r.putIndex(s2t('layer'), j);
if (t2s(executeActionGet(r).getEnumerationValue(p)) == 'layerSectionEnd') continue;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
r.putIndex(s2t('layer'), j);
var id = executeActionGet(r).getInteger(p);
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('visible'));
r.putIndex(s2t('layer'), j);
var visible = executeActionGet(r).getBoolean(p);
visiblity[id] = visible;
}
return {
selection: seletion,
visiblity: visiblity
};
}
function setLayersVisiblity(layersStateObject) {
// by jazz-y
var s2t = stringIDToTypeID;
for (var a in layersStateObject.visiblity) {
makeVisible = layersStateObject.visiblity[a] ? "show" : "hide";
(r = new ActionReference()).putIdentifier(s2t('layer'), a);
(d = new ActionDescriptor()).putReference(s2t('target'), r);
executeAction(s2t(makeVisible), d, DialogModes.NO);
}
if (layersStateObject.selection.length) {
var r = new ActionReference();
for (var i = 0; i < layersStateObject.selection.length; i++)
r.putIdentifier(s2t("layer"), layersStateObject.selection[i]);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t("select"), d, DialogModes.NO);
} else {
(r = new ActionReference()).putEnumerated(s2t("layer"), s2t('ordinal'), s2t('targetEnum'));
(d = new ActionDescriptor()).putReference(s2t('target'), r);
executeAction(s2t('selectNoLayers'), d, DialogModes.NO);
}
}
P.S. There are other versions here:
Copy link to clipboard
Copied
Sometimes I get this error, when I don't select a layer
Thank you very much for the script, it is a great tool, thank you very much
Copy link to clipboard
Copied
Sometimes I get this error, when I don't select a layer
Thank you very much for the script, it is a great tool, thank you very much
By @Delta2487
When this message occurs, does the layer have a colour?
You can // comment out the following line to remove the error message:
alert("Error setting layer color: " + "\r" + e.message + "\r" + 'Line: ' + (e.line || 'unknown'));
To:
//alert("Error setting layer color: " + "\r" + e.message + "\r" + 'Line: ' + (e.line || 'unknown'));