Copy link to clipboard
Copied
I have a script that loops through items on the page, then depending if they are a textFrame or a pathItem calls a seperate function. All seems to work well whilst I'm in the ExtendScript Toolkit environment, but when I put the script into illustrator and run it directly I get the following error. Can anyone tell me what I'm doing wrong
It seems to be refering to the code 'if (item instanceof TextFrame) {'
for (var i = doc.pageItems.length -1; i > -1; i--) {
var item=doc.pageItems;
if (item instanceof TextFrame) {
changeText(item)
}
if (item instanceof PathItem) {
changePath(item)
}
}
function changeText(item) {
item.textRange.characterAttributes.figureStyle=FigureStyleType.TABULAR
item.textRange.characterAttributes.fillColor=spotBlack;
item.textRange.characterAttributes.ligature.true
}
function changePath(item) {
if (Math.round(item.strokeColor.cyan) == crowbarBlack.cyan &&
Math.round(item.strokeColor.magenta)== crowbarBlack.magenta &&
Math.round(item.strokeColor.yellow) == crowbarBlack.yellow &&
Math.round(item.strokeColor.black) == crowbarBlack.black) {
item.strokeColor=spotBlack;
}
if (Math.round(item.fillColor.cyan) == crowbarBlack.cyan &&
Math.round(item.fillColor.magenta)== crowbarBlack.magenta &&
Math.round(item.fillColor.yellow) == crowbarBlack.yellow &&
Math.round(item.fillColor.black) == crowbarBlack.black) {
item.fillColor=spotBlack;
}
}
Copy link to clipboard
Copied
Weird, I was gonna post all kinds of answers, but then I examined it a little more closely and I'm not sure why this would be. TextFrame is something that should be part of Illustrator anyhow.
I guess try to alert this on a selection which is just a text frame?
alert(app.activeDocument.selection[0].constructor);
See what it says in ESTK and in AI
Copy link to clipboard
Copied
Hi Bob Haslett
you better use:
if (item.typename == "TextFrame") {
instead of:
if (item instanceof TextFrame) {
Have fun
Copy link to clipboard
Copied
but pixxxel schubserā , but why?? I want to know the mystery.
Copy link to clipboard
Copied
Because of ā¦
⦠it works.
Copy link to clipboard
Copied
I still want to know his constructor name!
But more often than not, I do settle for "it works". Exhibit 1: all of my code ever.
Copy link to clipboard
Copied
Silly-V wrote:
but pixxxel schubser , but why?? I want to know the mystery.
interestingly, it seems objects don't get initialized until they're actually used!!
the error is TextFrame undefined?? weird right? TextFrame is a core object, it should be defined!!
try this snippet on a new document with a pathItem only, don't add any textFrames...the code will give you the undefined error we're dealing with, ok maybe because there aren't any text frames
var idoc = app.activeDocument;
var sel = idoc.selection[0];
if (sel instanceof TextFrame) { // this line will raise TextFrame is undefined error
alert('yes text frame');
}
else {
alert ('no no text frame');
}
add a text frame manually, but don't select it, select the pathItem previously added...we still have the same error, as shown in the original question, merely having text frames in the document is not helping.
next, get a handle of the text frame, keep it deselected, only the pathItem should be selected.
var idoc = app.activeDocument;
var sel = idoc.selection[0];
var tframe = idoc.textFrames[0]; // initialize a TextFrame object
if (sel instanceof TextFrame) { // no error, yay!!
alert('yes text frame');
}
else {
alert ('no no text frame');
}
that did it, getting a handle of a textFrame initializes the object and gets rid of the error.
now for completeness, if there aren't any text frames, we'll get another error, so we can add a text frame before testing instanceof, then remove it after we're done
var idoc = app.activeDocument;
var sel = idoc.selection[0];
var tframe = idoc.textFrames.add(); // initialize TextFrame object to avoid "instanceof" error
if (sel instanceof TextFrame) {
alert('yes text frame');
}
else {
alert ('no no text frame');
}
tframe.remove();
cool finding, it could be useful, but we really should use "typename" as suggested by pixxxel.
Copy link to clipboard
Copied
Sorry for my lack of participation, I work in the UK news industry and its been rather busy. I'll pitch in later
Copy link to clipboard
Copied
why are you so busy? what's happening in the UK?
Copy link to clipboard
Copied
Hi @CarlosCanto, this is me, from 8 years in your future! I found this thread (due to this) and wanted to say: great piece of detective work! Thanks for working it out! - Mark
Find more inspiration, events, and resources on the new Adobe Community
Explore Now