Skip to main content
Known Participant
October 12, 2022
Answered

Do a comparison on string variable

  • October 12, 2022
  • 2 replies
  • 455 views

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]
This topic has been closed for replies.
Correct answer jduncan

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.

2 replies

pixxxelschubser
Community Expert
Community Expert
October 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");

 

 

jduncan
Community Expert
Community Expert
October 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

 

Known Participant
October 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.

 

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
October 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.