Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I create a user variable in Extendscript.

Engaged ,
Apr 02, 2025 Apr 02, 2025

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!

TOPICS
Scripting
51
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 02, 2025 Apr 02, 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.

Translate
Community Expert ,
Apr 02, 2025 Apr 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 02, 2025 Apr 02, 2025
LATEST

Works perfectly (as expected) - Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines