Skip to main content
interesting_Flower157F
Inspiring
January 14, 2009
Question

Insert Text Variable

  • January 14, 2009
  • 7 replies
  • 4996 views
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/" + ?????;
This topic has been closed for replies.

7 replies

Peter Kahrel
Community Expert
Community Expert
January 15, 2009
> 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
Inspiring
October 9, 2009

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?

Jeremy_bowmangraphics-DQuh1B
Inspiring
October 10, 2009

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:myVariable});

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.

interesting_Flower157F
Inspiring
January 15, 2009
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
Peter Kahrel
Community Expert
Community Expert
January 14, 2009
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
interesting_Flower157F
Inspiring
January 14, 2009
Well i tried that (and now once more) but it gives me a Invalid parameter in the line:

myFoundItems.insertionPoints.lastItem().textVariableInstances.add( {associatedTextVariable : myVar} );
Peter Kahrel
Community Expert
Community Expert
January 14, 2009
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.insertionPoints.lastItem().textVariableInstances.add( {associatedTextVariable : myVar} );

Peter
Jeremy bowmangraphics
Inspiring
March 15, 2024

You wouldn't believe the amount of time I've spent looking for the syntax of simply inserting a text variable instance! Yet again, thank you so much!

interesting_Flower157F
Inspiring
January 14, 2009
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.parentTextFrames[0].contents = SpecialCharacters.autoPageNumber;
myFoundItems.parentTextFrames[0].contents += "/";
myFoundItems.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.
interesting_Flower157F
Inspiring
January 14, 2009
Indesign CS4 and JS