Skip to main content
Participating Frequently
October 16, 2018
Answered

Import some variables

  • October 16, 2018
  • 4 replies
  • 1027 views

Hi All,

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?

JM

This topic has been closed for replies.
Correct answer Russ Ward

Hi JM,

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.

Russ

function transferVarDef(fromDoc, toDoc, varName)

{

    //Get the source variable format, abort if it does not exist.

    var varFmt1 = fromDoc.GetNamedVarFmt(varName);

    if(!varFmt1.ObjectValid())

    {

        alert("Variable format \"" + varName + "\" does not exist in " +

            "the file " + fromDoc.Name);

        return;

    }

   

    //Get the corresponding format in the destination doc, creating it if it

    //does not exist.

    var varFmt2 = toDoc.GetNamedVarFmt(varName);

    if(!varFmt2.ObjectValid())

    {

        varFmt2 = toDoc.NewNamedObject(Constants.FO_VarFmt, varName);

    }

    //Set the destination format with the properties of the source format.

    varFmt2.SetProps(varFmt1.GetProps());

}

4 replies

Participating Frequently
October 22, 2018

Thanks Russ,

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.

JM

Klaus Göbel
Legend
October 16, 2018

But you have to take care about charfmts that might be found in some variables.

frameexpert
Community Expert
Community Expert
October 16, 2018

Are you just concerned with User Variables or System Variables or both?

www.frameexpert.com
Russ WardCorrect answer
Legend
October 16, 2018

Hi JM,

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.

Russ

function transferVarDef(fromDoc, toDoc, varName)

{

    //Get the source variable format, abort if it does not exist.

    var varFmt1 = fromDoc.GetNamedVarFmt(varName);

    if(!varFmt1.ObjectValid())

    {

        alert("Variable format \"" + varName + "\" does not exist in " +

            "the file " + fromDoc.Name);

        return;

    }

   

    //Get the corresponding format in the destination doc, creating it if it

    //does not exist.

    var varFmt2 = toDoc.GetNamedVarFmt(varName);

    if(!varFmt2.ObjectValid())

    {

        varFmt2 = toDoc.NewNamedObject(Constants.FO_VarFmt, varName);

    }

    //Set the destination format with the properties of the source format.

    varFmt2.SetProps(varFmt1.GetProps());

}

Participating Frequently
October 16, 2018

Thanks Russ,

I will try your solution and let you know if it works for both User and System variables.

JM

Legend
October 16, 2018

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.

Russ