0 Replies Latest reply on Apr 22, 2015 12:18 PM by Zantcor

    Makeing the scripts a plugin & sharing is fun!

    Zantcor Level 1

      Hello world of scripters and fellow adobe enthusiasts. I've been tossing around the idea of making all my scripts into plugins for illustrator, and since they are all done in JS how hard would it be to do this?  Would it only be a matter of downloading eclipse and the SDK with a little copy and pasting and be done or would it mean an entire overhaul of my scripts?

       

      As for the sharing part I know that some of the scripts I've written I've done so with the help of previously written scripts.  I've gotten a lot of help from on here and I've even helped out a bit when I can.  I think it would be a big help to everyone if we shared some of our favorite scripts so that we can all see little tips and tricks that people have learned over the years.  Some of the things I've learned on here I've learned from looking at other peoples scripts and just going, "Wow I didn't know I could do that".

       

      I'll go first, this script is the one I created with the help of some of my fellow scripters on here but it is still a big thing for me. When the script is ran it grabs all the files that exist inside the folder that it is pointed to and creates a large list of radio buttons for the user to choose from.  Then if there is a guide box it will center the artwork inside the guide box and resize if needed.  It doesn't sound like much but it has been a big help to all of my fellow artists.

       

      //Created by Daryl R. Smith
      //This is the tab for Totes
      
      #target illustrator 
      
      if (app.activeDocument.selection.length > 0)
      {
          
          var thisDoc = app.activeDocument;
      
      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 );
      newItem.name = "Art Item" + ([i]+1);
      }
      newGroup.name = "ArtGroup1";
      
          //hide this alert, it is for testing the selection length
        // alert (thisDoc.selection.length)
      
      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 ("S:/Art/ArtDept/  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)
              {
                  
                  
                  
                  openTemp ();
                  copyandmove ();
                  centerArt ();
                  resizeart ();
                  
                  alert ("You can now save the file");
                  
               } /*end if */ else
           {
               alert ("You chose not to continue, script stopped")
               }//end else
      }//end if OK
      
      }//end if selection check
      
      
      else
      {
          alert("Please select the artwork.");
        } // end else selection check 
      
      
      //**********************************************************************************************************************************************************
      //*********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.groupItems[0];
              selecteditem.selected = true;
              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.
              selecteditem.position = [guideXCenter, guideYCenter]    
              
          }//end centerart
      
         function resizeart()
      {
          
        var selecteditem = app.activeDocument.groupItems[0];  
          
         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 = selecteditem.width;
              var itemH = selecteditem.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;
                 selecteditem.resize(a,a);
               }   
          if (artHeight > artWidth)     
              {
                 var b = (guideH/artHeight)*100;
                 selecteditem.resize(b,b);
               }
           
       var artWidth2 = selecteditem.width;
      var artHeight2 =selecteditem.height;    
      
       if (artHeight2 > guideH)
          {
             var c = (guideH/artHeight2)*100;
            selecteditem.resize(c,c);
            }
        
         if (artWidth2 > guideW)
          {
             var c = (guideW/artWidth2)*100;
            selecteditem.resize(c,c);
            }
              
      
           }//end if doresize
      }//end function resizeart
      
      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.jsx");
                  savescript.open("r");      
             var bt = new BridgeTalk;
                  bt.target = "illustrator";          
             var script = savescript.read();
                  savescript.close();               
             bt.body = script;
             bt.send();  
             
             return true;
       }      
      
      
       function copyandmove ()
       {
      var thisDoc = app.activeDocument;   
      var template = app.documents[1];
      var vartwork = app.documents[0];
      var itemToDuplicate = template.pageItems['ArtGroup1'];  
      itemToDuplicate.duplicate( thisDoc, ElementPlacement.PLACEATBEGINNING ); 
      }//end function copyandmove
      
      //**********************************************************************************************************************************************************
      //********************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 present 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 = File("Z:/  Vitronic Master Templates/STE2 Templates/TOTES & BAGS/" + selected_rbutton (totalgroups));
      
      var template = open(gettotetemp);
      var fileName = app.activeDocument.name
      
      var saveName = new File ("S:/Art/ArtDept/Extras/Temp Holding Folder" + "/" + fileName);
          saveOpts = new IllustratorSaveOptions();
          saveOpts.compatibility = Compatibility.ILLUSTRATOR15; 
          saveOpts.generateThumbnails = true; 
          saveOpts.preserveEditability = true;
      
        return template;
      
      }//end open temp function