Creating PDFs in Processing and post-processing them in Adobe Illustrator, I frequently find myself wanting to do things like adjust global transparency levels, colors etc. So far that’s been frustrated by the rather poor color adjustment options built into Illustrator, but today I finally got impatient enough to look into a scripting solution.

Illustrator has had Javascript support since CS 1, exposing the document object model to anyone with a bit of scripting savvy. Adobe is good about publishing technical documents, perhaps a holdover from the days when they relied on PostScript to build their empire. So anyone can go to the online scripting documentation and download a complete PDF with a description of the Illustrator API.

As might be expected, coding Javascript is not without its troubles, and I found myself having dotcom flashbacks to the days when I would do client-side scripting. In particular, debugging is always been a pain with Javascript, especially when one is not intimate with the DOM and API. Fortunately, the ExtendScript Toolkit provided by Adobe functions both as a IDE and a debugger. Still, I find myself wanting a few nice details, like the possibility of displaying a progress bar when a script is executing.

To give you an idea of the oddities of coding for Illustrator, I am posting a script that allows the user to input a multiplier, which is then used to adjust the opacity of all path items in the active document. This covers for the lack of such a function in Illustrator proper. To be honest I was surprised at the speed with which I was able to accomplish my goal. I would seriously consider this as a way to do brute-force post-processing of vector files.

That said, I doubt it will ever be as fun and quirky as Scriptographer

Source code – OpacityAdj.js

// OpacityAdj.js - JavaScript for Adobe Illustrator CS
// Marius Watz - http://workshop.evolutionzone.com
//
// Adjusts opacity for all paths by a given multiplier.

// Prompt user for input
var mult=eval(prompt("Multiplier for transparency",1,"TransMult.js"));

// $.writeln() is used to write to the JavaScript console
$.writeln("Multiplier: '"+mult+"'");
if(mult==1) alert("Multiplier is 1.0. No changes made.");
if(mult<=0) {
  alert("Multiplier can't be zero or negative.");
  mult=1;
}

index=0;

// Get active document from the global Application variable "app".
doc = app.activeDocument;

if ( doc.pathItems.length > 0 && mult!=1) {
  thePaths = doc.pathItems;
  numPaths = thePaths.length;
  for ( i = 0; i < numPaths ; i++ ) {
    pathArt = doc.pathItems[i];
    newval=pathArt.opacity*mult;
    if(newval>100) newval=100;
    pathArt.opacity=newval;

    // Provide some feedback for complex documents.
    if(index%500==0) {
      percstr=""+((index/numPaths )*100);
      decimalPos=percstr.indexOf(".");
      if(decimalPos!=-1 && (percstr.length-decimalPos>4))
        percstr=percstr.substring(0,decimalPos+2);
      $.writeln(percstr+"% - "+index+"/"+numPaths );
    }
    index++;
  }
  $.writeln("OpacityAdj.js done.");
}

1 Comment »

There is one comment to "Scripting Adobe Illustrator CS using JavaScript". You may leave your own comment.
1. Melatonin Side Effects ยท, November 7th, 2010 at 20:13

adboe illustrator is feature packed and i love all of its great features “

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">