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, ""); // https://regexr.com/ // 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!
... View more