12 Replies Latest reply on Mar 4, 2015 12:41 PM by Silly-V

    Script errors and a question

    Zantcor
      //Created by Daryl R. Smith
      //This is the tab for Totes
      
      #target illustrator
      
      var thisDoc = app.activeDocument;
      
      if (thisDoc.selection.length > 0)
      {
      
      var w = new Window ("dialog", "Choose Totes Template", undefined, {closeButton: false});
      w.alignChildren = "right";
      
                    w.onShow = function()
                      {  
      
                          for(var i=0; i<totes1.children.length; i++)
                  {  
                      var thisPanel2 = totes1.children[i];  
                      thisPanel2.originalLocation = [thisPanel2.location[0], thisPanel2.location[1]];  
                  }//end for
              
              }//end on show function
          
      var tpanel = w.add ("tabbedpanel");
      tpanel.size = [400, 520]; 
      
      
      //**********************************************************************************************************************************************************
      //******************************************************Section to setup the Totes Tab******************************************************************
      //**********************************************************************************************************************************************************
      
      //creates the Totes Tab
      var totesTab = tpanel.add ("tab", undefined, "Available Totes");
      totesTab.orientation = "column";
      
      //Creates first Row Group for Totes, contains panels 1,2 and 3
      var TotesGroup = totesTab.add("group",undefined);
              TotesGroup.orientation = "row";
              TotesGroup.alignChildren = "left";
      
      //first Totes Panel
      var totes1 = TotesGroup.add ("panel", undefined, "");
      totes1.alignChildren = "left";
      totes1.size = [300,480];
      
      
      var docSelection = app.activeDocument.selection;
      var newGroup = app.activeDocument.groupItems.add();
      
      for ( i = 0; i < docSelection.length; i++ ) {
         var newItem = docSelection[i] ();
      newItem.moveToEnd( newGroup );
      }
          
         alert (thisDoc.selection.length)
      
      GetToteTempNames()
      
      function GetToteTempNames()
      {  
          
          //Replace this address for the "toteTempPath" to the folder that has all the templates it in it, then the for loop will place them all
          //into a radio button array for the user to choose which one they want.  This updates everytime that this script is ran so if
          //one if removed or added it will show up reducing the amount of required time needed to update the script.
          //when replaced make sure to change the \ marks to / marks or it will not work.
          
        var toteTempPath = Folder ("Z:/  Vitronic Master Templates/STE2 Templates/TOTES & BAGS");   
        var toteTempFiles = toteTempPath.getFiles();  
        var TotenamesArr = [];  
        for(var i=0; i<toteTempFiles.length; i++){  
        var thisToteFile = toteTempFiles[i];  
        TotenamesArr.push(thisToteFile.displayName); 
        }  
      
        return TotenamesArr;  
      }  
      var totesarr1 = GetToteTempNames();
      
              for(var i=0; i<totesarr1.length; i++){  
                  var lbl = totes1.add("radiobutton", undefined, totesarr1[i]);  
              }  
          
              var scrl = TotesGroup.add('scrollbar'); scrl.size = [20, 480];  
                
              scrl.onChange = scrl.onChanging = function()
              {  //start function
                  for(var i=0; i<totes1.children.length; i++)
                  {  //start for
                      var thisPanel2 = totes1.children[i];  
                      var xLoc = thisPanel2.originalLocation[0];  
                      var yLoc = thisPanel2.originalLocation[1];  
                      thisPanel2.location=[xLoc, yLoc-((this.value/100) * ((totes1.children.length*27.5) - (scrl.size[1]+5)))];  
                      // These numbers are my arbitrary way of setting the y location  
                      }//end for loop
                  }//end function
              
             totes1.children[0].value = true;
              
              var bottombuttons = w.add ("group");
              bottombuttons.orientation = "row"
              
              var rsize = bottombuttons.add ("panel", undefined, "Resize Artwork?");
      rsize.alignChildren = "left"
      rsize.orientation = "column"
      
      rsizeyes = rsize.add ("radiobutton", undefined, "Yes");
      rsizeno  = rsize.add ("radiobutton", undefined, "No");
      
      rsize.children[1].value = true;
      
      
      var kbottombuttons = bottombuttons.add ("group", undefined, "")
      kbottombuttons.orientation = "column"
      kbottombuttons.add ("button", undefined, "Ok");
              
      
       //**********************************************************************************************************************************************************
      //********************This section is for the functions to output your choice then run the function depending on the choice you made****************
      //**********************************************************************************************************************************************************
      
      if (w.show () == 1)
      {
          
      var templateselected = selected_rbutton
      
      var requestedtemplate = selected_rbutton (totalgroups)
      
      var confirmed = confirm ("You picked [ " + selected_rbutton (totalgroups) +" ]"+ "\nYou picked [ " + questionResize (rsizechoice) + " ] to resize" + "\n" +"\nContinue?");
              confirm.noAsDflt == false;
              
              if (confirmed == true)
              {
                  
                  
                  copy();
                  openTemp ();
                  paste();
                  centerArt ();
                  resizeart ();
                  savefile ()
                 //need to wait for the save script to finish before it does the put name so that if the user changes the file name from the save pallet it will be reflected
                 //in the putname function.
                 
                 wait = savefile();
      while(!wait) {
          //wait = false; // this is just killing time  
          // so is this
          a++;     
            
          if (a>1000) break; // guard against infinite loop  
      }  //end wait
                 
                  putname ();
                  
               } /*end if */ else
           {
               alert ("You chose not to continue, script stopped")
               }//end else
      }//end if OK
      
      
      //**********************************************************************************************************************************************************
      //*********This section is for the function to check which option for resize gets pressed and returns the text to a variable for output******************
      //**********************************************************************************************************************************************************
      
      var rsizechoice = [rsize]
      
      function questionResize (rsizechoice)
      {//start function
      //when choice is pressed return choice text for variable output    
      for (var i = 0; i < rsize.children.length; i++)
      {
      if (rsize.children[i].value == true)
      return rsize.children[i].text;
      }//end for
      }//end function QuestionResize
      
      
      //**********************************************************************************************************************************************************
      //***************************************************************This is the section that will center the artwork ******************************************
      //**********************************************************************************************************************************************************
      
           var thisDoc = app.activeDocument;
          thisDoc.rulerOrigin = [0,0];  //  Sets the coordinates of the artboard to the lower left hand corner of the document.
          thisDoc.pageOrigin = [0,0];  //  This makes sure a valid reference point is used for different sized documents.
          
          function centerArt()
         {   
             var thisdoc = app.activeDocument;  
              var selecteditem = app.activeDocument.selection;
              var hasDocCoords = app.coordinateSystem == CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
              
              
              var p = thisdoc.pageItems;  
              for (var i = 0, l = p.length; i < l; i++) {  
                  var pID = p[i];  
                  if (pID.guides == true) {  
                      var guideBox = pID  
                  }  
              }   
              var wNum = guideBox.width;  
              var hNum = guideBox.height;
              var swNum = selecteditem.width;
              var shNum = selecteditem.height;
              
              var sxOffset = (swNum/2);
              var syOffset = (shNum/2);
              var xOffset = (wNum/2);
              var yOffset = (hNum/2);
              
              
              var xNum = guideBox.position[0]+xOffset;  
              var yNum = guideBox.position[1]-yOffset; 
              
              var guideXCenter = xNum-sxOffset;
              var guideYCenter = yNum+syOffset;
              
              var sxNum = selecteditem.position[0]+sxOffset;
              var syNum = selecteditem.position[1]-syOffset;
              
              //to test your script for position un annotate the two alerts below
              
                     alert("GuideBox Data:\nWidth: "+wNum+"\nHeight: "+hNum+"\nX Position: "+xNum+"\nY Position: "+yNum); 
                      alert ("Selected object Data:\nWidth: "+swNum+"\nHeight: "+shNum+"\nX Position: "+sxNum+"\nY Position: "+syNum);
                     
                     
              //changes the position of the selected artwork to the center of the guide box.
              app.activeDocument.selection[0].position = [guideXCenter, guideYCenter]    
          }//end centerart
      
         function resizeart()
      {
         if (questionResize (rsizechoice) == "Yes")
         {
              for (var i = 0; i < app.activeDocument.pageItems.length; i++) 
              {  
                    
                  if (app.activeDocument.pageItems[i].guides == true) {  
                      var guideBoxSize = app.activeDocument.pageItems[i];  
                  }  //end if
              }   //end for
              var guideW = guideBoxSize.width;  
              var guideH = guideBoxSize.height;
              
              var itemW = app.activeDocument.selection.width;
              var itemH = app.activeDocument.selection.height;
              
               //to test your script for resize un annotate the two alerts below
               
              alert ("guide is this tall " + guideH + "\nguide is this wide " + guideW);
              alert ("item is this tall " + itemH + "\nitem is this wide " + itemW);
              
              var artWidth = itemW;
              var artHeight =itemH;           
      
          if (artWidth > artHeight)
              {
                 var a = (guideW/artWidth)*100;
                 app.activeDocument.selection[0].resize(a,a);
               }   
          if (artHeight > artWidth)     
              {
                 var b = (guideH/artHeight)*100;
                 app.activeDocument.selection[0].resize(b,b);
               }
           
       var artWidth2 = itemW;
      var artHeight2 =itemH;    
      
       if (artHeight2 > guideH)
          {
             var c = (guideH/artHeight2)*100;
            app.activeDocument.selection[0].resize(c,c);
            }
              
      
           }//end if doresize
      }//end function resizeart
      
      function putname ()
      {
        
        var thisDoc = app.activeDocument;
              thisDoc.rulerOrigin = [0,0];
              thisDoc.pageOrigin = [0,0];
        
        var filename = app.activeDocument.name.replace(/\.[^\.]+$/, '');
        var createdate = app.activeDocument.modified;
        
        var  theDate = new Date();
              var day = theDate.getDate();
              var month = theDate.getMonth() + 1;
              var year = theDate.getFullYear();
              var hours = theDate.getHours();
              var min = theDate.getMinutes();
                  if (min < 10)
                      min = ("0" + min);
              var morn;
                  if (hours >= 12)
                      {
                          hours = hours-12;
                          morn = " PM";
                      }
                  else
                      morn = " AM"
      
      var saveDate = (month + "-" + day + "-" + year + "    " + hours + ":" + min + morn );
        
        
        
      
        var nameTextFrame = thisDoc.textFrames.add();
          nameTextFrame.contents = globalartistname + "\n" + filename + "\n" + saveDate;  
          nameTextFrame.top = 1000;
          nameTextFrame.left = 100;
          nameTextFrame.filled = true; 
          nameTextFrame.alignment = "center";
          
          }//end put name
      
      function savefile ()
      {
          
          //change this folder location for the "savescript" variable to the one where the location of the save script is located on the server, 
          //This save script is for the pallet that will pop up and list the different save options, not the individual save scripts themselves.
          
              var savescript =  new File("S:/Art/ArtDept/Illustrator JS Palette/Scripts/Art Share Server/Save/Save Option Pallet");
                  savescript.open("r");      
             var bt = new BridgeTalk;
                  bt.target = "illustrator";          
             var script = savescript.read();
                  savescript.close();               
             bt.body = script;
             bt.send();  
             
             if (saved = true)
             {
                 alert("File saved")
              }
             
             if (saved == false)
             {
                 alert ("File not saved")
                 }
             
             return true;
       }      
      
      
      //**********************************************************************************************************************************************************
      //********************This section is for the functions to check which button gets pressed and returns the text to a variable for output ****************
      //**********************************************************************************************************************************************************
      
      
      //all radiobutton groups must be pressent in this array
      var totalgroups = [totes1]
      
      //this function checks each group for the 1 radio button is clicked then returns its text
      function selected_rbutton (totalgroups)
      {//start function to check buttons
      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Totes tab buttons
      
      //when totes1 is pressed return text for output
      for (var i = 0; i < totes1.children.length; i++)
      {
      if (totes1.children[i].value == true)
      {
      return totes1.children[i].text;
      }//end if
      
      }//end if
      }//end for
      
      //////////////////////////////////////////////////////////////////////////open template 
      
      function openTemp()
       {
           //change the folder location of the templates here but leave the last section that is black, this will allow the script to open which
           //ever template they chose from the radiobuttons.  This location should be the same as the toteTempPath above but reduces the
           //chance for something to break by pulling the address here again instead of from the variable near the begining of the script.
           
       var gettotetemp = new File("Z:/  Vitronic Master Templates/STE2 Templates/TOTES & BAGS/" + selected_rbutton (totalgroups));
      
      open(gettotetemp);
      
      }//end open temp function
      
      }//end if selection check
      
      
      else
      {
          alert("Please select the artwork.");
        } // end else selection check 
      

       

      That is my entire script with notes to help, my script will not run it keeps saying there is an error on a different line everytime I run it and the line that it says the error is on is something simple like .alignChildren = "left"; I'm at a loss as to why it stopped working.

       

      My question is this, after I get it going again I made a mistake when I wrote this because I was testing it with a single path item to make sure that it moved to the template, resized and saved but when I try it with a bunch of different objects it breaks even if try and group them all together first.  It won't copy() the entire selection so I added in a section to the script near line 50 that takes all the path items of the selection and puts them into a group.  I've been going over this and over this and running it and testing it since I got this done and I can't get it to work, I'm at my wits end and looking for help.

       

      All I need it to do is the following in the correct order;

      copy();

      openTemp ();

      paste();

      centerArt ();

      resizeart ();

      savefile ()

      putname ();

       

      separately each function works correctly but when I try and put them all together something breaks.  I'll post the script that I'm using for the savefile() in the next post.

        • 1. Re: Script errors and a question
          Zantcor Level 1

          This is the section for the save script;  I've removed the names for the desktop section along with the location of their desktops

           

          //created by Daryl Smith
          //opens a window that shows the different save options with radio buttons for save as is or save with new name
          //and it has two check boxes so that the user can choose to save a copy to their desktop and/or a copy to the print folder
          //for the direct to screen printer.
          
          
          #target illustrator
          
          var savewindow = new Window ("dialog", "Select Save Options");
          var saveradio_group = savewindow.add ("panel",undefined, "Select Save Method");
          saveradio_group.alignChildren = "left";
          saveradio_group.size = [260,80];
          
          var saveold = saveradio_group.add ("radiobutton", undefined, "Save with Current Name");
          var savenew = saveradio_group.add ("radiobutton", undefined, "Save with New Name");
          
          var extraSaveOptions = savewindow.add ("panel",undefined, "Save Extra Copy");
          extraSaveOptions.alignChildren = "left";
          extraSaveOptions.size = [260,80];
          
          var checkbox1 = extraSaveOptions.add ("checkbox", undefined, "Save extra copy as .eps to STE_Art folder");
          var checkbox2 = extraSaveOptions.add ("checkbox", undefined, "Save extra copy as .pdf to my desktop");
          
          saveradio_group.children[0].value = true;
          
          savewindow.add ("button", undefined, "OK");
          
          var saved = false;
          
          function selected_rbutton (saveradio_group)
          {
          for (var i = 0; i < saveradio_group.children.length; i++)
          if (saveradio_group.children[i].value == true)
          return saveradio_group.children[i].text;
          }
          
          
          if (savewindow.show () == 1)
              var selBut = selected_rbutton (saveradio_group);
             
              if (checkbox1.value == true)
          {
               var eso1 = "yes";
            } 
          
          if (checkbox1.value == false)
          {
              var eso1 = "no";
              }
          
          if (checkbox2.value == true)
          {
               var eso2 = "yes";
            }     
          
          if (checkbox2.value == false)
          {
              var eso2 = "no";
              }
             
             var confirmed =  confirm ("Save option is [ " + selBut + " ]" + "\nYou chose [ " + eso1 + " ] to save a .eps copy to the new printer folder" + "\nYou chose [ " + eso2 + " ] to save a .pdf to your desktop" + "\n" + "\nContinue?")
                     confirm.noAsDflt == false;
          
          if (confirmed == true)
          {
              //alert ("confirmed is true" +"\n" + saveold.value + "\n" + savenew.value)
             
              if (saveold.value == true)
              {
                   var saved = true;
              var saveOldname =  new File("S:/Art/ArtDept/Illustrator JS Palette/Scripts/Art Share Server/Save/Art Share.jsx");
                      saveOldname.open("r");     
                 var bt = new BridgeTalk;
                      bt.target = "illustrator";         
                 var script = saveOldname.read();
                      saveOldname.close();              
                 bt.body = script;
                 bt.send(); 
                
                 saveFileToPDF();
                 saveFileToEPS();
                
                 }//end save old
                
                if (savenew.value == true)
                {
                    var saved = true;
                  var saveNewname =  new File("S:/Art/ArtDept/Illustrator JS Palette/Scripts/Art Share Server/Save/File Save.jsx");
                      saveNewname.open("r");     
                  var bt = new BridgeTalk;
                      bt.target = "illustrator";         
                 var script = saveNewname.read();
                      saveNewname.close();              
                 bt.body = script;
                 bt.send(); 
                    
                 saveFileToPDF();
                 saveFileToEPS();
                
              }//end save new
          
          }//end if confirmed check
          
          
                   
                   function saveFileToEPS() //  saves the file to the server as a .EPS document.
                            { 
                              var thisDoc = app.activeDocument;
                              if ( app.documents.length > 0 )
                                 {
                                    
                                           if (checkbox1.value == true)
                                                  {             
                                                       alert ("Your said [ " + checkbox1.value + " ] to save extra copy as .eps to STE_Art folder")
                                 
                                     var saveName = new File ("S:/Art/ArtDept/STE2/STE2_ArtBags");
                                    saveOpts = new EPSSaveOptions();
                                    saveOpts.compatibility = Compatibility.ILLUSTRATOR13;
                                    saveOpts.generateThumbnails = true;
                                    saveOpts.preserveEditability = true;
                 
                                    saveOpts.artBoardClipping = true;
                                    saveOpts.saveMultipleArtboards = true; 
          
                                    saveOpts.artboardRange = "1"; // artboard # 1 
                                    thisDoc.saveAs( saveName, saveOpts );
                       }//end if
                   }//end function savepdf
          }//end if checkbox1
          
             
                     
                  function saveFileToPDF () //  saves the file to the server as a .PDF document.
                    { 
                          if (checkbox2.value == true)
                             {
                               //alert ("Your said [ " + checkbox2.value + " ] to save extra copy as .pdf to my desktop" + globalartistname)
                              
                               var globalartistname = "drs";
                       
                                 var thisDoc = app.activeDocument;
                 
                  if ( app.documents.length > 0 )
                       {
                           var filename = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                          
                          
                           switch (globalartistname)
                           {
                     
          THIS IS THE AREA WHERE THE NAMES AND LOCATIONS OF THE USERS DESKTOPS WOULD GO, GLOBAL ARTIST IS A VARIABLE THAT IS ENTERED WHEN ILLUSTRATOR
          FIRST OPENS AND IS USED BY A LOT OF THE OTHER SCRIPTS.
          
                          
                          
                          
                  saveOpts = new PDFSaveOptions();
                  saveOpts.compatibility = PDFCompatibility.ACROBAT6;
                  saveOpts.generateThumbnails = true;
                  saveOpts.preserveEditability = true;
                  thisDoc.saveAs( saveName, saveOpts );
          
          
                                 }//end if
                          }//end function
             }//end if checkbox2
             
          
          

           

           

          The two scripts for the save old name and save new name are exactly what it sounds like, one saves it with its current name and the other has a prompt for the name to be entered.

          • 2. Re: Script errors and a question
            Zantcor Level 1

            I fixed the code on lines 260, 265, and 274 on the main script not the save script from app.activeDocument.selection[0] to app.activeDocument.selection and it still will only work with the bottom artwork not the top. The top one is all 5 boxes and the bottom one is one object only. All objects are red with a black stroke on them to show that they are a single object.


            1555555 - TESTARTTESTARTTESTARTTESTARTTESTARTTESTARTTESTARTTESTART.png

             

            I'm not sure what I'm doing wrong but it is not working if there are multiple items in my selection even if they are all in a group now.

            • 3. Re: Script errors and a question
              W_J_T Level 4

              Zantcor wrote:


              All I need it to do is the following in the correct order;

               

              copy();

              openTemp ();

              paste();

              centerArt ();

              resizeart ();

              savefile ()

              putname ();

               

              separately each function works correctly but when I try and put them all together something breaks.  I'll post the script that I'm using for the savefile() in the next post.

               

              [Question:]

              I saw your other thread. Is this still the need? If so I can post how I would suggest approaching it.

               

              ------

               

              But it seems the issues expand past that further perhaps?

              • 4. Re: Script errors and a question
                Zantcor Level 1

                Right now my two problems I have is that when I try and use the app.activedocument.selection it only works if the selection is one path item but if there are multiple path items in my selection then it breaks down at the copy() and centerart() part of the code.  I've got a section in there near line 50 that takes each path item of the selection and groups them together but it still breaks the code.

                 

                I'll take a look at your suggestion if you want to post it as anything that may work better is always a better choice.

                • 5. Re: Script errors and a question
                  W_J_T Level 4

                  Zantcor wrote:

                   

                  Right now my two problems I have is that when I try and use the app.activedocument.selection it only works if the selection is one path item but if there are multiple path items in my selection then it breaks down at the copy() and centerart() part of the code.  I've got a section in there near line 50 that takes each path item of the selection and groups them together but it still breaks the code.

                   

                  I had a quick glance at you code. You mentioned above that initially you write it for a single item then refactored it for multiple items. I see your group code, but in many other places it references ".selection[0]" (a single item). Again I only glanced quickly but did you refactor "all" the code references from the singular way you set it up originally?

                   

                  Between those two code posts is that the entire code if I wish to find time (if no one else does n the meantime) and try it?

                   

                  Zantcor wrote:

                   

                  I'll take a look at your suggestion if you want to post it as anything that may work better is always a better choice.

                   

                  I will post my suggestion in the other thread then, as it's more applicable there given the subject matter.

                  • 6. Re: Script errors and a question
                    Zantcor Level 1

                    Go ahead and try it, you will need to make a folder that has 2-3 blank artboards with a square guide box in it for the center art to locate and change the two locations to point to that folder.  I'm going to be going through the entire line of code 1 line at a time to double check my syntax and see if I can find out what is going on.

                    • 7. Re: Script errors and a question
                      CarlosCanto Adobe Community Professional & MVP

                      what does the paste() function do? paste selection?

                       

                      it is missing from your code.

                      • 8. Re: Script errors and a question
                        CarlosCanto Adobe Community Professional & MVP

                        most likely, the problem is copy() and paste(), paste is not reliable, instead you can duplicate the object onto a target document, so first, when you open your template, return a reference to it, so you can use it to move the duplicate Group to.

                         

                        see my comments with @@@@@@@@@, I didn't use savefile() or putname(), I just focused on resize() and center()

                         

                        //Created by Daryl R. Smith 
                        //This is the tab for Totes 
                        
                        #target illustrator 
                        
                        var thisDoc = app.activeDocument; 
                        
                        if (thisDoc.selection.length > 0) 
                        { 
                        
                        var w = new Window ("dialog", "Choose Totes Template", undefined, {closeButton: false}); 
                        w.alignChildren = "right"; 
                        
                                      w.onShow = function() 
                                        {   
                        
                                            for(var i=0; i<totes1.children.length; i++) 
                                    {   
                                        var thisPanel2 = totes1.children[i];   
                                        thisPanel2.originalLocation = [thisPanel2.location[0], thisPanel2.location[1]];   
                                    }//end for 
                                 
                                }//end on show function 
                             
                        var tpanel = w.add ("tabbedpanel"); 
                        tpanel.size = [400, 520]; 
                        
                        
                        //********************************************************************************************************************************************************** 
                        //******************************************************Section to setup the Totes Tab****************************************************************** 
                        //********************************************************************************************************************************************************** 
                        
                        //creates the Totes Tab 
                        var totesTab = tpanel.add ("tab", undefined, "Available Totes"); 
                        totesTab.orientation = "column"; 
                        
                        //Creates first Row Group for Totes, contains panels 1,2 and 3 
                        var TotesGroup = totesTab.add("group",undefined); 
                                TotesGroup.orientation = "row"; 
                                TotesGroup.alignChildren = "left"; 
                        
                        //first Totes Panel 
                        var totes1 = TotesGroup.add ("panel", undefined, ""); 
                        totes1.alignChildren = "left"; 
                        totes1.size = [300,480]; 
                        
                        
                        var docSelection = app.activeDocument.selection; 
                        var newGroup = app.activeDocument.groupItems.add(); 
                        
                        for ( i = 0; i < docSelection.length; i++ ) { 
                          //var newItem = docSelection[i] ();  // [error] pathItem() is not a function @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                          var newItem = docSelection[i]; 
                        newItem.moveToEnd( newGroup ); 
                        } 
                             
                          //alert (thisDoc.selection.length) 
                        
                        //GetToteTempNames()  // you don't need this one    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        
                        function GetToteTempNames() 
                        {   
                             
                            //Replace this address for the "toteTempPath" to the folder that has all the templates it in it, then the for loop will place them all 
                            //into a radio button array for the user to choose which one they want.  This updates everytime that this script is ran so if 
                            //one if removed or added it will show up reducing the amount of required time needed to update the script. 
                            //when replaced make sure to change the \ marks to / marks or it will not work. 
                             
                          var toteTempPath = Folder ("Z:/  Vitronic Master Templates/STE2 Templates/TOTES & BAGS"); 
                          //var toteTempPath = Folder ("c:/temp/templates"); 
                          var toteTempFiles = toteTempPath.getFiles();   
                          var TotenamesArr = [];   
                          for(var i=0; i<toteTempFiles.length; i++){   
                          var thisToteFile = toteTempFiles[i];   
                          TotenamesArr.push(thisToteFile.displayName); 
                          }   
                        
                          return TotenamesArr;   
                        }   
                        var totesarr1 = GetToteTempNames(); 
                        
                                for(var i=0; i<totesarr1.length; i++){   
                                    var lbl = totes1.add("radiobutton", undefined, totesarr1[i]);   
                                }   
                             
                                var scrl = TotesGroup.add('scrollbar'); scrl.size = [20, 480];   
                                   
                                scrl.onChange = scrl.onChanging = function() 
                                {  //start function 
                                    for(var i=0; i<totes1.children.length; i++) 
                                    {  //start for 
                                        var thisPanel2 = totes1.children[i];   
                                        var xLoc = thisPanel2.originalLocation[0];   
                                        var yLoc = thisPanel2.originalLocation[1];   
                                        thisPanel2.location=[xLoc, yLoc-((this.value/100) * ((totes1.children.length*27.5) - (scrl.size[1]+5)))];   
                                        // These numbers are my arbitrary way of setting the y location   
                                        }//end for loop 
                                    }//end function 
                                 
                              totes1.children[0].value = true;  // [error] undefined is not an object...until I added a reference to my own templates @@@@@@@@@@@@@@@
                                 
                                var bottombuttons = w.add ("group"); 
                                bottombuttons.orientation = "row" 
                                 
                                var rsize = bottombuttons.add ("panel", undefined, "Resize Artwork?"); 
                        rsize.alignChildren = "left" 
                        rsize.orientation = "column" 
                        
                        rsizeyes = rsize.add ("radiobutton", undefined, "Yes"); 
                        rsizeno  = rsize.add ("radiobutton", undefined, "No"); 
                        
                        rsize.children[1].value = true; 
                        
                        
                        var kbottombuttons = bottombuttons.add ("group", undefined, "") 
                        kbottombuttons.orientation = "column" 
                        kbottombuttons.add ("button", undefined, "Ok"); 
                                 
                        
                        //********************************************************************************************************************************************************** 
                        //********************This section is for the functions to output your choice then run the function depending on the choice you made**************** 
                        //********************************************************************************************************************************************************** 
                        
                        if (w.show () == 1) 
                        { 
                             
                        var templateselected = selected_rbutton 
                        
                        var requestedtemplate = selected_rbutton (totalgroups) 
                        
                        var confirmed = confirm ("You picked [ " + selected_rbutton (totalgroups) +" ]"+ "\nYou picked [ " + questionResize (rsizechoice) + " ] to resize" + "\n" +"\nContinue?"); 
                                confirm.noAsDflt == false; 
                                 
                                if (confirmed == true) 
                                { 
                                     
                                     
                                    //copy();  // copy and paste is unreliable, duplicate the item instead, targeting the template document @@@@@@@@@@@@@@@@@@
                                  var temp = openTemp ();  // open your template and get a reference to it @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                  newGroup.duplicate(temp, ElementPlacement.PLACEATEND); // duplicate group @@@@@@@@@@@@@@@@@@@@@@
                                    //paste();  // copy and paste is unreliable, duplicate the item instead, targeting the template document @@@@@@@@@@@@@@@@@
                                    centerArt(); 
                                    resizeart (); 
                                    //savefile (); // commented out for testing purposes only @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                  //need to wait for the save script to finish before it does the put name so that if the user changes the file name from the save pallet it will be reflected 
                                  //in the putname function. 
                                   
                                  wait = savefile(); 
                        while(!wait) { 
                            //wait = false; // this is just killing time   
                            // so is this 
                            a++;     
                               
                            if (a>1000) break; // guard against infinite loop   
                        }  //end wait 
                                   
                                    //putname ();  // commented out for testing purposes only @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                     
                                } /*end if */ else 
                            { 
                                alert ("You chose not to continue, script stopped") 
                                }//end else 
                        }//end if OK 
                        
                        
                        //********************************************************************************************************************************************************** 
                        //*********This section is for the function to check which option for resize gets pressed and returns the text to a variable for output****************** 
                        //********************************************************************************************************************************************************** 
                        
                        var rsizechoice = [rsize] 
                        
                        function questionResize (rsizechoice) 
                        {//start function 
                        //when choice is pressed return choice text for variable output     
                        for (var i = 0; i < rsize.children.length; i++) 
                        { 
                        if (rsize.children[i].value == true) 
                        return rsize.children[i].text; 
                        }//end for 
                        }//end function QuestionResize 
                        
                        
                        //********************************************************************************************************************************************************** 
                        //***************************************************************This is the section that will center the artwork ****************************************** 
                        //********************************************************************************************************************************************************** 
                        
                            var thisDoc = app.activeDocument; 
                            thisDoc.rulerOrigin = [0,0];  //  Sets the coordinates of the artboard to the lower left hand corner of the document. 
                            thisDoc.pageOrigin = [0,0];  //  This makes sure a valid reference point is used for different sized documents. 
                             
                            function centerArt() 
                          {   
                              var thisdoc = app.activeDocument;   
                                //var selecteditem = app.activeDocument.selection; // [error] this throws no error here, but it does down below, selection returns an Array @@@@@@
                                var selecteditem = app.activeDocument.groupItems[0]; // since you have duplicated a group, just get a reference to it @@@@@@@@@@@@
                                selecteditem.selected = true; // you don't have to select items to work with them, I just selected to continue Debugging your script @@@@@@@@
                                var hasDocCoords = app.coordinateSystem == CoordinateSystem.ARTBOARDCOORDINATESYSTEM; 
                                 
                                 
                                var p = thisdoc.pageItems;   
                                for (var i = 0, l = p.length; i < l; i++) {   
                                    var pID = p[i];   
                                    if (pID.guides == true) {   
                                        var guideBox = pID   
                                    }   
                                }   
                                var wNum = guideBox.width;   
                                var hNum = guideBox.height; 
                                var swNum = selecteditem.width; 
                                var shNum = selecteditem.height; 
                                 
                                var sxOffset = (swNum/2); 
                                var syOffset = (shNum/2); 
                                var xOffset = (wNum/2); 
                                var yOffset = (hNum/2); 
                                 
                                 
                                var xNum = guideBox.position[0]+xOffset;   
                                var yNum = guideBox.position[1]-yOffset; 
                                 
                                var guideXCenter = xNum-sxOffset; 
                                var guideYCenter = yNum+syOffset; 
                                 
                                var sxNum = selecteditem.position[0]+sxOffset; 
                                var syNum = selecteditem.position[1]-syOffset; 
                                 
                                //to test your script for position un annotate the two alerts below 
                                 
                                      alert("GuideBox Data:\nWidth: "+wNum+"\nHeight: "+hNum+"\nX Position: "+xNum+"\nY Position: "+yNum); 
                                        alert ("Selected object Data:\nWidth: "+swNum+"\nHeight: "+shNum+"\nX Position: "+sxNum+"\nY Position: "+syNum); 
                                       
                                       
                                //changes the position of the selected artwork to the center of the guide box. 
                                //app.activeDocument.selection[0].position = [guideXCenter, guideYCenter]; // use the group reference instead @@@@@@@@@@@@@@@@@
                                selecteditem.position = [guideXCenter, guideYCenter]; // selecteditem is the duplicated group @@@@@@@@@@@@@@@@@@@@@@
                            }//end centerart 
                        
                          function resizeart() 
                        { 
                          if (questionResize (rsizechoice) == "Yes") 
                          { 
                                for (var i = 0; i < app.activeDocument.pageItems.length; i++) 
                                {   
                                       
                                    if (app.activeDocument.pageItems[i].guides == true) {   
                                        var guideBoxSize = app.activeDocument.pageItems[i];   
                                    }  //end if 
                                }  //end for 
                                var guideW = guideBoxSize.width;   
                                var guideH = guideBoxSize.height; 
                                 
                                //var itemW = app.activeDocument.selection.width;  // selection returns an array @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                //var itemH = app.activeDocument.selection.height;  // same as above @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                                var itemW = app.activeDocument.selection[0].width; 
                                var itemH = app.activeDocument.selection[0].height; 
                               
                                //to test your script for resize un annotate the two alerts below 
                                 
                                alert ("guide is this tall " + guideH + "\nguide is this wide " + guideW); 
                                alert ("item is this tall " + itemH + "\nitem is this wide " + itemW); 
                                 
                                var artWidth = itemW; 
                                var artHeight =itemH;           
                        
                            if (artWidth > artHeight) 
                                { 
                                  var a = (guideW/artWidth)*100; 
                                  app.activeDocument.selection[0].resize(a,a); 
                                }   
                            if (artHeight > artWidth)     
                                { 
                                  var b = (guideH/artHeight)*100; 
                                  app.activeDocument.selection[0].resize(b,b); 
                                } 
                             
                        var artWidth2 = itemW; 
                        var artHeight2 =itemH;     
                        
                        if (artHeight2 > guideH) 
                            { 
                              var c = (guideH/artHeight2)*100; 
                              app.activeDocument.selection[0].resize(c,c); 
                              } 
                                 
                        
                            }//end if doresize 
                        }//end function resizeart 
                        
                        
                        
                        function putname () 
                        { 
                           
                          var thisDoc = app.activeDocument; 
                                thisDoc.rulerOrigin = [0,0]; 
                                thisDoc.pageOrigin = [0,0]; 
                           
                          var filename = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
                          var createdate = app.activeDocument.modified; 
                           
                          var  theDate = new Date(); 
                                var day = theDate.getDate(); 
                                var month = theDate.getMonth() + 1; 
                                var year = theDate.getFullYear(); 
                                var hours = theDate.getHours(); 
                                var min = theDate.getMinutes(); 
                                    if (min < 10) 
                                        min = ("0" + min); 
                                var morn; 
                                    if (hours >= 12) 
                                        { 
                                            hours = hours-12; 
                                            morn = " PM"; 
                                        } 
                                    else 
                                        morn = " AM" 
                        
                        var saveDate = (month + "-" + day + "-" + year + "    " + hours + ":" + min + morn ); 
                           
                           
                           
                        
                          var nameTextFrame = thisDoc.textFrames.add(); 
                            nameTextFrame.contents = globalartistname + "\n" + filename + "\n" + saveDate;   
                            nameTextFrame.top = 1000; 
                            nameTextFrame.left = 100; 
                            nameTextFrame.filled = true; 
                            nameTextFrame.alignment = "center"; 
                             
                            }//end put name 
                        
                        function savefile () 
                        { 
                        
                        
                            //change this folder location for the "savescript" variable to the one where the location of the save script is located on the server, 
                            //This save script is for the pallet that will pop up and list the different save options, not the individual save scripts themselves. 
                             
                                var savescript =  new File("S:/Art/ArtDept/Illustrator JS Palette/Scripts/Art Share Server/Save/Save Option Pallet"); 
                                    savescript.open("r");       
                              var bt = new BridgeTalk; 
                                    bt.target = "illustrator";           
                              var script = savescript.read(); 
                                    savescript.close();               
                              bt.body = script; 
                              bt.send();   
                               
                              if (saved = true) 
                              { 
                                  alert("File saved") 
                                } 
                               
                              if (saved == false) 
                              { 
                                  alert ("File not saved") 
                                  } 
                               
                              return true; 
                        }       
                        
                        
                        //********************************************************************************************************************************************************** 
                        //********************This section is for the functions to check which button gets pressed and returns the text to a variable for output **************** 
                        //********************************************************************************************************************************************************** 
                        
                        
                        //all radiobutton groups must be pressent in this array 
                        var totalgroups = [totes1] 
                        
                        //this function checks each group for the 1 radio button is clicked then returns its text 
                        function selected_rbutton (totalgroups) 
                        {//start function to check buttons 
                        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Totes tab buttons 
                        
                        //when totes1 is pressed return text for output 
                        for (var i = 0; i < totes1.children.length; i++) 
                        { 
                        if (totes1.children[i].value == true) 
                        { 
                        return totes1.children[i].text; 
                        }//end if 
                        
                        }//end if 
                        }//end for 
                        
                        //////////////////////////////////////////////////////////////////////////open template 
                        
                        function openTemp() 
                        { 
                            //change the folder location of the templates here but leave the last section that is black, this will allow the script to open which 
                            //ever template they chose from the radiobuttons.  This location should be the same as the toteTempPath above but reduces the 
                            //chance for something to break by pulling the address here again instead of from the variable near the begining of the script. 
                             
                        var gettotetemp = new File("Z:/  Vitronic Master Templates/STE2 Templates/TOTES & BAGS/" + selected_rbutton (totalgroups)); 
                        //var gettotetemp = new File("c:/temp/templates/" + selected_rbutton (totalgroups)); 
                        
                        
                        var template = open(gettotetemp);  // get a reference to the template @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                          return template; // return the open template document @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                        }//end open temp function 
                        
                        }//end if selection check 
                        
                        
                        else 
                        { 
                            alert("Please select the artwork."); 
                          } // end else selection check 
                        
                        • 9. Re: Script errors and a question
                          Zantcor Level 1

                          I'm not sure how I can thank you Carlos, you went through my entire script just about and correct all my mistakes that I had made, I was able to implement the reference to template and group name as you suggested.

                           

                          for ( i = 0; i < docSelection.length; i++ ) {
                             var newItem = docSelection[i];
                          newItem.moveToEnd( newGroup );
                          newItem.name = "Art Item" + ([i]+1);
                          }
                          newGroup.name = "ArtGroup1";
                          

                           

                          name the group then use

                           

                          
                          
                          
                          
                          var selecteditem = app.activeDocument.groupItems[0];
                          
                          
                          
                          

                           

                          instead of

                          var selecteditem = app.activeDocument.selection
                          

                          to avoid the whole "array" issue

                           

                          as for the Copy and Paste I made a function to replace both that now works wonderfully and I'm not getting the timing errors or simply not having the art files get pasted anymore. Here's the code I used for it;

                           

                           function copyandmove ()
                           {
                          var thisDoc = app.activeDocument;   
                          var template = app.documents[1];
                          var vartwork = app.documents[0];
                          var itemToDuplicate = template.pageItems['ArtGroup1'];  
                          itemToDuplicate.duplicate( thisDoc ); 
                           }
                          

                           

                          If you were a drinking man I'd buy you a round, I'm not sure how I can ever repay all the helpful people on here but someday I hope that I can.

                          • 10. Re: Script errors and a question
                            CarlosCanto Adobe Community Professional & MVP

                            you're welcome...someday when we have the mythical Scripting Christmas Party

                             

                            or if you ever come to Disneyland, give me a call

                            • 11. Re: Script errors and a question
                              Zantcor Level 1

                              I used to live in Titusville, FL lots of good memories down there.

                               

                              Is there a way to setup a script to pop up alerts with the line that it broke on?  Kind of like when a program breaks it always says "Error #1423 - Please copy and paste this into an email to Creator@creatorsfunhouse.com"  I think it would help debug the issues as I can't always recreate what someone else has done so I have to read through the entire code line for line and check to see if there is a conflicting variable or something.

                              • 12. Re: Script errors and a question
                                Silly-V Adobe Community Professional

                                you can use the try...catch statements to wrap the suspect page of code, and keep track of a running variable, which will be documented and shown in the catch statement.
                                My example

                                 

                                var myProblem = '';
                                try{
                                    myProblem = "Doing thing number One";
                                    // code code code for thing number One
                                
                                    myProblem = "Doing thing number Two";
                                    // code code code for thing number Two
                                
                                    myProblem = "Doing thing number Three";
                                    // code code code for thing number Three
                                } catch(Error_Object) {
                                    alert(
                                          "This error happened!"+"\r"+
                                          Error_Object+"\r"+
                                          "It happened during:"+"\r"+
                                          myProblem
                                    );
                                }
                                

                                 

                                As you can see, the catch block will help you alert the actual error in addition to your custom variable's string contents at the time something went wrong.