Skip to main content
Known Participant
September 22, 2022
Answered

Custom Text Variable

  • September 22, 2022
  • 1 reply
  • 1324 views

I want to create a text variable using User Name.  I am not verry good at scripting, so, hopefully someone can help me.

 

Thanks

This topic has been closed for replies.
Correct answer Mike Bro

Nothing happens when I createa text box and run the script



@GMK2624299024hl,

 

The script I posted just creates the user name text variable. Try the script below to add the text variable to the selected text box you created.

function isMacOS() {
    return ($.os.toLowerCase().indexOf('mac') >= 0);
  }
  
  function getUserName() {
    return (isMacOS()) ? $.getenv("USER") : $.getenv("USERNAME");
  }

try{app.activeDocument.textVariables.add({name: "User Name", variableType:VariableTypes.CUSTOM_TEXT_TYPE,});
  app.activeDocument.textVariables.item("User Name").variableOptions.contents = getUserName();
} catch (e) {
}

var mySelection = app.selection[0];
var myUserNametextVariable = app.activeDocument.textVariables.item("User Name"); 
mySelection.texts[0].textVariableInstances.add({associatedTextVariable:myUserNametextVariable});

 

Regards,

Mike

1 reply

Legend
September 22, 2022

Hello @GMK2624299024hl,

 

Give this a try...

function isMacOS() {
    return ($.os.toLowerCase().indexOf('mac') >= 0);
  }
  
  function getUserName() {
    return (isMacOS()) ? $.getenv("USER") : $.getenv("USERNAME");
  }

try{app.activeDocument.textVariables.add({name: "User Name", variableType:VariableTypes.CUSTOM_TEXT_TYPE,});
  app.activeDocument.textVariables.item("User Name").variableOptions.contents = getUserName();
} catch (e) {
}

Regards,

Mike

Known Participant
September 23, 2022

Thanks.  Do I include this in Startup Scripts?

Legend
September 23, 2022

Hello @GMK2624299024hl,

No, the code posted is not a startup script, you would run it manually from the scripts panel.

If you wanted to be a startup script you would want to add a EventListener to trigger the script.

 

Regards,

Mike