Adobe Illustrator Scripting


#1

Hey All,
Been a while since I posted on here, but I’ve been tasked with scripting out some changes in Adobe Illustrator and had a general question. Does anyone know any previous forum discussions, links, hidden gems of insight on adobe Illustrator scripting, etc that may point in the right direction to script out some color changes to .ai files? I’ll eventually have to batch through a few hundred files once I have figured out how to script out the proper changes.

Just looking for any links, general direction, etc.

Thanks! - Trey


#2

Main Doc I found: http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/illustrator/sdk/CC2015_3/Illustrator%20JavaScript%20Scripting%20Reference.pdf
Additionally found this site: http://yearbook.github.io/esdocs/#/Illustrator/Transformation

The following is the final script (A version of it) I used for this task. Hopefully it will help others.
What it does: I needed to change a stack of AI files, which contained a green fill color (3 different slight variations in each, due to 3 different designers.). They needed a blue fill color (Specified within the script). Additionally I needed to save out a new AI file with a _blue at the end to denote it is the blue version, as well as export out a new png file of the blue map. The script also unlocks, locked layers in order to do this. After each file is processed, it is closed, and it moves to the next file in the queue. Any other questions ask and I will be happy to clarify. - Trey


#target illustrator-20

var sourceFolder, files, fileType, sourceDoc, targetFile;

sourceFolder = Folder.selectDialog('Select the folder with the illustrator files');

var imageLoc = Folder.selectDialog("Select where you would like the new png images to be placed.");
var aiLoc = Folder.selectDialog("Select where you would like the newly created, modified illustrator files to be placed.");

if (sourceFolder != null) {
   files = new Array();
   fileType = '*.ai';
   files = sourceFolder.getFiles(fileType);
   //$.writeln(files.length);
   if (files.length >= 0) {
       for(var j=files.length-1; j >= 0; j--) {
       //$.writeln("FileName: ", files[j]);
        app.open(File(files[j]));
        var doc = app.activeDocument;
       
        for(var i=0; i<doc.layers.length;i++) {
        doc.layers[i].locked = false;
        //$.writeln("Layers Lock ", doc.layers[i].locked);

        }

        var allEl = app.name;

        $.writeln("All Elements: ", allEl); 


        var oldColor = new CMYKColor();
        oldColor.cyan = 50;
        oldColor.magenta = 0;
        oldColor.yellow = 99;
        oldColor.black = 0;

        var LightBlue = new CMYKColor();
        LightBlue.cyan = 45;
        LightBlue.magenta =18;
        LightBlue.yellow = 14;
        LightBlue.black = 0.0;

        var oldRGB = new RGBColor();
        oldRGB.red = 140;
        oldRGB.green = 198;
        oldRGB.blue = 63;

        var LightBlueRGB = new RGBColor();
        LightBlueRGB.red = 141;
        LightBlueRGB.green = 181;
        LightBlueRGB.blue = 201;

        var oldRGB2 = new RGBColor();
        oldRGB2.red = 139;
        oldRGB2.green = 197;
        oldRGB2.blue = 64;

        var LightBlueRGB2 = new RGBColor();
        LightBlueRGB2.red = 141;
        LightBlueRGB2.green = 181;
        LightBlueRGB2.blue = 201;

       /* for(var f = 0; f < doc.rasterItems.length; f++){
            $.writeln("Hit On: ", doc.rasterItems);
            doc.rasterItem[i].resize(90,90, true, true, true, true, 90, Transformation.DOCUMENTORIGIN);
            }*/
        
        //IF YOU NEED TO RESIZE THE DOCUMENT - UNCOMMENT THIS
      /*  for (var f = 0; f < doc.pageItems.length; f++){
            doc.pageItems[i].resize(90,90, true, true, true, true, 90, Transformation.DOCUMENTORIGIN);
            } */

        for(var i=0; i<=doc.pathItems.length; i++) {
            var a = doc.pathItems[i];
            var p = doc.pageItems[i];
            var aFill = a.fillColor;
            var aYellow;
            //doc.pathItems[i].resize(90,90, true, true, true, true, 90, Transformation.DOCUMENTORIGIN);
            
         
 
      
            
          if(a.fillColor != null) {
              a.fillColor.cyan= Math.round(a.fillColor.cyan);
              a.fillColor.magenta = Math.round(a.fillColor.magenta);
              a.fillColor.yellow = Math.round(a.fillColor.yellow);
              a.fillColor.black = Math.round(a.fillColor.black);
              //$.writeln("2)CYAN TEST: ",  a.fillColor.cyan);
         
            if(a.fillColor.cyan === oldColor.cyan && a.fillColor.magenta === oldColor.magenta && Math.round(a.fillColor.yellow) === oldColor.yellow && a.fillColor.black === oldColor.black) {
               // $.writeln("Elements with Fill Color =  C ",  a.fillColor.cyan + " M " +  a.fillColor.magenta + " Y " + Math.round(a.fillColor.yellow) + " K " + a.fillColor.black );
                aFill.cyan = LightBlue.cyan;
                aFill.magenta = LightBlue.magenta;
                aFill.yellow = LightBlue.yellow;
                aFill.black = LightBlue.black;
                
                //a.strokeWidth =2;
                //$.writeln("Line Wdith: ", a.strokeWidth);
                //$.writeln("Changed elements with Fill Color =  C ",  a.fillColor.cyan + " M " +  a.fillColor.magenta + " Y " +  Math.round(a.fillColor.yellow) + " K " + a.fillColor.black ); 
                }
            else {
               //$.writeln("No Matching Colors Found: Listed Colors Found = ", Math.round(a.fillColor.yellow * 100) /100);
              }
                 
                  
            }
        }

        for(var i=0; i<=doc.pathItems.length; i++) {
            var a = doc.pathItems[i];
            
          if(a.fillColor != null) {
           // $.writeln("Line Wdith: ", a.fillColor.red + " " + oldRGB.red + " " + a.fillColor.green + " " + oldRGB.green + " " + a.fillColor.blue + " " + oldRGB.blue);
            if(a.fillColor.red == oldRGB.red && a.fillColor.green == oldRGB.green && a.fillColor.blue == oldRGB.blue) {
                //$.writeln("Elements with Fill Color =  R ",  a.fillColor.red + " G " +  a.fillColor.green + " B " + a.fillColor.blue);
                a.fillColor = LightBlueRGB;
               
                
                //a.strokeWidth =2;
                //$.writeln("Line Wdith: ", a.strokeWidth);
                //$.writeln("Changed elements with Fill Color =  R ",  a.fillColor.red + " G " +  a.fillColor.green + " B " +  Math.round(a.fillColor.blue) ); 
                }
            else {
              // $.writeln("No Matching Colors Found: Listed Colors Found = ", a.fillColor.red);
              }
                 
                  
            }
        }

        for(var i=0; i<=doc.pathItems.length; i++) {
            var a = doc.pathItems[i];
            
        
            
          if(a.fillColor != null) {
          //  $.writeln("Line Wdith: ", a.fillColor.red + " " + oldRGB2.red + " " + a.fillColor.green + " " + oldRGB2.green + " " + a.fillColor.blue + " " + oldRGB2.blue);
            if(a.fillColor.red == oldRGB2.red && a.fillColor.green == oldRGB2.green && a.fillColor.blue == oldRGB2.blue) {
                //$.writeln("Elements with Fill Color =  R ",  a.fillColor.red + " G " +  a.fillColor.green + " B " + a.fillColor.blue);
                a.fillColor = LightBlueRGB2;
               
                
                //a.strokeWidth =2;
                //$.writeln("Line Wdith: ", a.strokeWidth);
               // $.writeln("Changed elements with Fill Color =  R ",  a.fillColor.red + " G " +  a.fillColor.green + " B " +  Math.round(a.fillColor.blue) ); 
                }
            else {
               //$.writeln("No Matching Colors Found: Listed Colors Found = ", a.fillColor.red);
              }
                 
                  
            }
        

        }
        
        //PNG Export 
        var prefixFilename = doc.name;
        var noAIFilename = prefixFilename.replace(/.ai/g, "");
        $.writeln("FileReName: ", noAIFilename);
        var myFile = File(imageLoc + "/" + noAIFilename +".png");
        var option = new ExportOptionsPNG24();
        option.artBoardClipping = true;
        doc.exportFile(myFile, ExportType.PNG24, option);
        var vers = new File(aiLoc + "/" + noAIFilename + "_blue" + ".ai");
        var options = new IllustratorSaveOptions();
        //Save as new file name
        doc.saveAs(vers, options);
        //Close the file
        doc.close(SaveOptions.SAVECHANGES); 
        $.gc();
        
        
      }

   }

    
    
 }