Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Do a comparison on string variable

Explorer ,
Oct 12, 2022 Oct 12, 2022

I am trying to read a variable and then set a font of another item accordingly. I can set the font okay, but don't understand how to get the variable value for compariston. In this case I want the text value of sfont which is "arial".

 

Script

    sgreg = app.activeDocument.variables.getByName('sfont');
    $.writeln(sgreg);
Extendscript debug console
    [Variable sfont]
TOPICS
Scripting
353
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

correct answers 1 Correct answer

Community Expert , Oct 12, 2022 Oct 12, 2022

Yes, it's because your variable isn't actually bound to an object, it is just an empty variable. If you compare your variables palette to mine attached, you can see that you have nothing listed for "Object". That "object" is the page item the variable is linked to. So when you try to access 'sgreg.pageItems[0]' you are getting an error because there are no page items actually bound to that variable.

Screen Shot 2022-10-12 at 17.31.16.pngexpand image

Translate
Adobe
Community Expert ,
Oct 12, 2022 Oct 12, 2022

The below works for me. I created a single text variable with the text "arial" and this returns exactly that. The first item in the 'sgreg.pageItems' should be a TextFrame so you could dig deeper for more info if you needed to.

sgreg.pageItems[0].contents

 

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 ,
Oct 12, 2022 Oct 12, 2022

Makes sense, but I'm getting this error.  Trying a few things to get string value. toString, and some others. I'm on windows. CC illustrator.

 

sc1.pngexpand imagesc2.pngexpand image

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 12, 2022 Oct 12, 2022

Yes, it's because your variable isn't actually bound to an object, it is just an empty variable. If you compare your variables palette to mine attached, you can see that you have nothing listed for "Object". That "object" is the page item the variable is linked to. So when you try to access 'sgreg.pageItems[0]' you are getting an error because there are no page items actually bound to that variable.

Screen Shot 2022-10-12 at 17.31.16.pngexpand image

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 ,
Oct 12, 2022 Oct 12, 2022
LATEST

That's it! Thanks. Now will try with the datasets. 

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 12, 2022 Oct 12, 2022

Hi @gstewartjr 

Does the following helps a little?

alert (app.activeDocument.textFrames[0].textRange.characterAttributes.textFont.name);

var aTF = app.activeDocument.textFrames.add();
aTF.contents = "test";
aTF.textRange.characterAttributes.textFont = app.textFonts.getByName("MyriadPro-Regular");

 

 

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