Copy link to clipboard
Copied
I have been working on this code for a little while now (with the help of users here...thank you!). Here is the code in it's entirety...
//#target illustrator-22
// Function to collect user input
function getText(field) {
return field.text !== undefined ? field.text : "";
}
// Function to build panel
function createPanel(parent, title) {
// Create Panel
var panel = parent.add("panel", undefined, title);
panel.orientation = "column";
// Return panel
return panel;
}
// Function to create button
function createButton(parent, title, onClick) {
// Create Button
var button = parent.add("button", undefined, title);
if (onClick !== undefined) button.onClick = onClick;
// Return button
return button;
}
// Function to create field in panel
function createTextField(parent, title, content) {
// Create a group for title and field
var group = createGroup(parent, "column");
group.alignChildren = 'left';
// Create title
var title = group.add("statictext", undefined, title);
var field = group.add("edittext", undefined, content);
field.preferredSize = [200, 23];
// Return the field as its all you'll use
return field;
}
// Function to organize the panel layout
function createGroup(parent, orientation) {
// Create Group
var group = parent.add("group");
group.orientation = orientation;
// Return Group
return group;
}
// Panel fields and information
function createGUI() {
// Palette can be run from ESTK and will allow you to still work in Illustrator while it is open
// var gui = new Window("palette", "Schematic Information Tool");
// Dialog can be run from ESTK or Scripts Menu but will not allow you to work in Illustrator while the window is open
var gui = new Window("dialog", "Schematic Information Tool");
// Progress bar
gui.pnl = gui.add("panel", [10, 10, 440, 100], "Script Progress");
gui.pnl.progBar = gui.pnl.add("progressbar", [20, 35, 410, 60], 0, 100);
gui.pnl.progBarLabel = gui.pnl.add("statictext", [20, 20, 320, 35], "0%");
//gui.alignChildren = ["fill", "fill"];
gui.orientation = "column";
var panelGroup = createGroup(gui, "row");
///////////////// Left Panel Group Fields - Cover Page /////////////////
// Left Panel Title - Static text used as an identifier
var leftInfoPanel = createPanel(panelGroup, "Cover Page");
// Media Number field
var mediaNumber = createTextField(leftInfoPanel, "Media Number:", "XENRXXXX-XX");
// Title Description Line field
// Format so if character count is over 27 move to next line
var titleDescription = createTextField(leftInfoPanel, "Title Description Line:", "(Add Title)");
var serialNumbers = createTextField(leftInfoPanel, "Serial Numbers:", "XXX1-UP, XXX1-UP, XXX1-UP");
///////////////// Right Panel Group Fields - Title Block /////////////////
// Right Panel Title - Static text used as an identifier
var rightInfoPanel = createPanel(panelGroup, "Title Block");
// Schematic Part Number field
var schematicPartNumber = createTextField(rightInfoPanel, "Schematic Part Number:", "XXX-XXXX");
// Schematic Change Level field
var schematicChangeLevel = createTextField(rightInfoPanel, "Schematic Change Level:", "XX");
// Schematic Version field
var schematicVersion = createTextField(rightInfoPanel, "Schematic Version:", "XX");
///////////////// Attachment Description Group Fields /////////////////
// Group container for attachment description field
var attachmentGrouping = gui.add("group");
// Attachment Description - Static text used as an identifier
var attachmentText = attachmentGrouping.add("statictext", undefined, "Attachment Description:");
// Attachment Description field
var attachmentDescription = attachmentGrouping.add("edittext", undefined, "");
// Attachment Description field sizing
attachmentDescription.preferredSize = [320, 23];
///////////////// Date Group Fields - Month Dropdown and Year /////////////////
// Group container for date fields
var dateGrouping = gui.add("group");
// Get the current date from the computer
var getTheDate = new Date();
// Get the current month from the getTheDate variable - Month is given as a number 0 - 11
var theCurrentMonth = getTheDate.getMonth();
// Cover Page Month static text
var monthStaticText = dateGrouping.add("statictext", undefined, "Cover Page Month:");
// Dropdown menu to select the month for cover page
var monthDropDown = dateGrouping.add("dropdownlist", undefined, ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
// Set the default selection for the dropdown menu to the current month
monthDropDown.selection = theCurrentMonth;
// Get the current year from the getTheDate
var theCurrentYear = getTheDate.getFullYear();
// Cover Page Year static text
var yearStaticText = dateGrouping.add("statictext", undefined, "Cover Page Year:");
// Cover Page Year field for user input - Default set to current year
var yearTextField = dateGrouping.add("edittext", undefined, theCurrentYear);
// Set the size of the Cover Page Year field box
yearTextField.preferredSize = [50, 23];
// Group container for buttons
var buttons = createGroup(gui, "row");
// Confirm button
var confirmBtn = createButton(buttons, "Confirm", function() {
try {
while (gui.pnl.progBar.value < gui.pnl.progBar.maxvalue) {
for (var i = 0; i < documents.length; i++) {
var currentActiveDoc = app.documents;
app.activeDocument = currentActiveDoc;
var allPageItems = app.activeDocument.pageItems;
var theDocVariables = currentActiveDoc.variables;
var allLayers = currentActiveDoc.layers;
// Loop through all the variables in the active document
for (z = 0; z < theDocVariables.length; z++) {
// Function for description lines/title
function formatDesc(str) {
var inputLen = str.length;
inputArray = str.split(" "),
inputArrayLen = inputArray.length,
formattedStr = "",
curLen = 0,
LINE_LENGTH = 27;
numberOfLines = 0;
if (inputLen > 108) {
alert("Your description is too long");
}
for (var x = 0; x < inputArrayLen; x++) {
if (curLen + inputArray
.length < LINE_LENGTH) { formattedStr += " " + inputArray
; curLen += inputArray
.length + 1; } else {
formattedStr += "\n" + inputArray
; curLen = inputArray
.length; }
if (formattedStr.match(/\n/) != null) {
numberOfLines++;
}
}
return formattedStr;
}
// Title description test strings
// This is a test - 14
// This is a test for the description - 34
// This is a test for the description line field length output - 59
// This is a test for the description line field length output to check the number of lines - 88
var userInput = getText(titleDescription);
var coverPageLineCount;
var formattedOutput = formatDesc(userInput);
// Remove leading space
formattedOutput = formattedOutput.replace(/^\s+|\s+$/g, "");
// Gets model number and capitalizes anything with it. Gets machine type and capitalizes first letter
formattedOutput = formattedOutput.replace(/(?!and|with|for)[0-9]\B\w+|(?!and|with|for)\b[a-z]/g, function(f) {
return f.toUpperCase();
})
// Single Line cover page description
if (userInput.length < 27 && theDocVariables
.name == "coverPageDecriptionLineOne") { theDocVariables
.pageItems[0].contents = formattedOutput; coverPageLineCount = 1;
var layerOneOn = currentActiveDoc.layers["Single Line"];
layerOneOn.visible = true;
var layerTwoOn = currentActiveDoc.layers["Two Line"];
layerTwoOn.visible = false;
var layerThreeOn = currentActiveDoc.layers["Three Line"];
layerThreeOn.visible = false;
var layerFourOn = currentActiveDoc.layers["Four Line"];
layerFourOn.visible = false;
var targetLayer = app.activeDocument.layers["Single Line"];
}
// Two Line cover page description
if ((userInput.length > 27 && userInput.length < 54) && theDocVariables
.name == "coverPageDecriptionLineTwo") { theDocVariables
.pageItems[0].contents = formattedOutput; coverPageLineCount = 2;
var layerOneOn = currentActiveDoc.layers["Single Line"];
layerOneOn.visible = false;
var layerTwoOn = currentActiveDoc.layers["Two Line"];
layerTwoOn.visible = true;
var layerThreeOn = currentActiveDoc.layers["Three Line"];
layerThreeOn.visible = false;
var layerFourOn = currentActiveDoc.layers["Four Line"];
layerFourOn.visible = false;
var targetLayer = app.activeDocument.layers["Two Line"];
}
// Three Line cover page description
if ((userInput.length > 54 && userInput.length < 81) && theDocVariables
.name == "coverPageDecriptionLineThree") { theDocVariables
.pageItems[0].contents = formattedOutput; coverPageLineCount = 3;
var layerOneOn = currentActiveDoc.layers["Single Line"];
layerOneOn.visible = false;
var layerTwoOn = currentActiveDoc.layers["Two Line"];
layerTwoOn.visible = false;
var layerThreeOn = currentActiveDoc.layers["Three Line"];
layerThreeOn.visible = true;
var layerFourOn = currentActiveDoc.layers["Four Line"];
layerFourOn.visible = false;
var targetLayer = app.activeDocument.layers["Three Line"];
}
// Four Line cover page description
if ((userInput.length > 81 && userInput.length < 108) && theDocVariables
.name == "coverPageDecriptionLineFour") { theDocVariables
.pageItems[0].contents = formattedOutput; coverPageLineCount = 4;
var layerOneOn = currentActiveDoc.layers["Single Line"];
layerOneOn.visible = false;
var layerTwoOn = currentActiveDoc.layers["Two Line"];
layerTwoOn.visible = false;
var layerThreeOn = currentActiveDoc.layers["Three Line"];
layerThreeOn.visible = false;
var layerFourOn = currentActiveDoc.layers["Four Line"];
layerFourOn.visible = true;
var targetLayer = app.activeDocument.layers["Four Line"];
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Media Number for the title blocks
if (theDocVariables
.name == "titleBlockMedia") { var mediaCase = "MEDIA NUMBER: " + getText(mediaNumber).toUpperCase();
for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = mediaCase; }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Title for the title blocks
if (theDocVariables
.name == "titleBlockTitle") { var checkContents = theDocVariables
.pageItems[0].contents; var changeCase = (getText(titleDescription)).toUpperCase();
// Electrical title for title block
if (checkContents.substring(12, 29) == "ELECTRICAL SYSTEM") {
for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = changeCase + " ELECTRICAL SYSTEM"; }
}
// Hydraulic title for title block
if (checkContents.substring(12, 28) == "HYDRAULIC SYSTEM") {
for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = changeCase + " HYDRAULIC SYSTEM"; }
}
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Part number for title blocks
if (theDocVariables
.name == "titleBlockPart") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = "SCHEMATIC PART NUMBER: " + getText(schematicPartNumber); }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Change level for title blocks
if (theDocVariables
.name == "titleBlockChange") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = "CHANGE: " + getText(schematicChangeLevel); }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Version of schematic for title blocks
if (theDocVariables
.name == "titleBlockVersion") { var versionCase = "VERSION: " + (getText(schematicVersion)).toUpperCase();
for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = versionCase; }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Publish date on outside cover page
if (theDocVariables
.name == "coverPageDate_Outside") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = monthDropDown.selection.text + " " + yearTextField.text; }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
if (theDocVariables
.name == "yellowBoxMedia_Inside") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = getText(mediaNumber).toUpperCase(); }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
if (theDocVariables
.name == "yellowBoxMedia_Outside") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = getText(mediaNumber).toUpperCase(); }
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
if (theDocVariables
.name == "coverPageMedia_Outside") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { theDocVariables
.pageItems .contents = getText(mediaNumber).toUpperCase(); }
}
if (theDocVariables
.name == "attachmentDescriptionTitleBlock") { for (var j = 0; j < theDocVariables
.pageItems.length; j++) { //alert(attachmentDescription.text);
var passText = attachmentDescription.text.toUpperCase();
//alert(passText);
theDocVariables
.pageItems .contents = "ATTACHMENT: " + passText; //theDocVariables
.pageItems .contents = attachmentDescription.text.toUpperCase(); }
}
}
}
}
// Increase progress bar
gui.pnl.progBar.value++;
gui.pnl.progBarLabel.text = gui.pnl.progBar.value + "%";
$.sleep(10);
gui.update();
// Alerts outside the loop can go here
// Serial number test strings
// abc1-up,def1-up,ghi1-up,jkl1-up,mno1-up,pqr1-up,stu1-up,vwx1-up,yzz1-up,
// abc1-up, def1-up, ghi1-up, jkl1-up, mno1-up, pqr1-up, stu1-up, vwx1-up, yzz1-up
// abc1-up def1-up ghi1-up jkl1-up mno1-up pqr1-up stu1-up vwx1-up yzz1-up zzz10001-up
var allSerialNumbers = getText(serialNumbers).split(/,|, | /)
var moveDown = 12;
var xPosColOne = 3357.07;
var xPosColTwo = 3491;
var xPosColThree = 3624.70;
var xPosColFour = 3758.39
if (coverPageLineCount == 1) {
var yPosTopColOne = -474.20;
var yPosTopColTwo = -474.20;
var yPosTopColThree = -474.20;
var yPosTopColFour = -474.20;
}
if (coverPageLineCount == 2) {
var yPosTopColOne = -501.25;
var yPosTopColTwo = -501.25;
var yPosTopColThree = -501.25;
var yPosTopColFour = -501.25;
}
if (coverPageLineCount == 3) {
var yPosTopColOne = -528.46;
var yPosTopColTwo = -528.46;
var yPosTopColThree = -528.46;
var yPosTopColFour = -528.46;
}
if (coverPageLineCount == 4) {
var yPosTopColOne = -555.90;
var yPosTopColTwo = -555.90;
var yPosTopColThree = -555.90;
var yPosTopColFour = -555.90;
}
var doc = app.activeDocument;
var serialNumberRowCount = 1;
var grayScale = new GrayColor();
grayScale.gray = 100;
for (i = 0; i < allSerialNumbers.length; i++) {
if (allSerialNumbers != "") {
var newFrame = doc.textFrames.add();
// Font size
newFrame.textRange.characterAttributes.size = 9;
// Font
newFrame.textRange.characterAttributes.textFont = app.textFonts.getByName("ArialMT");
// Fill color
newFrame.textRange.characterAttributes.fillColor = grayScale;
// Make uppercase remove spaces
newFrame.contents = allSerialNumbers.replace(/^\s+|\s+$/gm, '').toUpperCase();
// Placement of the textFrame
newFrame.position = new Point(xPosColOne, yPosTopColOne);
// Move text to target layer
newFrame.moveToBeginning(targetLayer);
if (serialNumberRowCount <= 18) {
yPosTopColOne = (yPosTopColOne - moveDown);
}
if (serialNumberRowCount >= 19 && serialNumberRowCount <= 36) {
newFrame.position = new Point(xPosColTwo, yPosTopColTwo);
yPosTopColTwo = (yPosTopColTwo - moveDown);
}
if (serialNumberRowCount >= 37 && serialNumberRowCount <= 54) {
newFrame.position = new Point(xPosColThree, yPosTopColThree);
yPosTopColThree = (yPosTopColThree - moveDown);
}
if (serialNumberRowCount >= 55 && serialNumberRowCount <= 72) {
newFrame.position = new Point(xPosColFour, yPosTopColFour);
yPosTopColFour = (yPosTopColFour - moveDown);
}
serialNumberRowCount++;
}
}
// Create Attachment Description text box on cover page
if (attachmentDescription.text != "") {
for (var xx = 0; xx < allPageItems.length; xx++) {
if (allPageItems[xx].layer == "[Layer Media Info]" && allPageItems[xx].typename == "TextFrame") {
// Volume x of x: - Attachment Description field placement
// 42 page - 3357.07, -777.72
// 36 page - 2783.25, -777.74
// 30 page - 1603.30, -772.98
// 24 page - 1210.17, -781.56
// 20 page - 1614.26, -777.86
// 16 page - 1222.85, -777.83
// 12 page - 628.89, -777.32
// 8 page - 84.17, -817.30
// 4 page - 652.12, -739.50
if (allPageItems[xx].contents.substring(0, 2) == "42") {
//alert("I see you are using a 42 page template");
var useThisX = 3357.07;
var useThisY = -777.72;
}
if (allPageItems[xx].contents.substring(0, 2) == "36") {
//alert("I see you are using a 36 page template");
var useThisX = 2783.25;
var useThisY = -777.74;
}
if (allPageItems[xx].contents.substring(0, 2) == "30") {
//alert("I see you are using a 30 page template");
var useThisX = 1603.30;
var useThisY = -772.98;
}
if (allPageItems[xx].contents.substring(0, 2) == "24") {
//alert("I see you are using a 24 page template");
var useThisX = 1210.17;
var useThisY = -781.56;
}
if (allPageItems[xx].contents.substring(0, 2) == "20") {
//alert("I see you are using a 20 page template");
var useThisX = 1614.26;
var useThisY = -777.86;
}
if (allPageItems[xx].contents.substring(0, 2) == "16") {
//alert("I see you are using a 16 page template");
var useThisX = 1222.85;
var useThisY = -777.83;
}
if (allPageItems[xx].contents.substring(0, 2) == "12") {
//alert("I see you are using a 12 page template");
var useThisX = 628.89;
var useThisY = -777.32;
}
if (allPageItems[xx].contents.substring(0, 2) == "8 ") {
//alert("I see you are using a 8 page template");
var useThisX = 84.17;
var useThisY = -817.30;
}
if (allPageItems[xx].contents.substring(0, 2) == "4 ") {
//alert("I see you are using a 4 page template");
var useThisX = 652.12;
var useThisY = -739.50;
}
}
}
var attachmentTextFrame = doc.textFrames.add();
//alert(attachmentTextFrame.layer);
// Font size
attachmentTextFrame.textRange.characterAttributes.size = 14;
// Font
attachmentTextFrame.textRange.characterAttributes.textFont = app.textFonts.getByName("Arial-BoldMT");
// Fill color
attachmentTextFrame.textRange.characterAttributes.fillColor = grayScale;
attachmentTextFrame.contents = "ATTACHMENT: " + attachmentDescription.text.toUpperCase();
//attachmentTextFrame.contents = attachmentDescription.text.replace(/ /g, "");
// Placement of the textFrame - hard coded - assign variable for different page sizes
attachmentTextFrame.position = new Point(useThisX, useThisY);
// Move text to target layer
attachmentTextFrame.moveToBeginning(targetLayer);
}
gui.close();
} catch (e) {
alert(e);
}
});
// Cancel Button
var cancelBtn = createButton(buttons, "Cancel", function() {
gui.close();
});
return gui;
}
// Display the GUI
try {
// Change name of gui function to match function name
var gui = createGUI();
// Show the panel
gui.show();
} catch (e) {
alert(e);
}
I was wondering if anyone would want to critique the code or offer suggestions? It is working....but I am still learning.
Also the code for this loop starting on line 537....
for (i = 0; i < allSerialNumbers.length; i++) {
if (allSerialNumbers != "") {
var newFrame = doc.textFrames.add();
How can I make this run on any open document that contains any of the following layer names: "Single Line", "Two Line", "Three Line", Four Line"
If it doesn't have one of those layers then it doesn't create the text there.
Sorry if this is too lengthy or extensive code. I don't know what the etiquette is for that. I can delete the post if it is asking too much.
Thank you in advance!
Copy link to clipboard
Copied
Hi wolfe,
what does your code in the following cases:
userInput.length == 27 or
userInput.length == 54 or == 81 or == 108 or higher value?
Perhaps it is better to do something like this:
if ((userInput.length >= 27 && userInput.length < 54) or
if ((userInput.length > 27 && userInput.length <= 54) and so on
Copy link to clipboard
Copied
I started to look at this, but im sorry, the project i'm currently working on is a much higher priority. But the one thing i'd point out is your for loop between lines 576 and 642.
It appears you're running this loop on every single pageItem in a document. Then for each page item, you check to see whether the item you're looking at is a text frame on the correct layer, then you're running 9 conditional statements wherein you pull the text out of the frame, call the substring function, then compare it to something.
You'll save yourself a lot of processing time, especially on large documents, by actively targeting the specific layer you're looking for, rather than checking every item in the document.
Then, since you're always doing the same substring function, save the info into a variable and compare to the variable instead of calling the substring function 9 times for each loop.
And finally, don't string together "if" statements unless it's possible they could all be correct. For example:
if(myBanana.color === "Yellow")
{
myBanana.eat();
}
if(myBanana.color === "Green")
{
myBanana.wait();
}
in this example, the banana cannot be yellow AND green. so the second statement should be an "else if". That way, if myBanana is yellow, we don't have to waste time or resources checking to see whether it's green.
Anywho, try changing your loop(s) to look something like this. Try to minimize the amount of things you have to process.
var targetLayer = app.activeDocument.layers["Media Info"];
var len = targetLayer.pageItems.length;
var curItem, mySubstring;
for(var xx=0;xx<len;xx++)
{
curItem = targetLayer.pageItems[xx]
if(curItem.typename !== "TextFrame")
{
continue;
}
mySubstring = curItem.contents.substring(0,2);
if(mySubstring === "4 ")
{
//do something
}
else if(mySubstring === "83")
{
//do something
}
// .... etc
else
{
//do something if none of those condtions were correct
}
}
Copy link to clipboard
Copied
williamadowling Ok I gotcha... that helps a lot. I appreciate the feedback you gave. So instead of searching through all the pageItems you said to target a specific layer FIRST....then search that layers pageItems? So I see in the api book you can get a layer by name.
So using this code how would I get the layer first then only the pageItems on that layer?
for (var i = 0; i < documents.length; i++) {
var currentActiveDoc = app.documents;
app.activeDocument = currentActiveDoc;
var allPageItems = app.activeDocument.pageItems;
var theDocVariables = currentActiveDoc.variables;
var allLayers = currentActiveDoc.layers;
if (attachmentDescription.text != "") {
for (var xx = 0; xx < allPageItems.length; xx++) {
if (allPageItems[xx].layer == "[Layer Media Info]" && allPageItems[xx].typename == "TextFrame") {
var checkString = allPageItems[xx].contents.substring(0, 2);
if (checkString == "42") {
//alert("I see you are using a 42 page template");
var useThisX = 3357.07;
var useThisY = -777.72;
}
}
}
}
}
Copy link to clipboard
Copied
If you take a look at the snippet that i included in my previous post, i show how you save the desired layer (Media Info) into a variable which i called "targetLayer".
Once you have the layer saved, you can then access the array(s) of children. So if you wanted to get all the textFrames of a given layer, you'd do so like this:
targetLayer.textFrames
you can do the same with pageitems, pathItems, compoundPathItems, groupItems etc.
So you can get access to different child elements in a couple different ways. You can either access them by index number, or by name. So if you're looking for the first element (remember that javascript arrays are zero based index) you'll access it with the number 0, like so:
var myTextFrame = targetLayer.textFrames[0];
Or you can also access child elements by name (if they are in fact named), like so:
var myTextFrame = targetLayer.textframes["Name Of Text Frame"];
or
var myTextFrame = targetLayer.textFrames.getByName("Name Of Text Frame");
these two examples should yield the same result, so i prefer the former since it's shorter.
Copy link to clipboard
Copied
Hi subieguy2,
there are many problems in your code. (Also see the notes of williamadowling)
And I did not looking to deep in your (much-to-long-for-5-minutes) code.
Some questions:
Where the values for position of the text frames comes from (line 575 and following) ?
Why there are so little differences in the positions?
Do you really have 30 pages and 42 page templates?
All these values are a little bit strange.
Try this example code with your values and see the result:
// textFrames_addTextFrames_ContentsAndPositionsFromArray.jsx
// regards pixxxel schubser
var doc = app.documents.add(DocumentColorSpace.RGB, new UnitValue ("3400", "px"), new UnitValue ("840", "px"));
//var doc = app.activeDocument;
var arr = [
[["42"], [3357.07], [-777.72]], /*are U sure? 42 pages?*/
[["36"], [2783.25], [-777.74]],
[["30"], [1603.30], [-772.98]], /*are U sure? 30 pages?*/
[["24"], [1210.17], [-781.56]],
[["20"], [1614.26], [-777.86]],
[["16"], [1222.85], [-777.83]],
[["12"], [628.89], [-777.32]],
[["8 "], [84.17], [-817.30]],
[["4 "], [652.12], [-739.50]]
];
for (i=0; i<arr.length; i++) {
makeText ( arr[0], arr[1], arr[2] );
}
function makeText (aCon, posX, posY) {
var textRef1 = doc.textFrames.pointText([posX*1, posY*-1]);
textRef1.contents = aCon.toString();
var Rect = doc.pathItems.rectangle(textRef1.top, textRef1.left, textRef1.width, textRef1.height);
Rect.stroked = true;
Rect.filled = false;
Rect.move(textRef1, ElementPlacement.PLACEAFTER);
}
I also see problems if you use some ifs in a for in a function in a for in a for in a while in a try clause in a function
(line 188 and following)
And so on …
Copy link to clipboard
Copied
pixxxel schubser Thank you for your help. Much appreciated. The values of the text positions are hard coded based on the placement I am required to have the users input. Same thing with the positioning of them. Yes they are meant to be that close. Again a requirement of my publisher. I do have that many page sizes. However it is for a large printed document that is folded. That is how the page count is determined. There are many different template sizes that we would run this script on so that is why all those options are available.
I will try the code you showed. Any suggestions for cleaning up the try catch for if for if for if for if for if
The while was just to make the progress bar work. I took the feedback williamadowling gave me and made the else if adjustments.
Again I very much appreciate your input. Helps me learn!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now