Copy link to clipboard
Copied
I want to create a text variable using User Name. I am not verry good at scripting, so, hopefully someone can help me.
Thanks
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 Nam
...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks. Do I include this in Startup Scripts?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
That works! Thank You
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You can edit the Text Variables under "Define" but you will not be able to amend the File Name to include the User Name.
If you want File Name_User Name that could be done with modifications to the script
Regards,
Mike
Copy link to clipboard
Copied
As I have no idea how to code that, is that something you can do?
Thanks.
Steve
Copy link to clipboard
Copied
Give this script a try...
Instead of creating the custom text variable "User Name" we can add "Text After" to the File Name text variable.
function isMacOS() {
return ($.os.toLowerCase().indexOf('mac') >= 0);
}
function getUserName() {
return (isMacOS()) ? $.getenv("USER") : $.getenv("USERNAME");
}
var mySelection = app.selection[0];
var myFileNametextVariable = app.activeDocument.textVariables.item("File Name");
myFileNametextVariable.variableOptions.textAfter = "_" + getUserName();
mySelection.texts[0].textVariableInstances.add({associatedTextVariable:myFileNametextVariable});
Regards,
Mike
Copy link to clipboard
Copied
Giving it a try.....
Copy link to clipboard
Copied
Works great! Thank you
Copy link to clipboard
Copied