Copy link to clipboard
Copied
Hi, I have encountered a weird error while trying to generate a new text layer.
When a single artboard is available in the document, the script runs fine and create an empty text layer;
When 2 or more artboards are available in the document, the scripts generates the layer, but it then gets stuck while trying to convert it to a text one. The console says "The layer cannot contain text".
Pasting my code here, with comments.
#target photoshop
//this script attempts to create a text layer above the selected layer contained into an artboard.
//For some reason, it works on the first available artboard, but not on the other ones. The error is "The layer cannot contain text"
//get active PS document
var doc = activeDocument;
//get active artboard
var currentArtboard = getActiveArtboard();
//create new art layer in the current artboard
var newLayer = currentArtboard.artLayers.add();
//trying to convert to text layer. Fails when 2+ artboards are available int the document
newLayer.name = "test";
newLayer.kind = LayerKind.TEXT;
function getActiveArtboard() {
var key = false;
var l = doc.activeLayer;
var p = l.parent;
try {
while (!key) {
doc.activeLayer = p;
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
key = executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
if (key) {
return p
}
p = p.parent;
}
} catch (e) {
alert('This layer is not contained within an artboard');
return undefined;
}
}
Any help is greatly appreciated. Thank you in advance!
...var doc = activeDocument;
var currentArtboard = getActiveArtboard();
function makeTextLayer() {
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass(app.charIDToTypeID('TxLr'));
desc.putReference(app.charIDToTypeID('null'), ref);
desc2.putString(app.charIDToTypeID('Txt '), "text");
var list2 = new ActionList();
desc2.putList(app.charIDToTypeID('Txtt'), list2);
desc.putObject(app.charIDToTypeI
Copy link to clipboard
Copied
I got this error too when I was trying to use the specKing photoshop plug-in for measuring and design specifications plugin ... Bizzaro ... worked like 2 months ago *subscribing*
Copy link to clipboard
Copied
var doc = activeDocument;
var currentArtboard = getActiveArtboard();
function makeTextLayer() {
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass(app.charIDToTypeID('TxLr'));
desc.putReference(app.charIDToTypeID('null'), ref);
desc2.putString(app.charIDToTypeID('Txt '), "text");
var list2 = new ActionList();
desc2.putList(app.charIDToTypeID('Txtt'), list2);
desc.putObject(app.charIDToTypeID('Usng'), app.charIDToTypeID('TxLr'), desc2);
executeAction(app.charIDToTypeID('Mk '), desc, DialogModes.NO);
return doc.activeLayer
}
var newLayer = makeTextLayer();
newLayer.move(currentArtboard, ElementPlacement.INSIDE);
function getActiveArtboard() {
var key = false;
var l = doc.activeLayer;
var p = l.parent;
try {
while (!key) {
doc.activeLayer = p;
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
key = executeActionGet(ref).getBoolean(stringIDToTypeID("artboardEnabled"));
if (key) {
return p
}
p = p.parent;
}
} catch (e) {
alert('This layer is not contained within an artboard');
return doc
}
}
Copy link to clipboard
Copied
This is the correct workaround. There is a bug in the current javascript interface to create text layers, I guess.