4 Replies Latest reply on Sep 2, 2015 3:37 PM by aiScripting

    Execute and return a value (or object) from another script

    aiScripting Level 1

      I would like to be able to have some of the little functions that I use often in my scripts just as seporate scripts. so I can update then in one place and not have to copy them into every script I want to use them in.

      I am not sure if this is possible at all. But I would love to be able to just call them somehow and return values from them.

      e.g.

      Main code I need some colored swatches:

              var inCutColorCMYK = cmykColor(50, 0, 100, 0);
              var intCutSPOT = makeSwatch("CutIN", inCutColorCMYK);
      

       

      so I have these functions that I found/tweeked:

         function makeSwatch( swName, swCol) {  
          var doc = app.activeDocument;
          var sel = doc.selection;
          if (!inCollection(doc.swatches, swName)) {  
          var aSwatch = doc.spots.add();  
          aSwatch.name = swName;  
          aSwatch.color = swCol;  
          aSwatch.tint = 100;  
          aSwatch.colorType = ColorModel.SPOT;  
          var aSwatchSpot = new SpotColor();  
          aSwatchSpot.spot = aSwatch;  
          return aSwatchSpot;  
          }else{  
              return doc.swatches.getByName(swName);  
              }  
          };
      
      
      function cmykColor(c, m, y, k) {  
          var newCMYK = new CMYKColor();  
          newCMYK.cyan = c;  
          newCMYK.magenta = m;  
          newCMYK.yellow = y;  
          newCMYK.black = k;  
          return newCMYK;  
          };
      
      
      
      
      function inCollection(collection, nameString) {  
             var exists = false;  
         try{
          for (var i = 0; i < collection.length; i++) {  
              if (collection[i].name == nameString) {  
                  exists = true;  
                  }  
              }  
          }catch(e){//alert("Illustrator needs drop kicked!!\nPlease restart Illustrator :/\n\n"+e)
              }
              return exists;  
              
            };
      

       

      anyway! I keep finding my self using functions like this over and over in new scripts and hope there is a way to do this!

      Any help is appreciated!

      -Boyd

        • 1. Re: Execute and return a value (or object) from another script
          Silly-V Adobe Community Professional

          You could have a.jsx file , something like myLibrary.jsx, and in it you can have all the functions for future use. In the file it may look like this:

          ------------------------------------ Library file -----------------------------------

          #target illustrator

          function MyLibrary(){

               this.cmykColor = function(c, m, y, k) {   

                   var newCMYK = new CMYKColor();   

                   newCMYK.cyan = c;   

                   newCMYK.magenta = m;   

                   newCMYK.yellow = y;   

                   newCMYK.black = k;   

                   return newCMYK;   

              };

              function applyFillColorTo(item, color){

                  item.fillColor = color;

              }

              function CataloguedPathItem(pathItem){

                  var p = pathItem;

                  p.applyFillColor = function(color){

                      applyFillColorTo(this, color);

                  };

                  return p;

              }

              this.cataloguedPathItem = function(pathItem){

                  var p = new CataloguedPathItem(pathItem);

                  return p;

              }

          };

           

          ----------------------------------------------------------------------------------------

          And then you can include and use that file in other scripts, like here:
          -------------------------------------- Script File ---------------------------------

          #target illustrator

          #include "C:\\Users\\Me\\Desktop\\My Adobe Scripts\\Illustrator\\MyLibrary.jsx"

          function test(){

              var mylib = new MyLibrary(); // the library script is used as an object - so items from its scope can be accessed via dot (.) like mylib.myFunction();

           

           

              if(app.documents.length > 0){

                  var doc = app.activeDocument;

                  if(doc.selection.length > 0){

                     

                      var myItem = mylib.cataloguedPathItem(doc.selection[0]);

                      var myColor = mylib.cmykColor(0,100,50,0);

                      myItem.applyFillColor(myColor);

                     

                  }

              } else {

                  alert("There is no document open.");

              }

          }

          test();


          ----------------------------------------------------------------------------------------


          The test script just colors a pathItem, but it uses functions from inside the "library" object to power the process. Note that to use the library to color a path involved instantiating that path as a custom object which has a "applyFillColor" method which uses a function from the library scope (called "applyFillColorTo")  to power it. We can't then, simply use "applyFillColorTo" function in the Script File scope, because it is protected by the MyLibrary object.

          • 2. Re: Execute and return a value (or object) from another script
            Silly-V Adobe Community Professional

            Of course, you can just keep simple functions inside the MyLibrary file, and use no object instances or anything, to get access to your commonly used functions.

            • 3. Re: Execute and return a value (or object) from another script
              aiScripting Level 1

              wow! this sounds EXACTLY like what I have been looking for!

              Thanks a million!!!!!!

              -Boyd

              • 4. Re: Execute and return a value (or object) from another script
                aiScripting Level 1

                Yes! just tested this and this is so perfect for what I need!!

                Thanks a million!!!!