Quitter
  • Communauté internationale
    • Langue:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

Placing a USER name in a box?

Conseiller ,
May 16, 2016 May 16, 2016

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;

}

SUJETS
Scripting
2.2K
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines

correct answers 1 bonne réponse

Community Expert , May 19, 2016 May 19, 2016

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

...
Traduire
Conseiller ,
May 16, 2016 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

Screen Shot 2016-05-13 at 9.25.21 AM.png

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
May 16, 2016 May 16, 2016

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

Peter

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 16, 2016 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."); 

}; 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 16, 2016 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.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Défenseur du peuple ,
May 17, 2016 May 17, 2016

Hi I wrote an utility a few years ago for adding highly customized variables within InDesign such as the user name, the computer name and so on:

http://www.ozalto.com/add-custom-indesign-variables/

Hope that helps,

Loic

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 17, 2016 May 17, 2016

I only have this on my computer right now.  If i place this on other people's computers will the user name auto update when the file is changed/Saved?

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Défenseur du peuple ,
May 17, 2016 May 17, 2016

Given that the user also installed the script, then yes. Otherwise no.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 17, 2016 May 17, 2016

Ok i'll test it out on the other computers when they leave for the night so that way i can see if it does what i'm needing.  If so i'll mark this correct.  Thanks either way though.  this is a great script.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 17, 2016 May 17, 2016

Is there a way i can keep this with the Indesign User name?  Everytime i change it to My name or Other people's names it does not keep the saved settings When i shut down and start up indesign again it goes back to the default Computer user name.  I'm selecting the User Name variable at the bottom of the list.  I have checked my Indesign user name just to be sure i have it set and it is.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 17, 2016 May 17, 2016

Other than that this script will solve all my problems. 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Guide ,
May 18, 2016 May 18, 2016

i think this will give the user name

var userNames = $.getenv($.os[0] == "M" ? "USER" : "username");

    alert(userNames)

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
May 18, 2016 May 18, 2016

Hi Karthik,

yes, but we have to sort out what kind of username the OP is after.

( I did not test or see into Loïc's script )

1. The user name that denotes the logged in user of the machine => Your post or the one the OP is lending from Martin Fischer

2. The user name that is defined in InDesign's user interface under File > User => Peter Kahrel's post

I'm a little confused 😉

Uwe

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Guide ,
May 18, 2016 May 18, 2016

Hi Uwe,

I hope cbishop01 asking system name with indesign user name. As per Peter method we can get indesign user name. I gave the code that will show you the system name (cbishop01 said using MAC)

Not sure this is cbishop01 requirement.

Thanks,

Karthi

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Défenseur du peuple ,
May 18, 2016 May 18, 2016

Based on Peter's comment, here is a fix that takes the indesign user name into account.

You can now use a boolean parameter to let the script know if you want to actually update the variable value or not.

//Adding InDesign User name to text variables

  createTextVariable(doc, "InDesign User name", app.userName, false );

//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;

}

http://support.ozalto.com/downloads/addCustomVariables.zip

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 18, 2016 May 18, 2016

as of right now this is perferct.   I'll need to double check it with the other computers.  But for now it is Perfect.  Thank you.  If it works with the computer i'll mark it correct.  again thanks.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 18, 2016 May 18, 2016

Ok i tested it today.  It did not work for saving from the other computers here.  It only showed my Indesign User name and does not display there's when the file is saved.  After i opened it on the other computer saved it . and opened it back on mine it kept my name the whole time.  So its close just not working for me all the way yet.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 18, 2016 May 18, 2016

Will changing app.userName. false to true fix this?  I'm not on my workplace now so I cannot test it

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 18, 2016 May 18, 2016

Or from  bRewrite!==false  from false to true

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
May 19, 2016 May 19, 2016

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

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 19, 2016 May 19, 2016

Thank you my friend.  This does everything i was wanting. 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Conseiller ,
May 19, 2016 May 19, 2016
LA PLUS RÉCENTE

One quick question.  More for just making it look cleaner can i put those in rows?  Again this script is exactly what i wanted and works great now.  I'm just trying to make it as easy to read as possible. 

Thank you again for all your work on this.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines