10 Replies Latest reply on Oct 10, 2009 7:30 AM by jay fresno

    Insert Text Variable

    Thomas B. Nielsen Level 3
      Hello,

      Need to set the content of a textFrame on a master page to "Current Page Number" a slash and then the missing item "Last Page Number" as found under Type -> Text Variables -> Insert Variable -> "Last Page Number";

      How do i add a text variable with scripting?

      ....parentTextFrames[0].contents = "^N/" + ?????;
        • 1. Re: Insert Text Variable
          Thomas B. Nielsen Level 3
          Indesign CS4 and JS
          • 2. Re: Insert Text Variable
            Thomas B. Nielsen Level 3
            Found a solution:

            try {
            myVar = myDocument.textVariables.item("Last Page");
            myVar.name;
            } catch (e) {
            //alert(e);
            myVar = myDocument.textVariables.add({name:'Last Page', variableType:VariableTypes.LAST_PAGE_NUMBER_TYPE});
            myVar.variableOptions.scope = VariableScopes.DOCUMENT_SCOPE;
            }

            myFoundItems[i].parentTextFrames[0].contents = SpecialCharacters.autoPageNumber;
            myFoundItems[i].parentTextFrames[0].contents += "/";
            myFoundItems[i].insertionPoints.lastItem().textVariableInstances.add( {associatedTextVariable : myVar} );

            If anyone can tell me how i can use the application text variable "Last Page Number" without having to make a document variable, please post.
            • 3. Re: Insert Text Variable
              Peter Kahrel Adobe Community Professional & MVP
              Thomas,

              Since the application text variable you're after is called "Last Page Number", all you need to do is this:

              myVar = myDocument.textVariables.item ("Last Page Number");
              ...
              ...
              myFoundItems[i].insertionPoints.lastItem().textVariableInstances.add( {associatedTextVariable : myVar} );

              Peter
              • 4. Re: Insert Text Variable
                Thomas B. Nielsen Level 3
                Well i tried that (and now once more) but it gives me a Invalid parameter in the line:

                myFoundItems[i].insertionPoints.lastItem().textVariableInstances.add( {associatedTextVariable : myVar} );
                • 5. Re: Insert Text Variable
                  Peter Kahrel Adobe Community Professional & MVP
                  Strange -- it should work. Does your document have a variable called "Last Page Number"? (You call it an 'application variable', but these don't exist in that sense. The variables you see in the "Text Variables" dialog are examples created there by Adobe. I deleted them because I have no use for all those variables in every new document that I create. Which is also the reason why I can't really check your problem.)

                  What exactly does the error message say: invalid parameter or invalid value? If you specify a text variable that doesn't exist, you'd expect "invalid value", not "invalid parameter".

                  Peter
                  • 6. Re: Insert Text Variable
                    Thomas B. Nielsen Level 3
                    Hello Peter,

                    First of all thank you for helping :o)

                    > Strange -- it should work. Does your document have a variable called "Last Page Number"? (You call it an application variable', but these don't exist in that sense. The variables you see in the "Text Variables" dialog are examples created there by Adobe. I deleted them because I have no use for all those variables in every new document that I create. Which is also the reason why I can't really check your problem.)

                    If I understand you correctly, a variable that i can place in a text, needs to be visible under text variables -> define.
                    So first i define a variable, then i make an instance of it at the location needed right?

                    I thought that Last Page Number was a variable that was always present and therefore did not need to be added before using it.
                    Are these "demo" variables from Adobe included in the document in any way, or why have you deleted them?

                    (Btw my try statement above is inverted, I do it the other way around now)

                    Once again, thank you for your time, and scripts at
                    http://www.kahrel.plus.com/indesignscripts.html
                    • 7. Re: Insert Text Variable
                      Peter Kahrel Adobe Community Professional & MVP
                      > a variable that i can place in a text, needs to be visible under text variables -> define. So first i define a variable, then i make an instance of it at the location needed right?

                      That's right.

                      >I thought that Last Page Number was a variable that was always present and therefore did not need to be added before using it.

                      If you delete it without any documents open, you'll never see it in documents you create afterwards.

                      >Are these "demo" variables from Adobe included in the document in any way

                      These demo variables are included in all new documents you create. Text variables behave just like styles in this respect. Styles created in a document are available only in that document. Styles created with no open documents are available in all documents created later.

                      > why have you deleted them?

                      Because I have no use for them, and I like to strip everything unnecessary from documents.

                      Peter
                      • 8. Re: Insert Text Variable
                        jay fresno Level 1

                        Peter,

                         

                        Thank you so much for the Insert Variable script, and also the runscript.jsx. Both of these scripts would be extremely useful to me if I can get them to work correctly.

                         

                        When using Insert Variable, I get a JavaScript error message, which may be due to user error or it could be because I'm using InDesign version CS4. (I have not deleted any of the default text variables.)

                         

                        It would be helpful if you would post the full text of the script. That way I can compare it to what I have -- maybe there is a typo.

                         

                        Also, when trying to use the runscript.jsx script. It runscript dialog opens, but does not display recently run scripts. When I type the name of one of the scripts into the Edit field, and click OK, I get a message:

                         

                        Error: [name of script] doesn't exist.

                         

                        I've tried typing the name of various script and always get the same message.

                         

                        Do you have any suggestions?

                        • 9. Re: Insert Text Variable
                          Jeremy bowmangraphics-DQuh1B Level 3

                          By coincidence, I just happen to be working on a script that inserts a text variable. The following also creates a text frame and a paragraph style, but you can see the relevant bit in the middle:

                           

                          var myDocument = app.documents.item(0);
                          var myPage = app.activeWindow.activePage;
                          myDocument.viewPreferences.horizontalMeasurementUnits =
                          MeasurementUnits.points;
                          myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                          var myTextFrame = myPage.textFrames.add({geometricBounds:[100, 100, 200, 400]});
                          myTextFrame.insertionPoints.item(-1).contents = "Today's date:\r";

                           

                          myVariable = myDocument.textVariables.item("Modification Date");
                          myTextFrame.insertionPoints.item(-1).textVariableInstances.add({associatedTextVariable:myV ariable});

                           

                          var myParagraphStyle = myDocument.paragraphStyles.item("Date");
                          try {var myName = myParagraphStyle.name;}
                          catch (myError){myParagraphStyle =
                          myDocument.paragraphStyles.add({name:"Date"});}

                          myParagraphStyle.appliedFont = "Arial";

                          myParagraphStyle.pointSize = 24;
                          myTextFrame.paragraphs.item(0).applyParagraphStyle(myParagraphStyle, true);
                          myTextFrame.paragraphs.item(1).applyParagraphStyle(myParagraphStyle, true);

                           

                           

                          You can be sure that the above is more clunky and verbose -- and less elegant -- than anything Peter writes, but it does work for me in CS3 and CS4.

                           

                          EDIT: 'associatedTextVariable' gets split by the word wrap of the forum software -- just make sure you remove any added breaks.

                          • 10. Re: Insert Text Variable
                            jay fresno Level 1

                            Jeremy,

                             

                            Thank you for sharing your script. I'm so pleased -- the portion that inserts a text variable is great; plus, the whole script in itself is very useful as a time stamp.

                             

                            The only issue now is that runscript is not working in my case, but I think that Peter will reply when he gets a chance.