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

Writing an InDesign script to identify users

Community Beginner ,
Mar 26, 2020 Mar 26, 2020

Our office has multiple users who often work on the same InDesign files at various times.  We are trying to figure out a way to create a footer, variable text box, or something that we can place in our InDesign documents, that will identify the Mac user/ InDesign username of the person who last saved the file.  I've played around with various scripts and finally found one that will at least add a variable text option (which we then added to a text box) for the InDesign username.... however, the username did not automatically update when the file was opened by another user, even when he had the same script installed on his computer.

 

Please note:  I am very new to scripting, and have only attempted to learn it to resolve this specific issue.  (I have, however, spent the last few days researching this and trying to find a solution.)

TOPICS
How to , Scripting
2.9K
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 2 Correct answers

Community Expert , Mar 27, 2020 Mar 27, 2020

Hi Kellewisphoto,

you can get the name of the logged-in user with:

 

// On Windows:
$.getenv("USERNAME")

 

//On Mac OS X:
$.getenv("USER")

 

and then, if you like, set InDesign's app.userName value to its result.

Note: If you trash InDesign's preferences or cache ( I did not try this ) the value of userName is set back to its default string "$ID/Unknown User Name".

 

Regards,
Uwe Laubender

( ACP )

Translate
Valorous Hero , Mar 27, 2020 Mar 27, 2020

You can do this using this startup script:

#targetengine "Kasyan"

var eventListener = app.addEventListener("beforeSave", rememberLastUser, false);     

function rememberLastUser(event) {
	try {
		var doc = event.parent;
		doc.metadataPreferences.author = app.userName;
	}
	catch(err) {}
}

It inserts the user's name into the author field before the document is saved.

User name

s.png

File info dialog box
2020-03-27_19-19-16.png

Also, the name goes to an exported PDF file

2020-03-27_19-21-47.png

Of course, you can use another field in metadata.

Hope it help

...
Translate
Advisor ,
Mar 27, 2020 Mar 27, 2020

Hello Kellewisphoto,

Can you please post the script your testing\modifying, also what OS are all the users on....Mac based?.....PC based?

 

Regards,

Mike

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
Community Beginner ,
Mar 27, 2020 Mar 27, 2020

Mike,

I haven't even found a script that I'm able to modify yet.  (Very much a beginner in scripting.) I'm tinkering with Applescript and all of the ones I've downloaded so far are... well, not Apple.  We're all using Macs here.

This is vastly out of my realm of expertise, but I'm trying to creatively problem-solve, and maybe learn something new while I'm at it.

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
Community Expert ,
Mar 27, 2020 Mar 27, 2020

Mike means the script that you are using now – the one that doesn't work. Maybe we can figure out why it doesn't if you show it.

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
Community Beginner ,
Mar 27, 2020 Mar 27, 2020

Sorry if this is a lot of unnecessary stuff, but below is the first script we had tried (the one that doesn't update when my colleague opens the file from his Mac).  It's also from 2012 so I'm aware that could be part of the problem.

 

//This function adds static text variables in InDesign
//They won't be dynamically updated though.
//That's why we need the updating function below
function addVariables(openEvent){
 
    var doc = openEvent.parent;
    
    while ( doc.constructor.name != "Document" )
    {
   if ( doc.constructor.name == "Application" ){ return; }
  
  doc = doc.parent;
}
    
//Adding User name to text variables
createTextVariable(doc, "User name", (Folder.myDocuments).parent.displayName );
 
//Adding Computer name to text variables
createTextVariable(doc, "HD Name", (Folder.system).parent.displayName );
 
//Adding Computer name to text variables
createTextVariable(doc, "Computer Name", getComputerName() );
 
//Adding a link to my website 😉
createTextVariable(doc, "_About Loic Aigon", "Feel free to visit my website: http://www.loicaigon.com !" );
 
//Adding User name to text variables
createTextVariable(doc, "InDesign User name", app.userName, false );
 
 
//Adding XMP serverURL to text variables
createTextVariable(doc, "Next Month", getNextMonth(), true );
 
//Adding XMP Author to text variables
createTextVariable(doc, "XMP Author", doc.metadataPreferences.author, true );
 
//Adding XMP Description to text variables
createTextVariable(doc, "XMP Description", doc.metadataPreferences.description, true );
 
//Adding XMP copyrightInfoURL to text variables
createTextVariable(doc, "XMP Copyright Info URL", doc.metadataPreferences.copyrightInfoURL, true );
 
//Adding XMP copyrightNotice to text variables
createTextVariable(doc, "XMP Copyright Notice", doc.metadataPreferences.copyrightNotice, true );
 
//Adding XMP copyrightNotice to text variables
createTextVariable(doc, "XMP Document Title", doc.metadataPreferences.documentTitle, true );
 
//Adding XMP creator to text variables
createTextVariable(doc, "XMP Creator", doc.metadataPreferences.creator, true );
 
//Adding XMP keywords to text variables
createTextVariable(doc, "XMP Keywords", doc.metadataPreferences.keywords, true );
 
//Adding XMP serverURL to text variables
createTextVariable(doc, "XMP Server URL", String(doc.metadataPreferences.serverURL), true );
 
 
}
 
function getNextMonth() {
var date = new Date();
var month = date.getMonth();
var Months = ["January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"];
 
return Months[month+1];
}
 
//Generic function to add static custom text variables
function createTextVariable(target, variableName, variableContents, bRewrite){
 
var usernameVariable = target.textVariables.itemByName(variableName);
if(!usernameVariable.isValid){ 
usernameVariable = target.textVariables.add();
usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;
usernameVariable.name = variableName;
usernameVariable.variableOptions.contents = variableContents;
}
bRewrite!==false && usernameVariable.variableOptions.contents = variableContents;
}
 
 
 
//Snippet for grabbing the  deep name of the computer
function getComputerName(){
 
var APLScript = "get computer name of (system info)";
var VBScript = "Dim wshShell\
Set wshShell = CreateObject( \"WScript.Shell\" )\
strComputerName = wshShell.ExpandEnvironmentStrings( \"%COMPUTERNAME%\" )\
app.scriptArgs.SetValue \"computerName\", strComputerName";
 
var scpt = (File.fs=="Windows")? VBscript : APLScript;
var language = (File.fs=="Windows")? ScriptLanguage.visualBasic : ScriptLanguage.APPLESCRIPT_LANGUAGE;
 
var scriptResult = app.doScript(scpt, language);
 
var computerName = (File.fs=="Windows")? app.scriptArgs.getValue("computerName") : scriptResult;
 
return computerName;
}
 
//Add listeners to update file when opened.
app.addEventListener('afterOpen', addVariables);
app.addEventListener('beforeSave', addVariables);
//app.addEventListener('afterSelectionChanged', addVariables);
app.addEventListener('afterNew', addVariables);

 

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
Community Expert ,
Mar 27, 2020 Mar 27, 2020

Hi Kellewisphoto,

 

by default, after installing InDesign, app.userName is a string like that ( from my German InDesign ) :

"Unbekannter Benutzername"

This is equal to: "$ID/Unknown User Name".

 

This user name usually is not equal to the user name a user is logged-in to the computer.

I cannot see that InDesign is storing the computer's user name to a document.

So you cannot trace an InDesign document back to its creator, the logged-in user.

 

Did every of your InDesign users define the InDesign user name?

Menu: File > User…

 

Menu-File-User_app.userName.PNG

 

Regards,
Uwe Laubender

( ACP )

 

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
Community Expert ,
Mar 27, 2020 Mar 27, 2020

Hi Kellewisphoto,

you can get the name of the logged-in user with:

 

// On Windows:
$.getenv("USERNAME")

 

//On Mac OS X:
$.getenv("USER")

 

and then, if you like, set InDesign's app.userName value to its result.

Note: If you trash InDesign's preferences or cache ( I did not try this ) the value of userName is set back to its default string "$ID/Unknown User Name".

 

Regards,
Uwe Laubender

( ACP )

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
Valorous Hero ,
Mar 27, 2020 Mar 27, 2020

You can do this using this startup script:

#targetengine "Kasyan"

var eventListener = app.addEventListener("beforeSave", rememberLastUser, false);     

function rememberLastUser(event) {
	try {
		var doc = event.parent;
		doc.metadataPreferences.author = app.userName;
	}
	catch(err) {}
}

It inserts the user's name into the author field before the document is saved.

User name

s.png

File info dialog box
2020-03-27_19-19-16.png

Also, the name goes to an exported PDF file

2020-03-27_19-21-47.png

Of course, you can use another field in metadata.

Hope it helps.

— Kas

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
Participant ,
Mar 27, 2020 Mar 27, 2020

Hi. I know you've found a solution, but another route would be to have an intermediary check-in/check-out system for users based in, say, a Filemaker or other database. Filemaker is quite scriptable and comes with networking built-in. The meta info could be inserted from Filemaker, and/or Filemaker could also keep the info so that anyone on the network could see who worked on which documents at what time, keep statistics, time worked, etc.

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
Community Beginner ,
Mar 30, 2020 Mar 30, 2020
LATEST

I appreciate this advice.  However, this is a small company and I think they prefer we don't purchase additional software unless it's completely necessary.  I will definitely do some research on Filemaker and keep it in mind for the future, in case our scripting endeavors don't fix the issues.

 

Everyone's help is very much appreciated.

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