Copy link to clipboard
Copied
I've been working on a script with a lot of help from the community here. Very thankful for all of it.
I have run into a bug. This is a script that I only run on text layers. The script will perform some tasks that some of it depends on whether or not Leading is set to (Auto) or a user defined setting. It worked great until this morning when I came across an instance where it told me that there was an error in the code (line marked in red) and that there was no such attribute to the layer. This particular text layer was set to Auto Leading. If I converted it to a specific leading and then back to Auto the script would work, but of course I don't want to do that every time I come across this.
Any ideas, o' Scripting Community?
A snippet of my script:
var doc = activeDocument
var lay = doc.activeLayer
var textSize = lay.textItem.size
var textLayer = doc.activeLayer
var myFont = app.fonts.getByName(textLayer.textItem.font).name;
var textHeight = textLayer.bounds[3]-textLayer.bounds[1]
var textWidth = textLayer.bounds[2]-textLayer.bounds[0]
var docHeight = doc.height
docHeight = docHeight.toString().replace(' px', '');
var docWidth = doc.width
docWidth = docWidth.toString().replace(' px', '');
var specLayName = lay.name
var docName = doc.name
var tempDoc = """Temp File For Script"""
// Set leading offset
var ogHeight = getBoundingBox(textSize, multiplier);
var leadOffset = ogHeight;
// Convert to Auto Leading?
{// ========================================
if (lay.textItem.useAutoLeading == false){
var leadOffset = activeDocument.activeLayer.textItem.leading
{if (confirm('Can I convert this to Auto Leading for you?'))
{
// Make Snapshot======================
var idMk = charIDToTypeID( "Mk " );
var desc61 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref28 = new ActionReference();
var idSnpS = charIDToTypeID( "SnpS" );
ref28.putClass( idSnpS );
desc61.putReference( idnull, ref28 );
var idFrom = charIDToTypeID( "From" );
var ref29 = new ActionReference();
var idHstS = charIDToTypeID( "HstS" );
var idCrnH = charIDToTypeID( "CrnH" );
ref29.putProperty( idHstS, idCrnH );
desc61.putReference( idFrom, ref29 );
executeAction( idMk, desc61, DialogModes.NO );
alert ("I created a History Snapshot if you need to revert back to your custom "+(activeDocument.activeLayer.textItem.leading) +" leading.");
lay.textItem.useAutoLeading = true
var leadOffset = ogHeight}
}}
}
Copy link to clipboard
Copied
FYI: I omitted this for brevity here but realized it is important to include it
var multiplier = 1.1823
var descFraction = 0.207
}
// Functions to define bounding boxes
{
function getBoundingBox(textSize, multiplier) {
return Math.round(textSize * multiplier);
}
function getHeightAboveBaseline(textSize, multiplier, descFraction) {
var boundingBoxHeight = getBoundingBox(textSize, multiplier);
return Math.round(boundingBoxHeight - (boundingBoxHeight * descFraction));
}
function getHeightBelowBaseline(textSize, multiplier, descFraction) {
return Math.round(getBoundingBox(textSize, multiplier) * descFraction);
}
function getSpecTextHeight(textSize) {
return Math.round(textSize * 0.5);
}
}
Copy link to clipboard
Copied
Maybe a dumb question, but do you make sure that the active layer is indeed a text layer?
Could it also be that more than just the a textItem has to be specified in order to get access to the useAutoLeading property?
Copy link to clipboard
Copied
A fair question. With the script I do not verify that it is a text layer. It is something I want to add, but have not tackled it yet. I am sure it is easy to do, but haven't gotten into it.
This is a script that has been working for weeks but for some reason just this one file is having this issue.
If I assign Auto Leading to the layer using {lay.textItem.useAutoLeading = true} the script works. However, I don't want to assign Auto Leading by default to a text layer with multiple lines of text.
There is probably a way to test for a custom Leading value and to not assign Auto Leading to the layer if it already has a custom value, but have no idea how to do that.
Copy link to clipboard
Copied
It is possible that useAutoLeading has to be defined before it can be used. You might try to test whether it exists:
if (lay.textItem.useAutoLeading == undefined) {alert("I don't know how to use useAutoLeading") }
you may also test for the string "undefined" and/or for null.
Or, you may also get all valid properties of your layer when you run the script from ExtendScript Editor, and have linked it to Photoshop, by using this code:
for (var i in lay.textItem) {$.writeln(i + " " + lay.textItem) }
This will write all properties of your textItem and their values to the Console.
Note: I have not tested the code, and it may therefore be buggy…
Hope this can help.
Copy link to clipboard
Copied
Thanks, Max.
Any reference to useAutoLeading for this text layer is giving the same 8500 error.
The code you provided to show the layer properties isn't working for me.
Using:
var doc = activeDocument
var lay = doc.activeLayer
for (var i in lay.textItem) {$.writeln(i + " " + lay.textItem) }
gives me this error:
using CC2015
Copy link to clipboard
Copied
This essentially means that the active layer does not have a textItem…
I will have a closer look at the code later today.
Copy link to clipboard
Copied
timmysmuckers wrote:
A fair question. With the script I do not verify that it is a text layer.
then do it
if (layer.kind = LayerKind.TEXT)
Copy link to clipboard
Copied
Thanks, JJMack​.
I added it along with an alert that a text layer needs to be selected to run the script.