0 Replies Latest reply on Mar 22, 2015 5:57 AM by Bobo Charlos

    Distribute eps objects with file list and quantity

    Bobo Charlos

      Hello everyone,

       

      Here's a script i wrote so I share it for the contribution.

       

      Here's my purpose. Every year, for a customer of our company, we have to print several description plates for their products. They give us about 30 or more texts, traduced in about 7 languages, and an excel file with quantities that each retail seller of each country do want to receive.

       

      We print these aluminium plates on a uv cured ink printing machine which have a table dimension of about 60x40cm. Preparing manually every files of 60x42cm to print was way too much time devouring, so i wrote that script.

      Before running this script you have to create a folder with your text files in eps, and create a new document with the dimension you want.

      Don't hesitate if you want more precisions with the process.

       

       

       

      #target illustrator

      #targetengine session

       

      /*

      This script allows you to import EPS files as linked files. It distributes them and centers the all according to the dimension of the artboard of your document, then it saves the file.

      You can define everything in the start list : target folder, basename of the destination files, how much objects per sheet you want, the roation if necessary, spacing between each objects, number of culumn for the distribution.

      If you want to distribute a number of objects that is bigger thant the limit of objects per sheet, the script reports the remaining in a new sheet after saving the previous one.

       

      The first 2 arrays contains the filenames to use in the source folder and their respective quantity.

      */

       

       

       

      var doc = app.activeDocument;

      doc.rulerOrigin = [0,0];

      var xtimes = 0;

      var countName;

      var folder;

      var folder2;

      //var folder = 'C:\\Documents and Settings\\Administrateur\\Bureau\\BBB\\';

       

       

      // array for filenames

      var listEx=new Array (

      "File1.eps",

      "File2.eps",

      "File3.eps",

      );

       

      // array for quantity for each filenames

      var listqte=new Array (

      15,

      8,

      19,

      );

       

       

       

      //-------------UI CODE------------

       

      var win = new Window ("dialog");

      win.alignChildren = "left";

       

      // ------Folders selection panel

      var foldPanel = win.add("panel");

      foldPanel.alignChildren = "right";

       

      // source folder for files

      var panelGrp1 = foldPanel.add("group");

      var btnSource = panelGrp1.add("button",undefined,"Source Folder :");

      var txtSource = panelGrp1.add("edittext",undefined);

      txtSource.characters = 40;

      btnSource.onClick = function()

      {

          folder2 = Folder.selectDialog ("Select Source folder..."); // get the source folder

          txtSource.text = folder2.fsName; // show the file Path here

      }

       

      // destination folder

      var panelGrp2 = foldPanel.add("group");

      var btnSave = panelGrp2.add("button",undefined,"Save Folder :");

      var txtSave = panelGrp2.add("edittext",undefined);

      txtSave.characters = 40;

      btnSave.onClick = function()

      {

          folder = Folder.selectDialog ("Select Destination folder..."); // get the source folder

          txtSave.text = folder.fsName; // show the file Path here

      }

       

      // Base name for destination files

      var panelGrp3 = foldPanel.add("group");

      panelGrp3.alignment = "left";

      var bfn = panelGrp3.add("statictext",undefined,"Basename of target file :");

      var txtbfn = panelGrp3.add("edittext",undefined,"Description plates - UK");

      txtbfn.characters = 20;

      //--------

       

              // other parameters

              var grp = win.add("group");

              grp.alignChildren = "left";

              grp.orientation = "column";

       

              // counter

              var wincount = grp.add("group");

              var textcount = wincount.add("edittext",undefined,0);

              textcount.characters=2;

              wincount.add("statictext",undefined,"Start count");

       

              //Limit number of placeditems to distribute in one file

              var winlimit = grp.add("group");

              var textlimit = winlimit.add("edittext",undefined,0);

              textlimit.characters=2;

              winlimit.add("statictext",undefined,"Limit");

       

              //number of Columns for the distribution

              var wincolon = grp.add("group");

              var textcolon = wincolon.add("edittext",undefined,0);

              textcolon.characters=2;

              wincolon.add("statictext",undefined,"Nb of columns");

       

              // Spacing values between each objects in mm

              var winspace = grp.add("panel",undefined,"Spacing in mm");

              winspace.orientation = "row";

              winspace.add("statictext",undefined,"X");

              var Xspace=winspace.add("edittext",undefined,0);

              Xspace.characters=7;

              winspace.add("statictext",undefined,"Y",undefined,0);

              var Yspace=winspace.add("edittext");

              Yspace.characters=7;

       

              // rotation angle

              var winrotate = grp.add("group");

              var textangle = winrotate.add("edittext",undefined,0);

              textangle.characters=3;

              winrotate.add("statictext",undefined,"Rotation in °");

             

      // Dropdownlist of presets   

      var winPreset =grp.add ("dropdownlist", undefined, [ "Description plates","Trapezes plates","-","Perso"]);

      winPreset.onChange = function ()

      {

          switch (winPreset.selection.text)

              {

                      case "Description plates":

                              textlimit.text=8;

                              textcolon.text=4;

                              Xspace.text=6.4;

                              Yspace.text=27;

                              textangle.text=0;

                              break;

                     

                      case "Trapezes plates":

                              textlimit.text=18;

                              textcolon.text=6;

                              Xspace.text=9;

                              Yspace.text=9;

                              textangle.text=0;

                              break;

                             

                      case "Perso":

                              textlimit.text=0;

                              textcolon.text=0;

                              Xspace.text=0;

                              Yspace.text=0;

                              textangle.text=0;

                              break;

              }

      }

      //winPreset.selection=1;

      //   

             

      var runBT=grp.add("button",undefined,"Run !");

      runBT.onClick = function()

      {

          win.close();

          moteur();

      }

       

      win.show();

      //--------------------End of UI CODE----------------

       

       

       

       

      //---------------------------------fonction  Placement of objects

      function place()

      {

          var paddingx = parseFloat(Xspace.text)*2.834;        //spacing in mm between each column (for a value in points just suppress the *2.834)

          var paddingy = parseFloat(Yspace.text)*2.834;        //spacing in mm between each line

          var gridCols = textcolon.text;                            // number of columns

          var newGroup = doc.groupItems.add();

          var sel = doc.placedItems;

          // set the position of the first element, assuming that the group of all elements are centered  in X and Y  in the artboard

          var originX = (doc.width - ((doc.width - ((sel[0].width * gridCols) + (paddingx) * (gridCols-1)))/2))-sel[0].width

          var originY =((doc.height - (( sel[0].height * (textlimit.text/gridCols) ) +  (  paddingy*( (textlimit.text/gridCols)-1 )  )))/2)+sel[0].height

          var currentX= originX

          var currentY = originY

             

          for(var e=0, slen=sel.length;e<slen;e++)       

         

              {

                  //   :::SET POSITIONS:::

                  sel[e].top = currentY;

                  sel[e].left = currentX;

             

                  //  :::DEFINE X POSITION:::

                  currentX += -(sel[e].width + paddingx);

                 

                                

                  if((e % gridCols) == (gridCols - 1))

                      {

                          currentX =  originX

                          //  :::DEFINE Y POSITION:::

                          currentY  += sel[e].height+paddingy;

                      }

             

                  // ::Add to group

                  sel[e].moveToBeginning (newGroup );

                  redraw()

              }

       

      }

      //---------------------------------

       

       

       

      //----------------------function Save

      function sauve()

      {

          var fich =  txtbfn.text;    //Basename of the destination file

         

          // embed elements (for my case these are eps placeditem files)   

          for (var i = doc.placedItems.length-1; i >= 0; i-- )

              {

                  app.activeDocument.placedItems[i].embed();

              }

         

          // draw a rectangle with the dimensions of the artboard and centered to it, with no fill color neither stroke color

          doc.rulerOrigin = [0,0];  // rulers to the origin

          var artboardRef = doc.artboards[0];

          // read dimensions oh the artboard to position therectangle

          var top=artboardRef.artboardRect[1] ;

          var left=artboardRef.artboardRect[0];

          var width=artboardRef.artboardRect[2]-artboardRef.artboardRect[0];

          var height=artboardRef.artboardRect[1]-artboardRef.artboardRect[3];

         

          var rect = doc.pathItems.rectangle (top, left, width, height);   

          rect.stroked = false;

          rect.filled = false;

          //

       

          countName ++;

         

          // when several files are saved, the ten first files are numbered like this : 01, 02, 03... instead of 1,2,3

          var countName2 = countName;

          if (countName<=9) { countName2 = ("0" + countName)};

       

          if (xtimes != 0) // saves in eps, basename of file + number of times to repeat if necessary (it's to avoid to save several but identical files. For ex. if i have an object to print 100 times and i place 50 objects per file, the mention "2 times" will be mentionned in the name of the destination file )

              {

                  var dest= new File(folder + '/' + fich + ' ' + countName2 + ' - ' + xtimes + ' times' + '.eps');

              }

          else

              {

                  var dest= new File(folder+ '/' + fich + ' ' + countName2 + '.eps') ;

              }

             

          var options = new EPSSaveOptions();

          options.preview  = EPSPreview.None;

          //options.compatibility = Compatibility.ILLUSTRATOR14;

          //options.overprint = PDFOverprint.DISCARDPDFOVERPRINT

          //options.embedAllFonts = false;

          //options.includeDocumentThumbnails = false

          doc.saveAs(dest, options);

         

      }

      //------------------------------

       

       

      //-------------------- function moteur

      function moteur()

      {

          var limite = textlimit.text;            // max number of objects to distribute in one sheet

          var couchangl = textangle.text;    // rotation angle of the element

             

          //--------------Searches and signals if there is missing files in the source folder. If so, the script stops and displays which files are missing

          var miss=new Array();

         

          for (var i=0,len1=listEx.length;i<len1;i++)

          {

              var myfile = new File(folder2+'/'+listEx[i]);

              if (myfile.exists==false)

                  {

                      miss.push(listEx[i]);

                  }

          }

          if (miss.length != 0)

              {

                  alert (miss.length+" missing files : "+"\r"+miss.join(", "+"\r")+"\r"+"\r"+" Please correct and try again");

                  return;

              }

          //--------------end of  verification

         

         

      var start = new Date();        // starts chrono

      countName = textcount.text;    // start of the counter to number the name of the file to save

       

       

          //-------------disctribution of the object on the sheet

          for (var i=0,howmuch = 0,len1=listEx.length;i<len1;i++)

          {

              for (var j =0; j<listqte[i];j++)

                  {

                      if (howmuch==0)

                          {

                              if ((listqte[i]-j)/limite>=2) //activate "xtimes" if quantity is twice or more bigger than "limit"

                                  {

                                      xtimes = parseInt ((listqte[i]-j)/limite);

                                      j += limite*(xtimes-1);

                                  }

                          }

                     

                      myfile = new File(folder2+'/'+listEx[i]);

                      var  thisPlacedItem = doc.placedItems.add();

                      thisPlacedItem.file = myfile;

                      // rotate if necessary

                      if (couchangl !=0) thisPlacedItem.rotate(couchangl);

                      howmuch ++;

             

                      if (howmuch == limite)

                          {

                              place();

                              sauve();

                              doc.pageItems.removeAll();

                              howmuch = 0;

                              xtimes = 0;

                          }       

                  }

          }

      place();

      sauve();

      redraw();

      alert("time spent : "+((new Date() - start)/1000)+" secondes");

      }

      //------------------------