Skip to main content
Inspiring
May 16, 2016
Answered

Placing a USER name in a box?

  • May 16, 2016
  • 2 replies
  • 2394 views

I'm trying to add a user name to a box so whom ever saves the file last it will place there name in the box as you press the save button.  I will post the code i'm thinking of using below.  as well as a pic to show what i'm wanting. 

#targetengine "myUniqueName" 

 

// Add an event listener for the event "beforeSave" that is executing a function 

 

app.addEventListener 

(  

     /* the event you are listening to: | as String */ "beforeSave" ,  

     /* the function that is called when found: */ doTextFrameOnPageOne  

); 

 

function doTextFrameOnPageOne() 

{  /*IS THIS WHERE I SHOULD PLACE THE CODE BELOW?*/

   

    // Here will start your code, that is: 

    // 1. Looking for the user name 

    // 2. Check, if the text frame you want to create is already there 

    // 3. Adds the user name, the current date and some other information 

    // 4. Tells the document where to exactly put the text frame, its dimensions and other properties 

 

    // Test the function: 

    alert("Event \"beforeSave\" just fired."); 

}; 

// SHOULD THIS CODE GO IN THE FUNCTION BRACKETS above?

var myString = 'Hallo InDesign ' + app.version + ', I am ' + getUser() + '.';

alert ( myString );

function getUser() {

var myUser = ($.os.substring(0, 7)=="Windows") // I'M USING A MAC SO DO I JUST REPLACE "Windows" with "MAC".  ALSO I WOULD LIKE IT TO GRAB THE USER NAME FROM INDESIGN NOT THE COMPUTER*IF POSSIBLE*

  ? $.getenv("USERNAME")

  : $.getenv("USER");

return myUser;

}

This topic has been closed for replies.
Correct answer Laubender

Or from  bRewrite!==false  from false to true


Hi together,

I would not work with custom text variables at all.

Just with a plain event listening mechanism.

Adding and updating information in the text frame will be done every time the user is saving the document with the "Save" command.

For a demonstration I recorded a video clip you can download from my Dropbox account here:

Dropbox - beforeSave-doTextFrameOnPageOne.mov

The code I was using is below.

Customize it to your needs.

E.g. add the text frame on a master page, if you want it on every page where the master is applied.

Name the frame like you want. If you are using more than one frame you have to loop through every occurence of the frame if you do duplicates.

Change the position of the frame, change the contents to your needs, the frame's color, text size, etc.pp.

Here the basic code for adding and updating a text frame on page one of a document every time the "Save" command is executed by the user:

// beforeSave-doTextFrameOnPageOne.jsx

// Uwe Laubender

/**

* @@@BUILDINFO@@@ beforeSave-doTextFrameOnPageOne.jsx !Version! Thu May 19 2016 09:22:13 GMT+0200

*/

#targetengine "chooseReasonableNameHere"

app.addEventListener( "beforeSave" , doTextFrameOnPageOne );

function doTextFrameOnPageOne()

{

  

    var frameName = "Temp";

    var doc = app.documents[0];

    var tempFrame = doc.textFrames.itemByName(frameName);

  

    var contentsOfFrame =

        doc.name+"\r"+

        getLogInUserName() +"\r"+

        getLocaleDateString() +"\r"+

        getAppUserName();

   

    // Change the propeties to your needs:

    var tempFrameProperties =

    {

        name : frameName ,

        contents : contentsOfFrame ,

        fillColor : "Yellow" ,

        geometricBounds : [0,0,"50mm","50mm"]

    }

    if(!tempFrame.isValid)

    {

        var tempFrame = doc.pages[0].textFrames.add();

        tempFrame.properties = tempFrameProperties;

    };

  

    tempFrame.contents = contentsOfFrame;

}

function getLogInUserName()

{

    var userNameOSX = $.getenv("USER");

    var userNameWindows = $.getenv("USERNAME");

    if(userNameWindows == null){return userNameOSX}

    else{return userNameWindows};

}

function getAppUserName()

{

    return app.userName;

}

function getLocaleDateString()

{

    return new Date().toLocaleString();

}

Important notes:

1. If the document is not saved at all and you save it the first time, the event is not fired.

2. If you test the script and you want to do changes and execute the script again, quit and restart InDesign

I did not test extensively. All tests are done with InDesign CS6 v8.1.0 on Mac OSX 10.10.3.

3. If the script is placed in a startup scripts folder the script will be automatically activated after InDesign is started and is active until you quit InDesign.

4. If the script is executed from InDesign's Scripts Panel it will be active until you quit InDesign.

Hope, that helps.

Uwe

2 replies

Peter Kahrel
Community Expert
Community Expert
May 16, 2016

app.userName returns the name of the user as specified in File > User.

Peter

cbishop01Author
Inspiring
May 16, 2016

#targetengine "myUniqueName" 

 

 

app.addEventListener 

(  

      "beforeSave" ,  

     doTextFrameOnPageOne  

); 

 

function doTextFrameOnPageOne() 

{

   

var myString = app.userName

/*var myString = 'Hallo InDesign ' + app.userName + ', I am ' + getUser() + '.';*/

alert ( myString );

function getUser() {

var myUser = app.userName()

  ? $.getenv("USERNAME")

  : $.getenv("USER");

return myUser;

}

      alert("Event \"beforeSave\" just fired."); 

}; 

cbishop01Author
Inspiring
May 16, 2016

Would this work for the username part? I am not getting any errors when i run the script but i am also not seeing if its getting my user name.

cbishop01Author
Inspiring
May 16, 2016

Here's what i'm wanting the end result to be.  I want the <My NAME HERE> to be replaced with the Indesign USER name.  I have the date being set by a text variable.  Thanks to some help from  on the Indesign form.  and