Copier le lien dans le Presse-papiers
Copié
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;
}
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
...Copier le lien dans le Presse-papiers
Copié
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 Jeffrey_Smith on the Indesign form. Peter Spier and Laubender
suggested i see about a script to fill in the Name part. If i need to i can create a custom Text variable on each computer and have the script get the Custom Variable(probably called NAME) and place it in the "My name here" Part as soon as the Save button is pressed.
Copier le lien dans le Presse-papiers
Copié
app.userName returns the name of the user as specified in File > User.
Peter
Copier le lien dans le Presse-papiers
Copié
#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.");
};
Copier le lien dans le Presse-papiers
Copié
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.
Copier le lien dans le Presse-papiers
Copié
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
Copier le lien dans le Presse-papiers
Copié
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?
Copier le lien dans le Presse-papiers
Copié
Given that the user also installed the script, then yes. Otherwise no.
Copier le lien dans le Presse-papiers
Copié
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.
Copier le lien dans le Presse-papiers
Copié
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.
Copier le lien dans le Presse-papiers
Copié
Other than that this script will solve all my problems.
Copier le lien dans le Presse-papiers
Copié
i think this will give the user name
var userNames = $.getenv($.os[0] == "M" ? "USER" : "username");
alert(userNames)
Copier le lien dans le Presse-papiers
Copié
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
Copier le lien dans le Presse-papiers
Copié
Copier le lien dans le Presse-papiers
Copié
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;
}
Copier le lien dans le Presse-papiers
Copié
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.
Copier le lien dans le Presse-papiers
Copié
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.
Copier le lien dans le Presse-papiers
Copié
Will changing app.userName. false to true fix this? I'm not on my workplace now so I cannot test it
Copier le lien dans le Presse-papiers
Copié
Or from bRewrite!==false from false to true
Copier le lien dans le Presse-papiers
Copié
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
Copier le lien dans le Presse-papiers
Copié
Thank you my friend. This does everything i was wanting.
Copier le lien dans le Presse-papiers
Copié
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.
Trouvez plus d’idées, d’événements et de ressources dans la nouvelle communauté Adobe
Explorer maintenant