Skip to main content
February 7, 2008
Question

Text Variables and Scripts

  • February 7, 2008
  • 21 replies
  • 14324 views
I have a number of standard documents that a setup script uses. In each document there are text variables called Author and Job Number.

I'm working in VB and these commands can display the name and index of the variable but can't seem to find the way to actually change the value of the variable.

Set IDoc = InD.ActiveDocument
MsgBox IDoc.TextVariables("Author").Name
MsgBox IDoc.TextVariables("Author").Index

Any help would be much appreciated.
This topic has been closed for replies.

21 replies

Participating Frequently
June 22, 2008
Thanks Peter,
There's no question that the VariableTypes.MATCH_PARAGRAPH_STYLE_TYPE is much harder to work with but I discovered that it can be used as a conditional. The variable is only active when the layer it resides on is active. So a multi-layered document can conditionally apply the same Running Header variable with different content as long as only one of the layers the variable resides on is active.

This has a lot of potential for complex projects and allows different content to be used to define the contents of the same variable on different layers.

The problem I'm running into is that it only applies to the page it's defined on and all subsequent pages until changed. I need to find a way to take the contents of the Running Header variable on one page and apply it to several text frames that precede the first instance page.

Maybe there's a way to pass the contents of the variable using a frame label ID scheme.

Thanks again for the VariableTypes.CUSTOM_TEXT_TYPE example. It's great and applies variables to all layers from a hidden and or non-printing layer.

Regards,
John
Peter Kahrel
Community Expert
Community Expert
June 22, 2008
John,

The other variable types are a bit harder to handle than text variables (in a script). As text variables are the only ones that are changed regularly, they're the only ones for which I think quick access is sensible to script. Other variable types are set once (in my experience anyway).

Peter
Participating Frequently
June 22, 2008
Sorry I guess I not explained this well.

No, I'm referring to all the other variable types - specifically the Running Header variable that allows for First and Last instance of "Use:".

John
Peter Kahrel
Community Expert
Community Expert
June 22, 2008
Sorry, I still don't get it. Are we talking about the same type of variable?

Peter
Participating Frequently
June 21, 2008
Thanks for Responding Peter,
Example: Recording a macro in a program yields code that mimics activity. The code can then be used in a variety of ways in other scripts and it also simplifies an understanding of program functionality.

Ole pointed out, Extend Scripting needs to reflect the way InDesign works and the Object Model is very complex. An approach that first mimics or reflects the defaults in the program could help to simplify change and could act as a form of documentation.

If we could first build a table of all text variables in the document in a way that defines the variable type and property, then alterations become easier and more robust.

One of the amazing aspects to InDesign is the idea of unlimited text variables that can be passed automatically to populate other text frames in a file or book of documents. However, there are a number of rules related to how the variables behave that become limitations within a book file. For instance, the Running Header variable is the only one that allows 1st or Last instance of "Use:" but logically is applied in a linear fashion - always applies the content of the instance to style instance page and subsequent pages until the value is changed.

Assuming the program allows it, it would be amazing to build on the instance of "Use:" in different ways using the Table or a hidden frame label.

Regards,
John
Peter Kahrel
Community Expert
Community Expert
June 20, 2008
McClure,

>... the opportunity to mimic variable properties in the document as a means of coding the script to include header or footer "Use:" cases for variable definition and adjustment.

>Is there a simple way to call existing variables of all types into a table where the table cells reflect each of the ExtendScript values for ease of use in coding?

I'm afraid I don't understand what you mean. Could you elaborate?

Thanks,

Peter
Participating Frequently
June 20, 2008
Peter,
Many thanks for sharing this code.

Initial observation using InDesign CS3 5.0.2 on OS X 10.5.3:
Most of us use picas and points in InDesign and your code calls
tf.geometricBounds = [20,20,500,500]; which creates a multi-spread text frame. I changed the values to fall within a common 8x10" page.

var tf = doc.pages[0].textFrames.add ({label: 'vars___'});
tf.geometricBounds = [1,1,47,47];
var table = tf.tables.add ({columnCount: 2, bodyRowCount: 1});
table.columns[0].width = "12p";
table.columns[1].width = "34p";

One of the things that immediately occurred to me using this script was the opportunity to mimic variable properties in the document as a means of coding the script to include header or footer "Use:" cases for variable definition and adjustment.

Is there a simple way to call existing variables of all types into a table where the table cells reflect each of the ExtendScript values for ease of use in coding?
Peter Kahrel
Community Expert
Community Expert
February 8, 2008
Coming back to scripted text variables, here's a script that illustrates a couple of things that were raised in this thread. I use it to display and set all text variables in a document. I use it to set variables in documents I use as masters for journals, to set things like volume, issue, year, date, and whatever else.

When first run, the script creates a two-column table on the first page of the document. It places variable names in the first column, their values in the second column. To set different values for any variable, replace the value in the second column with the new value. Then run the script again.

Peter

(PS: jongware's list is a great tool indeed, should have mentioned that)

doc = app.activeDocument;
if ( doc.textFrames.item ('vars___') == null)
show_variables (doc);
else
set_variables (doc);

function show_variables (doc)
{
var v = doc.textVariables;
var table_contents = [];
var n = 0;
for (var i = 0; i < v.length; i++)
{
if (v.variableType == VariableTypes.customTextType)
{
table_contents.push (v.name);
table_contents.push (v.variableOptions.contents);
n++;
}
}
var tf = doc.pages[0].textFrames.add ({label: 'vars___'});
tf.geometricBounds = [20,20,500,500];
var table = tf.insertionPoints[0].tables.add();
table.columnCount = 2;
table.bodyRowCount = n;
table.contents = table_contents;
}

function set_variables (doc)
{
var t = doc.textFrames.item ('vars___').parentStory.tables[0];
var var_names = t.columns[0].contents;
var new_contents = t.columns[1].contents;
var vars = doc.textVariables;
for( var i = 0; i < var_names.length; i++)
vars.item (var_names).variableOptions.contents = new_contents;
// doc.textFrames.item ('vars___').remove()
}
Jongware
Community Expert
Community Expert
February 7, 2008
Adding to Peter's wealth of data: as I found the Help browser infuriatingly slow and cumbersome, I extracted all Help data into a handy set of hyperlinked-to-the-gills html pages. D/L from my site at http://www.jongware.com/binaries/indesigncs3jshelp.zip if you like to see it.

The only thing that I really miss is caused by an artefact of the source XML data: if a reference can return multiple objects, that list of objects is written down in plain ASCII, and I'm unable to harvest links from these. So the references to the different variableTypes showed up as plain text -- if they had been hyperlinks too, I might've spotted'em sooner.

[Edit: Ole, nothing to apologize for! As an old WP macro programmer -- I see there are more than a few here -- and an unwilling VBA user, I was able to create and maintain an entire workflow based on just a few scripts, which amazingly kept on working by and large through 3 major versions of ID.]
Known Participant
February 7, 2008
Fellow Scripters,

I agree that the documentation should cover text variables better--and I created it. There just wasn't time to get them in, and it's high on my list of things to fix for CS4.

Ken, I don't think you need another browser--the VB6 version is at least as good as the JavaScript versions mentioned above. The only trick is to understand that TextVariable.VariableType tells you which "preferences" object to look at for the settings for the variable.

For a simple text (custom) variable, you'd see that the VariableType is idVariableType.idCustomTextType. That tells you that the object you want to look at is the CustomTextVariablePreference object. Looking at that object, you can see that it has only a Content and a Parent property.

This, by the way, is why the type of the TextVariable.VariableOptions property is a Variant--it can be any of the text variable preferences objects. When you think of the question in terms of the user interface, this seems reasonable--the different preferences objects correspond to the menu choices in the Text Variables dialog box. The menu choice you make changes the controls that appear in the dialog box. The controls correspond to the options in the preferences object.

Which brings me to a point: the more you know about the way that InDesign works, the easier it is to understand the object model. That said, InDesign is a big program, with a very large object model!

Sorry I didn't get to text variables in the documentation, and I'll do better next time. Until then, ask questions here! It's an incredible resource.

Thanks,

Ole