Below is a sample script I wrote to make web posters automatically.

An array is defined with arrays of image data (copyright, author, image name, etc.).  Then one image is loaded into Illustrator, scaled to fit the screen, placed on screen, and clipped to give it some border space.  A title is added and styled according to a pre-defined style called “MainTestStyle”.  A copyright notice is added underneath the image.  There are a few glitches, and it is not universal code, but this was a short project that needed a quick solution.

It does require some setup in Illustrator.  This script assumes:

  1. at least two layers.
  2. the document has a style called “MainTextStyle”.

I will comment more on this post if anyone asks for more details.  Otherwise, this is just a reference for me to a lot of AI scripting ideas.

// Note:  This script requires the correct context.  
// If you are using ExtendScript Toolkit CS6 (version 3.8.x or maybe others): 
// 1) In the top right corner of the script view, there is a drop-down menu where "Adobe Illustrator CS6 Type Library" should be selected.
// 2) In the dropdown box immediately above the script area, select "Adobe Illustrator CS6 [...]" should be selected.  

var rootdir = "C:\\Root\\directory\\for your images\\";

var ND_FILE = 0;
var ND_NAME = 1;
var ND_LICENSE = 2;
var ND_QUOTE = 3;

var arr = [
["some image filename 1.jpg","Author Name","License Type","Some quote to paste over the image."],
["some image filename 2.jpg","Another Author Name","License Type","Some different quote."],
["some image filename 3.jpg","Some Name","License Type","Third quote."],
];

// store font names
// Note:  show a list of font names available in Illustrator:  var str = "";  for(var i = 0; i < {some number}; i++){str += app.textFonts[i].name + "\n";}; alert(str);
var copyrightFontName = "ChaparralPro-Regular";    

// store document references
var doc = app.activeDocument;
var artb = doc.artboards[0];

// store rectangle sizes.  They do not work as expect, just remember that.
var inrect = Rect(27,-37,artb.artboardRect[2], -artb.artboardRect[3] );
var cliprect = Rect(-inrect[0], -inrect[1], inrect[2]+(inrect[1] * 2), inrect[3]-(inrect[0] * 2));

// store colors:
var blackRGB = new RGBColor();

var baseRGB = new RGBColor(); // #fbb03b
baseRGB.red = 251;
baseRGB.green = 176;
baseRGB.blue = 59;

var greyRGB = new RGBColor(); // 50% grey
greyRGB.red = 127.5;
greyRGB.green = 127.5;
greyRGB.blue = 127.5;

// get the text style
// "MainTextStyle" has to exist in the Illustrator document.  
// One issue in AI JS is that you cannot access a pre-defined style's settings, 
// (e.g., you can apply a "Drop Shadow" using code, but cannot change it from the default settings)
//  so you cannot build your own style in code.
// (Please research creating a new style if you do not know how.)
var mainTextStyle = doc.graphicStyles.getByName ("MainTextStyle");

// variables:
var lyr = null;
var imgClipGroup = null;
var img = null;
var scl = 0;
var clipbox = null;
var borderbox = null;
var maintext  = "";
var copyright = "";

// loop through the images

//for(var idx = 0; idx < 2; idx++)
for(var idx = 0; idx < arr.length; idx++)
{
    // =================
    // create a layer
    // name the layer {name: "woods scene"}
    // move the layer below the foreground
    // =================

    lyr = doc.layers.add();
    lyr.name = "image" + (doc.layers.length - 2);

    // Note: Moving layers DOES NOT work correctly.
    // move the layer below the foreground
    // lyr.zOrder (ZOrderMethod.SENDBACKWARD);  

    // =================
    // place image
    // create a clipping group
    // scale to max: 37p x 27p from artboard edges
    // center
    // =================

    imgClipGroup = doc.groupItems.add();
    imgClipGroup.name = "image, clipped";

    img = imgClipGroup.placedItems.add();
    img.name = "image";
    img.file = File (rootdir + arr[idx][ND_FILE]);
    scl = Math.max(  
        (doc.width - (37* 2)) / img.width,
        (doc.height - (27* 2)) / img.height,
    ) * 100 +1;

    //alert("scale:" + scl + " --- doc.width: " +doc.width + " --- img.width: " + img.width + " --- post img.width: " + (img.width * scl));

    img.resize(scl,scl);
    img.left = doc.width - inrect[2] - inrect[1];
    img.top = doc.height - inrect[3]- inrect[0];

    // =================
    // clipping mask
    // max: 37p x 27p from artboard edges
    // fit to smaller wid/ht, but not to larger
    // clip the image with the box
    // =================

    clipbox = imgClipGroup.pathItems.rectangle(cliprect[0], cliprect[1], cliprect[2], cliprect[3]);
    clipbox.filled = false;

    imgClipGroup.clipped = true;

    // =================
    // border
    // same size as clipping mask
    // fill: none
    // stroke: solid, black, 3pt
    // =================

    borderbox = doc.pathItems.rectangle(cliprect[0], cliprect[1], cliprect[2], cliprect[3]);
    borderbox.name = "border";
    borderbox.strokeColor = blackRGB;
    borderbox.strokeWidth = 3;
    borderbox.filled = false;

    // =================
    // text: "Image © copyright {name} under {license}."
    // font: {font name}, regular, 12pt
    // applied appearance: none
    // position: one (imaginary) line below bottom of border's stroke, flush left to border stroke
    // =================

    copyright = lyr.textFrames.add();
    copyright.name = "text, copyright";

    if(ND_LICENSE == "none" || ND_LICENSE == "" )
    {
        copyright.contents = "Image by " + arr[idx][ND_NAME] + ", no known copyright.";
    } else{
        copyright.contents = "Image ©Copyright " + arr[idx][ND_NAME] + " under " + arr[idx][ND_LICENSE] +".";
    }

    //var str = "";  for(var i = 15; i < 45; i++){str += app.textFonts[i].name + "\n";}; alert(str);
    copyright.textRange.characterAttributes.textFont = app.textFonts.getByName (copyrightFontName);
    copyright.textRange.characterAttributes.size = 14;
    copyright.top = -cliprect[3] +cliprect[0] - 5;
    copyright.left = cliprect[1];
    copyright.textRange.characterAttributes.fillColor = greyRGB;

    // =================
    // text: {phrase}
    // font: {font name}, 56pt, regular
    // applied appearance: 
    // stroke = 1.6 point, offset 1.0666 px, solid black
    // fill = solid, bg color
    // effect = under all, drop shadow, multiply, 100% opacity, x and y offset 5.33 px, blur 12.8 px, color black
    // position: center 
    // =================

    maintext = lyr.textFrames.add();
    maintext.name = "text, main";
    maintext.contents = arr[idx][ND_QUOTE];
    maintext.textRange.characterAttributes.size = 56;
    // copy previously created style:
    mainTextStyle.applyTo( maintext );
    maintext.top = (-cliprect[3] ) / 2 ;
    maintext.left = (cliprect[2]  - maintext.width) / 2 ;

}

// =================
// leave user with control of the placement of the clipped image
// =================
Advertisements