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

Peter Kahrel
Community Expert
Community Expert
February 7, 2008
Most of the time I use Teus de Jong's object browser (http://www.teusdejong.nl/), which is a good way to look things up. You can set it to display things in VB style. If I can't find things in a browser, I search a list of all properties and methods, here: http://www.kahrel.plus.com/indesignscripts.html (look for Script dictionary). This is a dumb list, but it has been useful for me.

InDesign scripting does take some getting used to, but it's worth the effort. Like you, I used to do WordPerfect scripts, and that's a different universe altogether.

Peter
February 7, 2008
Well, this has all been VERY helpful, at least I'm somewhat on my way.

The Data Browser (Windows / VB 6) has a loooong list of Classes. By selecting TextVariable in the Classes list I get the Members in the right side, one of which is VariableOptions. At the bottom of the Object Browser there is a description with a list of preferences, one of which is CustomTextVariablePreference. CustomTextVariablePreference is listed in the Classes list, members of which are "Contents" and "Parent." Not sue I would have gotten to the line of code that actually works from all of this (IDoc.TextVariables("Author").VariableOptions.Contents = "Oy-vey!") but maybe It'll just take some getting used to.

Peter, Mind if I ask in what exactally you're looking all this up?

Thanks again everyone.

Ken
Jongware
Community Expert
Community Expert
February 7, 2008
It seems to defy logic.

<pre>
TextVariable.variableOptions
|
(TextVariable.variableType)
|
+-PageNumberVariablePreference
+-ChapterNumberVariablePreference
+-DateVariablePreference
+-FileNameVariablePreference
+-MatchCharacterStylePreference
+-MatchParagraphStylePreference
+-CustomTextVariablePreference -> contents
</pre>

I had to scan the entire contents of the Object Help before finding these in the "Preferences Suite".

A likely reason is that each of these 'preferences' (a confusing choice of name) have wildly different properties, so they had to be captured in a single object (the variableOptions). For example, the DateVariablePreference has no contents but a format instead.

Perhaps Ole should finish his object tree idea.
Peter Kahrel
Community Expert
Community Expert
February 7, 2008
It isn't particularly clear indeed. VariableOptions has any of these properties (as an object)

PageNumberVariablePreference
ChapterNumberVariablePreference
DateVariablePreference
FileNameVariablePreference
MatchCharacterStylePreference
MatchParagraphStylePreference
CustomTextVariablePreference

As Ken wanted to set a text variable, I then looked under customTextVariablePreference, where I found contents.

Actually, it so happened that I figured this out last week for a script, and it took me a good deal longer than the above description may have suggested!

Peter
Kasyan Servetsky
Legend
February 7, 2008
Aha! Although it hasn't been documented,it can be seen in Data Browser.
Kasyan Servetsky
Legend
February 7, 2008
Hi,

I checked OMV, SciptDebugger's dictionary and VB6's object browser but didn't find "contents" property of "variableOptions" in any of them. Peter, how did you find it?

Kasyan
February 7, 2008
Peter, I found the VariableOptions in the object browser but, again, didn't see a reference to .Contents.

Can I get personal and ask how you knew about the .Contents?

Ken
Peter Kahrel
Community Expert
Community Expert
February 7, 2008
>Where does one get such info?

It should be in the guides, and I guess you should be able to find it in the object browser. InDesign's object model is indeed complex. To find which methods and properties exist, you have to go through the lists (guides, obj viewer) and see what's there. It's like reading a dictionary.

Peter
February 7, 2008
He is indeed! Thanks Peter!! I've been writing code for 25 or so years, every from WordPerfect 5.0 (DOS) macros to WordBasic to CorelScript and gobs of VB and I've never struggled as much to find commands as I have with this InDesign stuff.

I found the VariableOptions and tried .Text, .Value everything I could think of but found no reference to .Contents.

I have the CS3 scripting guide and tutorial PDFs but neither of those even mentions the TextVariable. Where does one get such info?!?!?

Again, thanks a bunch,
Ken
Peter Kahrel
Community Expert
Community Expert
February 7, 2008
You set the contents of a variable ucing variableOptions.contents. Example:

app.activeDocument.textVariables.item ('Author').variableOptions.contents = "Krugh"

For VB you need to change capitalisation here and there.

Peter
December 23, 2011

Peter,

Thank you! Thank you! Thank you! for this code example. I have been looking all over for a working code example to do just this.

I tried your code snippet and it worked for me.

Can you tell me what I am doing wrong in the following thread?

http://forums.adobe.com/thread/940934

Thanks,

Gordon