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

check variables contents

Participant ,
Mar 08, 2018 Mar 08, 2018

Hey All,

Since there is not allot of information on variable content out there, how can you check if the variable has any content? Do you have to first have it linked to textObject or can it be checked via if or try?  This does the job but duplicates the variable when not found.  Easier solution??

function relinkvariable(){

    var idoc = app.activeDocument; 

    var mainLayer = idoc.layers;

    var textLayer= mainLayer["text layer"];

    var ftext = textLayer.textFrames["Front"];

   

    if (ftext.name = "Front") {

         ftext.selected = true;

    }

    var sel = idoc.selection;

    for (i=0; i<sel.length; i++) { 

          var itext = sel;

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

              var frontText = activeDocument.variables.getByName('variable1');

              itext.contentVariable = frontText;

          } 

    }

};

        try  {

        relinkvariable();

        }

        catch (e) {

        }

    }

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

correct answers 1 Correct answer

Community Expert , Apr 06, 2018 Apr 06, 2018

to start, you don't need to select a text frame.

hope this snippet helps you. It simply tells you if a variable is bound to a text frame or not

var idoc = app.activeDocument;

var vars = idoc.variables;

for (j=0; j<vars.length; j++) {

    if (vars.kind == VariableKind.TEXTUAL) { // only deal with text variables

        try {

            alert(vars.name + ' : ' + vars.pageItems[0].contents);

        }

        catch (e) {

            alert(vars.name + ': is not linked to a text frame');

          

           

...
Translate
Adobe
Valorous Hero ,
Mar 08, 2018 Mar 08, 2018

Yea, you gotta have a real text box and read its 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
Participant ,
Mar 08, 2018 Mar 08, 2018

Thanks V. maybe I asked this question wrong. How do you check to see if the variable is bound to an object? Or more specifically a textFrame?

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 ,
Mar 09, 2018 Mar 09, 2018

Oh, the Variable object has a .pageItems property, try that!

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
Participant ,
Mar 14, 2018 Mar 14, 2018

So I made some progress on this.  I can't get it to pass if not linked though.  If linked it will alert LINKED but won't pass to else if not linked.  Any input would be helpful.

    var idoc = app.activeDocument; 

    var linkedVariable = idoc.variables.getByName('subText').pageItems;

    var layers = idoc.layers;

    var textLayer = layers["Text Layer"];

    var subText = textLayer.textFrames["Text Frame"];

    for (var i = linkedVariable.length-1 ; i >= 0; i--) {

           

            if (linkedVariable.name = "Text Frame") {

            alert ("Variable Linked") ;

            }

            else {

            subText.selected = true ;

            alert ("Selected");

            }

        }

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
Participant ,
Apr 06, 2018 Apr 06, 2018

   Update on trueVariables.  This works but am having an issue. When the variable is found to have no contents, the script will add and link an extra variable of the same name?  Any ideas?

function trueVariable() {

                    var idoc = app.activeDocument; 

                    var mainLayer = idoc.layers;

                    var orderVar = idoc.variables.getByName('textVariable1');

                    var mainText= mainLayer["Text Layer"];

                   

                    var ftext = mainText.textFrames["Text Sub Layer"];

                   

                    if (ftext.name = "Text Sub Layer") {

                    ftext.selected = true;

                    }

                    var sel = idoc.selection;

                   

                    try {

                        for (i=0; i<sel.length; i++) { 

                          var itext = sel

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

                              itext.contentVariable = orderVar;

                              } 

                            }

                        }

                    catch (e) {

                        }

        };

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 ,
Apr 06, 2018 Apr 06, 2018

to start, you don't need to select a text frame.

hope this snippet helps you. It simply tells you if a variable is bound to a text frame or not

var idoc = app.activeDocument;

var vars = idoc.variables;

for (j=0; j<vars.length; j++) {

    if (vars.kind == VariableKind.TEXTUAL) { // only deal with text variables

        try {

            alert(vars.name + ' : ' + vars.pageItems[0].contents);

        }

        catch (e) {

            alert(vars.name + ': is not linked to a text frame');

          

            // if you're trying to relink variables to named text frames it would be great if you name your variable and text

            // objects with the same name. This way we could do something like this

            //idoc.textFrames[vars.name].contentVariable = vars;

        }

    }

}

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
Participant ,
Apr 10, 2018 Apr 10, 2018
LATEST

Carlos. Your great! Thanks for the help. With a little work I was able to check the specific variable see if it had been bound. As well I am working on a loop though all variables to make sure they are correctly linked. Thanks A bunch.

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