Exception has been thrown by the target of an invocation. Error 1220: Illegal Argument
While running a JavaScript file from C#/COM, I will occasionally receive the error "Exception has been thrown by the target of an invocation. Error 1220: Illegal Argument", the error goes on to show the line # of the offending javascript, but this doesn't happen every time, and usually within a few retries it will pass.
What might cause this? Here is the javascript in question, and usually the error reports that line #8 is the offender "if (frames[x].textRange.characterAttributes.textFont != null) {"
var frames = app.activeDocument.textFrames;
var len = frames.length;
for (var x = 0; x < len; x++) {
if (frames[x] != null) {
if (frames[x].textRange != null) {
if (frames[x].textRange.characterAttributes != null) {
if (frames[x].textRange.characterAttributes.textFont != null) {
if (frames[x].textRange.characterAttributes.textFont.name != null) {
if (frames[x].textRange.characterAttributes.textFont.name.toLowerCase().indexOf("avenir") >= 0) {
if (frames[x].textRange.characterAttributes.textFont.name == "Avenir-BlackOblique") {
frames[x].textRange.characterAttributes.textFont = textFonts.getByName("ProximaNova-Bold");
} else if (frames[x].textRange.characterAttributes.textFont.name == "Avenir-Medium") {
frames[x].textRange.characterAttributes.textFont = textFonts.getByName("ProximaNova-Medium");
}
}
}
}
}
}
}
}
var avenirFonts = "";
for (var x = 0; x < len; x++) {
if (frames[x] != null) {
if (frames[x].textRange != null) {
if (frames[x].textRange.characterAttributes != null) {
if (frames[x].textRange.characterAttributes.textFont != null) {
if (frames[x].textRange.characterAttributes.textFont.name != null) {
if (frames[x].textRange.characterAttributes.textFont.name.toLowerCase().indexOf("avenir") >= 0 && avenirFonts.indexOf(frames[x].textRange.characterAttributes.textFont.name) < 0) {
avenirFonts = avenirFonts + frames[x].textRange.characterAttributes.textFont.name + "\n";
}
}
}
}
}
}
}
if (avenirFonts.length > 0) {
throw "Found Avenir Fonts: " + avenirFonts;
}The purpose of this script is to replace the Apple Avenir fonts to a windows available font, and it will work even if I receive an error after a few retries. We have automation built out to create AI templates from source files ever 5 minutes to assist our design team.