Skip to main content
Inspiring
April 2, 2025
Answered

How do I create a user variable in Extendscript.

  • April 2, 2025
  • 1 reply
  • 172 views

I think the below info is correct. Let's say my user variable is named "_DocType". I can read and write and check the existence of the variable like this:

var doc = app.ActiveDoc;
// Check Exists
var varFmt = doc.GetNamedObject (Constants.FO_VarFmt, "_DocType");
if(varFmt.ObjectValid()){
    alert("Variable _DocType exists!")
}

// Write Variable - as "User Guide":
        var varFmt = doc.GetNamedVarFmt ("_DocType");
        varFmt.Fmt = "User Guide";

// Read Variable
           var varFmt = doc.GetNamedObject (Constants.FO_VarFmt, "_DocType");
           alert (varFmt.Fmt);

However, I'm not sure how to create the variable if it doesn't exist yet.

Thanks in advance!

Correct answer frameexpert

I use something like this:

function getVarFmt (name, definition, doc) {
    
    var varFmt;
    
    varFmt = doc.GetNamedVarFmt (name);
    if (varFmt.ObjectValid () === 0) {
        varFmt = doc.NewNamedVarFmt (name);
        varFmt.Fmt = definition;
    }

    return varFmt;
}

Note that if the variable format current exists, then the "definition" parameter isn't used.

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
April 2, 2025

I use something like this:

function getVarFmt (name, definition, doc) {
    
    var varFmt;
    
    varFmt = doc.GetNamedVarFmt (name);
    if (varFmt.ObjectValid () === 0) {
        varFmt = doc.NewNamedVarFmt (name);
        varFmt.Fmt = definition;
    }

    return varFmt;
}

Note that if the variable format current exists, then the "definition" parameter isn't used.

Inspiring
April 2, 2025

Works perfectly (as expected) - Thanks again!