Copy link to clipboard
Copied
I'm using this script but it can make one text.
Is it possible that selected imge layers to be changed to text layers together? T T
Voilà:
if (app.documents.length > 0) mainScript();
function mainScript() {
try {
var myLayerName = activeDocument.activeLayer.name;
var myLayerText = activeDocument.artLayers.add();
myLayerText.name = myLayerName ;
myLayerText.kind = LayerKind.TEXT;
myLayerText.textItem.contents = myLayerName;
} catch (errStr) {
alert(errStr);
}
}
There are many examples in the forum. You could try one of the following two scripts to process selected layers:
// from jazz-y
#target photoshop;
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();
for (var i = 0; i < lrs.count; i++) {
sel.putIdentifier(s2t('layer'), p =
...
Copy link to clipboard
Copied
Please post before/after screenshots, or better yet, sample PSD files clearly indicating what you want.
Copy link to clipboard
Copied
Hi, Stephen 🙂
I bring some new images for experation below.
I need to change selected image layers to text layers(same image-layer name)
but I can't find the way. @ @
Copy link to clipboard
Copied
*experation > explanation
Copy link to clipboard
Copied
There are many examples in the forum. You could try one of the following two scripts to process selected layers:
// from jazz-y
#target photoshop;
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();
for (var i = 0; i < lrs.count; i++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).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);
// Your code here:
alert(activeDocument.activeLayer.name);
}
Or:
/*
Process Selected Layers
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/do-action-for-selected-layer/td-p/12604989
*/
var selectedLayers = getSelectedLayersIdx();
for (var a in selectedLayers) {
makeActiveByIndex(Number(selectedLayers[a]));
// Your code here
alert(app.activeDocument.activeLayer.name);
}
function getSelectedLayersIdx() {
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
desc = desc.getList(stringIDToTypeID('targetLayers'));
var c = desc.count;
for (var i = 0; i < c; i++) {
try {
activeDocument.backgroundLayer;
selectedLayers.push(desc.getReference(i).getIndex());
} catch (e) {
selectedLayers.push(desc.getReference(i).getIndex() + 1);
}
}
} else {
ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
try {
activeDocument.backgroundLayer;
selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1);
} catch (e) {
selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")));
}
}
return selectedLayers;
}
function makeActiveByIndex(idx, visible, add) {
if (visible === undefined) visible = false; // Boolean to make the selected layer visible
if (add === undefined) add = false;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), idx);
desc.putReference(charIDToTypeID("null"), ref);
if (add) desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
desc.putBoolean(charIDToTypeID("MkVs"), visible);
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
}
Copy link to clipboard
Copied
Oh my god, how could you solve this T T
It works perfect! Thank you so much! 🙂
I want to learn about photoshop script coding but it hard to find the study program in Korea.
Is there any photoshop scripting class abroad?
Copy link to clipboard
Copied
Oh my god, how could you solve this T T
It works perfect! Thank you so much! 🙂
By @wellearn
You're welcome! I didn't write the code, I just curated it.
In this example, the initial layer visibility and selection is restored after the script loops over each selected layer.
/*
Special thanks jazz-y for the code to work with multiple selected layers
*/
#target photoshop
// 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);
// Your code here:
alert(activeDocument.activeLayer.name);
}
// 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);
}
}
Copy link to clipboard
Copied
Ah-ha, I understand it.
Later after work, I will see the code again and study it. 🙂
Have a good day, Stephen~