Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Script running in toolkit but fails in Illustartor

Explorer ,
Jun 23, 2016 Jun 23, 2016

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

Screen Shot 2016-06-23 at 15.31.21.png

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;

            }

    }

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Valorous Hero ,
Jun 23, 2016 Jun 23, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2016 Jun 23, 2016

Hi Bob Haslett

you better use:

if (item.typename == "TextFrame") {

instead of:

if (item instanceof TextFrame) {

Have fun

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 23, 2016 Jun 23, 2016

but pixxxel schubser​ , but why?? I want to know the mystery.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2016 Jun 23, 2016

Because of …

… it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 23, 2016 Jun 23, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2016 Jun 23, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 24, 2016 Jun 24, 2016

Sorry for my lack of participation, I work in the UK news industry and its been rather busy. I'll pitch in later

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 24, 2016 Jun 24, 2016

why are you so busy? what's happening in the UK?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2024 Oct 05, 2024
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines