Intermittent 'PARM' Error When Accessing Text Font in Illustrator Script
I’m brand new to scripting in Illustrator, I’m using VS Code and things are, sometimes, working . . . sort of.
I’m working from PDFs created from Word and have the script checking the font applied to the text frames, and if italic is in the name apply our italic font, if not apply our regular font. Seemed pretty straight forward.
But I was getting this error: an Illustrator error occurred: 1346458189 ('PARM')
On this line of code: txtFnt = tf.textRange.characterAttributes.textFont;
I began debugging and commenting out various lines. Eventually I had everything uncommented again with no changes to the actual code and it worked exactly as expected! No changes to the code and it stopped working again at the same spot.
I’ve seen a few posts with no resolution referring to this error.
Full script is at the bottom.
Does anyone have any hits as to WTF is happening?
Thank you,
Ken
//#target illustrator
function cleaupDoc() {
if (app.documents.length > 0) {
var doc = app.activeDocument;
var textFrames = doc.textFrames;
var count = 0;
var txtFontReg = app.textFonts.getByName("MyFont-Regular");
var txtFontItal = app.textFonts.getByName("MyFont-Italic");
var txtFnt;
// Loop backwards through text frames to avoid indexing issues after deletion
for (var i = textFrames.length - 1; i >= 0; i--) {
var tf = textFrames[i];
// Check if frame is empty OR contains only whitespace characters
// Replace '^\s*$' with '^$' if you ONLY want to delete truly empty objects
if (tf.contents.match(/^\s*$/)) {
tf.remove();
count++;
} else {
txtFnt = tf.textRange.characterAttributes.textFont;
if(txtFnt.name.indexOf('Italic') > 0) {
tf.textRange.characterAttributes.textFont = txtFontItal;
}else{
tf.textRange.characterAttributes.textFont = txtFontReg;
}
}
}
alert(count + " empty text objects were deleted.");
} else {
alert("No active document found.");
}
}
cleaupDoc();
