Copy link to clipboard
Copied
This is almost perfect. A few things. When i save the file it keeps placing the information over the otehr rather than rewriting it. Meaning if i save it at 3:00 then save again at 301 it will keep both and just place them over one another. How can i keep only the latest one. A few other things That you will see when i post the Script.
app.addEventListener( "beforeSave" , doTextFrameOnPageOne );
function doTextFrameOnPageOne()
{
var columnOneContentsArray = [
"Document:" ,
"User Name:",
"Computer Name:",
"Date Modified:",
"Date Output:",
"Date Created:",
"Output Date:"
];
var columnTwoContentsArray = [
/*
"",
"",
""
*/
];
var myBlendingSettings = { blendMode : BlendMode.OVERLAY };
var myTransparencySettings = { blendingSettings : myBlendingSettings };
var doc = app.activeDocument;
var tf = doc.textFrames.add({geometricBounds: ['-.75','-4.45in','.75in','-.75in'] });
var myTable = tf.texts[0].tables.add
(
{
// Number of Rows and Columns.
bodyRowCount : 6 ,
columnCount : 2
// Use other property/value pairs to define the width of the table, stroke weights, fill color etc.pp.
}
);
var R1, R2, R3, R4, R5, R6, ip;
ip = tf.insertionPoints;
myTable.columns[0].contents = columnOneContentsArray;
myTable.columns[1].contents = columnTwoContentsArray;
var cell0OfColumn2insertionPoint1 = myTable.columns[1].cells[0].insertionPoints[0];
var R1 = cell0OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell0OfColumn2insertionPoint1);
R1.associatedTextVariable = doc.textVariables.itemByName("File Name");
//-----------------> CANNOT GET THESE TO RUN R2, R3 <--------------------\\
var cell1OfColumn2insertionPoint1 = myTable.columns[1].cells[1].insertionPoints[0];
var R2 = cell1OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell1OfColumn2insertionPoint1);
R2.associatedTextVariable = doc.textVariables.itemByName("userName"); // Cannot find a text variable for User name.
var cell0OfColumn2insertionPoint1 = myTable.columns[1].cells[2].insertionPoints[0];
var R3 = cell2OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell2OfColumn2insertionPoint1);
R3.associatedTextVariable = doc.textVariables.itemByName("Login User Name"); // Same as User name.
//------------------------------------------------------------------------------------\\
var cell3OfColumn2insertionPoint1 = myTable.columns[1].cells[3].insertionPoints[0];
var R4 = cell3OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell3OfColumn2insertionPoint1);
R4.associatedTextVariable = doc.textVariables.itemByName("Modification Date");
var cell5OfColumn2insertionPoint1 = myTable.columns[1].cells[4].insertionPoints[0];
var R5 = cell5OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell5OfColumn2insertionPoint1);
R5.associatedTextVariable = doc.textVariables.itemByName("Output Date"); // How can i make this show the date string? Like The Modification Date!
var cell6OfColumn2insertionPoint1 = myTable.columns[1].cells[5].insertionPoints[0];
var R6 = cell6OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell6OfColumn2insertionPoint1);
R6.associatedTextVariable = doc.textVariables.itemByName("Creation Date");
}
i know userName is not the right one for the variable as well as Login user name but i cannot find the Variable name.. below is how i did it before if i could add the below to the above it would be great. or if there are variables that i cannot see somewhere let me know thanks.
var myBlendingSettings = { blendMode : BlendMode.OVERLAY };
var myTransparencySettings = { blendingSettings : myBlendingSettings };
var doc, tf, tvINFO, ip;
doc = app.activeDocument;
// TEXT FRAME PROPERTIES
tf = doc.pages[0].textFrames.add({ fillColor :"Yellow", fillTint: 20, transparencySettings : myTransparencySettings, geometricBounds: ['-.75','-4.45in','1.5in','-.75in'] });
// Text frame information
ip = tf.insertionPoints;
ip[-1].contents = "Document:"+"\t "+ doc.name +'\r';
ip[-1].contents = "User Name:" +"\t"+ getAppUserName() +'\r';
ip[-1].contents = "Computer Name:" +"\t"+ getLogInUserName()+'\r';
tvINFO = ip[-1].textVariableInstances.add(LocationOptions.AFTER, ip[-1]);
"Date Modified:" +'\t'; tvINFO.associatedTextVariable = doc.textVariables.itemByName("Modification Date");
ip[-1].contents = '\r';
tvINFO = ip[-1].textVariableInstances.add(LocationOptions.AFTER, ip[-1]);
tvINFO.associatedTextVariable = doc.textVariables.itemByName("Output Date");
ip[-1].contents = '\r';
tvINFO = ip[-1].textVariableInstances.add(LocationOptions.AFTER, ip[-1]);
tvINFO.associatedTextVariable = "Date Created:" +'\t'; doc.textVariables.itemByName("Creation Date");
tf.texts[0].convertToTable();
}
function getLogInUserName()
{
// Computer User Name
var userNameOSX = $.getenv("USER");
var userNameWindows = $.getenv("USERNAME");
if(userNameWindows == null){return userNameOSX}
else{return userNameWindows};
}
function getAppUserName()
{
return app.userName;
}
Thanks again ..
Hi cbishop,
Seems like your issue is not resolved yet and taking long time...
I have reviewed your code and made modifications. I believe I have addressed all of your issues. See comments.
Comments
1. Now the code addresses the issue of multiple text frames being created with SaveInfo details.
2. The text frame with SaveInfo details actually will now be added into the layer named SaveInfo. I think it was not achieved in your code.
3. You can now address any number of documents. I have handled it in th
...Copy link to clipboard
Copied
You can get/set environment variables using $.getenv and $.setenv functions.
// for windows
var userName = $.getenv("USERNAME");
//For Mac, if above works then fine else try below.
var userName = $.getenv("USER"); // mac
Copy link to clipboard
Copied
There are only 10 pre-defined variables (default) available on doc object.
TV XRefChapterNumber
TV XRefPageNumber
Chapter Number
Creation Date
File Name
Image Name
Last Page Number
Modification Date
Output Date
Running Header
If you want to use any other apart from above then you can add them first to doc.
doc.textVariables.add(yourvar);
and then use it as instance in your doc.
Copy link to clipboard
Copied
Here's the code i have so far. I got the add Layers to work, I had misspelled one of the lines of code. A few more questions though.
1.) How can i make the text frame that the below Table is in have the name SaveInfo?
2.) How to make this script work in all Documents not just the active.. (If i save maybe 6 files at a time i would like the script to apply to all those documents as well.)
3.) I want it to only save one instance. Now i can save as many times as i want but every time i do it just places another text box on top of the other ones. I only want one box that keeps updating when saved. I'll paste picture in another reply so you can see what i'm saying incase i'm not making sense.
#targetengine "AutoStartScript"
app.addEventListener( "beforeSave" , doTextFrameOnPageOne );
/* app.addEventListener( "beforeSaveAll" , doTextFrameOnPageOne ); */
function doTextFrameOnPageOne()
{
var doc = app.activeDocument;
// Create New Layer
var myLayer = doc.layers;
var myPage = doc.pages;
var flag=false;
checkLayer();
function checkLayer(){
for (var l=0; myLayer.length>l; l++){
if (myLayer
.name =="SaveInfo"){ flag=true;
}
}
if (flag==false){
var myNewLayer = doc.layers.add();
myNewLayer.move(LocationOptions.atBeginning, undefined);
myNewLayer.name = "SaveInfo";
}
}
var columnOneContentsArray = [
"Document:" ,
"User Name:",
"Computer Name:",
"Date Modified:",
"Date Output:",
"Date Created:",
"Output Date:"
];
var columnTwoContentsArray = [
/*
"",
"",
""
*/
];
var myBlendingSettings = { blendMode : BlendMode.OVERLAY };
var myTransparencySettings = { blendingSettings : myBlendingSettings };
var tf = doc.textFrames.add({fillColor :"Yellow", fillTint: 20, transparencySettings : myTransparencySettings, geometricBounds: ['-.75','-4.45in','.75in','-.75in'] });
var myTable = tf.texts[0].tables.add
(
{
// Number of Rows and Columns.
bodyRowCount : 6 ,
columnCount : 2
// Use other property/value pairs to define the width of the table, stroke weights, fill color etc.pp.
}
);
// Create New Text Variables
tv = doc.textVariables.item("Users Name");
!tv.isValid && tv = doc.textVariables.add({name:"Users Name", variableType:VariableTypes.CUSTOM_TEXT_TYPE});
tv.variableOptions.contents = String( getAppUserName() );
tvL = doc.textVariables.item("CLN");
!tvL.isValid && tvL = doc.textVariables.add({name:"CLN", variableType:VariableTypes.CUSTOM_TEXT_TYPE});
tvL.variableOptions.contents = String( getLogInUserName() );
// Placing information in tables
var R1, R2, R3, R4, R5, R6;
myTable.columns[0].contents = columnOneContentsArray;
myTable.columns[1].contents = columnTwoContentsArray;
var cell0OfColumn2insertionPoint1 = myTable.columns[1].cells[0].insertionPoints[0];
var R1 = cell0OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell0OfColumn2insertionPoint1);
R1.associatedTextVariable = doc.textVariables.itemByName("File Name");
var cell1OfColumn2insertionPoint1 = myTable.columns[1].cells[1].insertionPoints[0];
var R2 = cell1OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell1OfColumn2insertionPoint1);
R2.associatedTextVariable = doc.textVariables.itemByName("Users Name")
var cell2OfColumn2insertionPoint1 = myTable.columns[1].cells[2].insertionPoints[0];
var R3 = cell2OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell2OfColumn2insertionPoint1);
R3.associatedTextVariable = doc.textVariables.itemByName("CLN");
var cell3OfColumn2insertionPoint1 = myTable.columns[1].cells[3].insertionPoints[0];
var R4 = cell3OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell3OfColumn2insertionPoint1);
R4.associatedTextVariable = doc.textVariables.itemByName("Modification Date");
var cell4OfColumn2insertionPoint1 = myTable.columns[1].cells[4].insertionPoints[0];
var R5 = cell4OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell4OfColumn2insertionPoint1);
R5.associatedTextVariable = doc.textVariables.itemByName("Output Date"); // How can i make this show the date string? Like The Modification Date!
var cell5OfColumn2insertionPoint1 = myTable.columns[1].cells[5].insertionPoints[0];
var R6 = cell5OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell5OfColumn2insertionPoint1);
R6.associatedTextVariable = doc.textVariables.itemByName("Creation Date");
}
// Computer User Name
function getLogInUserName()
{
var userNameOSX = $.getenv("USER");
var userNameWindows = $.getenv("USERNAME");
if(userNameWindows == null){return userNameOSX}
else{return userNameWindows};
}
// Application Name
function getAppUserName()
{
return app.userName;
}
Copy link to clipboard
Copied
This is what it should look like. The yellow will overlay on anything that may be under it so if we have something in the pastboard we can still see it.
This is what it looks like after saving 4 times in a row. the other one below just shows that it just places a new text box ontop of the old one.
!
Copy link to clipboard
Copied
Give the text frame a name when you add it.
Always check, if the text frame is already there. If it is there, update its contents only.
var textFrameName = "AfterSaveTextFrame";
if(app.documents[0].textFrames.itemByName(textFrameName).isValid)
{
customFunctionUpdateContents();
}
else
{
customFunctionAddTextFrame()
}
I was showing this in one of my first answers of one of your threads that are revolving around that same topics.
Uwe
Copy link to clipboard
Copied
I tried something like that the other day but it wouldn't work. But I was not using an if else statement. The below is the one i use on earlier on this one. but it will not work now that i changed this to a table layout. Or at least i couldnt get the information to be placed in it. It made the text frame but did not name it. (On this new Code)
var frameName = "Save_Information";
var doc, tvINFO, ip, contentsOfFrame;
var doc = app.activeDocument;/* */
var tempFrame = doc.textFrames.itemByName(frameName);
Copy link to clipboard
Copied
When constructing the text frame by using the add() method you could give a name property plus value.
Or immediately after using the add() method.
var myTextFrame = app.documents[0].textFrames.add
(
{
name : "Save_Information" ,
// All other properties would follow here:
// geometricBounds : [0,0,"100mm","100mm"] ,
// etc.pp.:
}
);
That would happen in the customFunctionAddTextFrame() I was suggesting in my post above.
However, there is your issue where you like to save all open documents in one go, if I remember right.
Hm. EventListeners are tricky…
Regards,
Uwe
Copy link to clipboard
Copied
I'm still not getting the First textFrameName you posted to work. IF i type/paste it anywhere in the script the script breaks and will not run. As for the second one. it will add a separate Text frame with the correct name and i cannot get it to rename my text frame with the table in it. I will keep messing around and see if i can get the second one to work. Now for the save as i can still save all documents and it will save them. It just will not display the Boxes above with the SaveInfo.
Copy link to clipboard
Copied
This is the text frame code i was originally using that worked until i added the tables.
Copy link to clipboard
Copied
When i do the below code the Frame name does not work. it actuall breaks my Table script. And the text frame is empty
var tf, myTable, myBlendingSettings, myBlendingSettings, myTransparencySettings, textFrameName;
myBlendingSettings = { blendMode : BlendMode.OVERLAY };
myTransparencySettings = { blendingSettings : myBlendingSettings };
tf = doc.textFrames.add({fillColor :"Yellow", fillTint: 30, transparencySettings : myTransparencySettings, geometricBounds: ['0','-5.45in','1.45','-.75in'] });
textFrameName = "SaveInfo";
if(app.documents[0].textFrames.itemByName(textFrameName).isValid)
{
customFunctionUpdateContents();
}
else
{
customFunctionAddTextFrame()
}
myTable = tf.texts[0].tables.add
(
{
// Number of Rows and Columns.
bodyRowCount : 6 ,
columnCount : 2
}
);
// Column Properties
setColumnWidthsAndAlignments(myTable)
function setColumnWidthsAndAlignments(tableObj)
{
var myWidths=[1.35, 3.32];
var myAlignments=[Justification.LEFT_ALIGN,Justification.LEFT_ALIGN];
var numberOfColumns=tableObj.columns.count();
for (c=0;c<numberOfColumns;c++)
{
myTable.columns
.width=myWidths ; myTable.columns
.cells.everyItem().texts.everyItem().justification=myAlignments ; }
}
Copy link to clipboard
Copied
Hi cbishop,
Seems like your issue is not resolved yet and taking long time...
I have reviewed your code and made modifications. I believe I have addressed all of your issues. See comments.
Comments
1. Now the code addresses the issue of multiple text frames being created with SaveInfo details.
2. The text frame with SaveInfo details actually will now be added into the layer named SaveInfo. I think it was not achieved in your code.
3. You can now address any number of documents. I have handled it in the eventlistener to capture the target document. This is simple.
4. Naming the textframe issue is also fixed. Simple approach. You can check the code.
Let me know if all is well.
#targetengine "AutoStartScript"
app.addEventListener( "beforeSave" , doTextFrameOnPageOne );
function addLayer(doc, name) {
try{
var layer = doc.layers.itemByName(name);
if(layer && layer.isValid) {
return layer;
} else {
var myLayer = doc.layers.add();
myLayer.name = name;
return myLayer;
}
} catch(e) {
}
}
function addTextFrame(doc, layer, name) {
var myBlendingSettings = { blendMode : BlendMode.OVERLAY };
var myTransparencySettings = { blendingSettings : myBlendingSettings };
var tf = doc.textFrames.itemByName(name);
if(tf && tf.isValid) {
tf.remove();
}
tf = doc.textFrames.add(layer, LocationOptions.UNKNOWN, {name: name, fillColor :"Yellow", fillTint: 20, transparencySettings : myTransparencySettings, geometricBounds: ['-.75in','-4.45in','1.0in','-.75in'] });
return tf;
}
function addTable(tf) {
var myTable = tf.texts[0].tables.add
(
{
// Number of Rows and Columns.
bodyRowCount : 6 ,
columnCount : 2
// Use other property/value pairs to define the width of the table, stroke weights, fill color etc.pp.
}
);
return myTable;
}
function addVariable(doc, name, value) {
var v = doc.textVariables.itemByName(name);
if(v && v.isValid)
return;
v = doc.textVariables.add({name:name, variableType:VariableTypes.CUSTOM_TEXT_TYPE});
v.variableOptions.contents = value;
}
function doTextFrameOnPageOne(event)
{
var doc = event.target;
if(!(doc && doc.constructor.name == "Document")) {
return;
}
// Create New Layer
var myLayer = addLayer(doc, "SaveInfo");
var columnOneContentsArray = [
"Document:" ,
"User Name:",
"Computer Name:",
"Date Modified:",
"Date Output:",
"Date Created:",
"Output Date:"
];
var columnTwoContentsArray = [
/*
"",
"",
""
*/
];
var tf = addTextFrame(doc, myLayer, "SaveInfo");
var myTable = addTable(tf);
// Create New Text Variables
addVariable(doc, "Users Name", getAppUserName());
addVariable(doc, "CLN", getLogInUserName());
// Placing information in tables
var R1, R2, R3, R4, R5, R6;
myTable.columns[0].contents = columnOneContentsArray;
myTable.columns[1].contents = columnTwoContentsArray;
var cell0OfColumn2insertionPoint1 = myTable.columns[1].cells[0].insertionPoints[0];
var R1 = cell0OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell0OfColumn2insertionPoint1);
R1.associatedTextVariable = doc.textVariables.itemByName("File Name");
var cell1OfColumn2insertionPoint1 = myTable.columns[1].cells[1].insertionPoints[0];
var R2 = cell1OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell1OfColumn2insertionPoint1);
R2.associatedTextVariable = doc.textVariables.itemByName("Users Name")
var cell2OfColumn2insertionPoint1 = myTable.columns[1].cells[2].insertionPoints[0];
var R3 = cell2OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell2OfColumn2insertionPoint1);
R3.associatedTextVariable = doc.textVariables.itemByName("CLN");
var cell3OfColumn2insertionPoint1 = myTable.columns[1].cells[3].insertionPoints[0];
var R4 = cell3OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell3OfColumn2insertionPoint1);
R4.associatedTextVariable = doc.textVariables.itemByName("Modification Date");
var cell4OfColumn2insertionPoint1 = myTable.columns[1].cells[4].insertionPoints[0];
var R5 = cell4OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell4OfColumn2insertionPoint1);
R5.associatedTextVariable = doc.textVariables.itemByName("Output Date"); // How can i make this show the date string? Like The Modification Date!
var cell5OfColumn2insertionPoint1 = myTable.columns[1].cells[5].insertionPoints[0];
var R6 = cell5OfColumn2insertionPoint1.textVariableInstances.add(LocationOptions.AFTER, cell5OfColumn2insertionPoint1);
R6.associatedTextVariable = doc.textVariables.itemByName("Creation Date");
}
// Computer User Name
function getLogInUserName()
{
var userNameOSX = $.getenv("USER");
var userNameWindows = $.getenv("USERNAME");
if(userNameWindows == null){return userNameOSX}
else{return userNameWindows};
}
// Application Name
function getAppUserName() {
return app.userName;
}
Copy link to clipboard
Copied
You my Friend are awesome. Its Perfect. I was way off on this code ..
Find more inspiration, events, and resources on the new Adobe Community
Explore Now