I like to import only some variables from an old FM template file to a new one.
The SimpleImport function is not accurate as all variables of the source old FM template file will be imported.
With this function, no filters may be set to define which variables must be imported or not.
I am able to get TextRange string of a source variable but do not find any solution to copy it as TextRange value of the same variable (var.VarFmt.Name) in the new FM template file.
Is it possible to copy an applicable variable definition between two files with FM scripting?
What is(are) the function(s) to use?
It's probably more simple than you might have guessed. Here is a function that transfers the properties of a single variable format to another document. Hope this helps.
function transferVarDef(fromDoc, toDoc, varName)
//Get the source variable format, abort if it does not exist.
var varFmt1 = fromDoc.GetNamedVarFmt(varName);
alert("Variable format \"" + varName + "\" does not exist in " +
"the file " + fromDoc.Name);
//Get the corresponding format in the destination doc, creating it if it
//does not exist.
var varFmt2 = toDoc.GetNamedVarFmt(varName);
varFmt2 = toDoc.NewNamedObject(Constants.FO_VarFmt, varName);
//Set the destination format with the properties of the source format.
I will try your solution and let you know if it works for both User and System variables.
Klaus makes a good point about character formats. If a variable definition references a character format that does not exist in the target document, obviously that format would not be applied. It would be some work to add that check, then transfer the desired formats along with the variable definition. Likely, though, your templates are the same and this should not be an issue. If it is, you can transfer a character format by a very similar process.
But ChrFmts are not the end. What about e.g. colors nested to the ChrFmts.?
Do they exist?
If they exist: is their definition is identical or do they differ?
So I hope, your templates are the same........
If a character format doesn't exist in the target document, you won't actually "lose" it when you run Russ's code; it will still appear in the variable definition and you will see the reference to it when you insert the variable. Once you import Character formats from your template, the variables will be "fixed".
Are you just concerned with User Variables or System Variables or both?
But you have to take care about charfmts that might be found in some variables.
I have tested your solution that works fine for User and System variables.
Those variable definitions have no CharFmt nor color differences between old and new FM template files.
I will take other suggestions about these parameters if such differences will be included in new versions of FM templates.