photoshop scripting

a little bit about photoshop scripting


adobe has provided a scripting plug-in for photoshop since version 7, and later version of photoshop have it built in: [photoshop scripting]
scripts can be loaded through 'File / Automate / Scripts', or more recently, 'File / Scripts'

the following information was written for Photoshop 7.0, but the scripts should still work in recent versions too.

the dialog shows scripts installed in ..Adobe\Photoshop 7.0\Presets\Scripts folder, others can be loaded via Browse button

download and try example: helloWorld.js
scripts are text files, saved with .js extension

user forum:
   ·photoshop scripting

basically everything is an object
some objects contain only properties, called enumerations (close cousins of constants), to provide labels for the various valid values that can be used with the objects' methods
main objects of concern:
   documents, Document, Units
   ArtLayer, LayerKind
   selection, SelectionType
   Region (Array)
   Color

ecmascript convention is to capitalize object names: new Array(), new SolidColor(),
properties and methods start with a lowercase: someArray.length, preferences.rulerUnts, documents.add(), Math.random()
and enumerations are all caps: Units.INCHES

improving drawing performance


notice that the various inspector windows reflect the actions being carried out. while this is cool, it slows down performance considerably. you may want to close all tool windows before executing any large scripts.
note: even after doing this, operations using the setPixel() and especially getPixel() commands will be very slow. i'm sorry.

a canvas utility for photshop scripting


Canvas.js
properties:
   width
   height
   penColor
   penWeight
   opacity
methods:
   setBackgroundColor(normR, normG, normB)
   setOpacity(normO)
   setPenColor(normR, normG, normB)
   setPenWeight(w)
   getPixel(x, y, pixelColor)
   setPixel(x, y, opacity)
   drawLine(x1,y1, x2,y2)
   drawSoftLine(x1,y1, x2,y2, softness)
   drawRegion(filled, region)
   drawSoftRegion(filled, region, softness)

plotting a line


 
include("Canvas.js");

// set dimensions for new document
var w = 200;// units
var h = 140;

// instantiate raster canvas for use
var C = new Canvas(w, h, Units.PIXELS);

// set a color to use
C.setPenColor(0, 0, 0);

// plot a line
var opacity = 1;
var x = 0;
var y = 30;
// plot while we're on the canvas, then stop
while ((y < h) && (x < w)) {
      // inverse y value to plot graph right-side up
      C.setPixel(x, y, opacity);
      // increment x
      x++;
      // calculate new y for loop check
      // as per the graph we're plotting-- y = f(x)
      y = 30;
}

// release object referencess
C = null;
	

y = 30

more lines


 
y = x
[yx.js]

y = x

array example


points and regions
the photoshop scripting environment uses an array containing two values to represent a point:

// x and y are the horizontal and vertical coordinates of the point
pointA = new Array(x, y);

an array of points defines a region (like a slection you might make with the lasso tool)

var p1 = new Array(12, 17);
var p2 = new Array(35, 19);
var p3 = new Array(38, 92);
var p4 = new Array(32, 95);
var squareishRegion = new Array(p1, p2, p3, p4);

the drawregion() and drawSoftRegion() methods offered by the Canvas object accept a region as an input parameter defining the area to be filled.

emergent patterns


by focusing on controlling the individual elements of a system, rather than the whole, behavior and patterns can emerge

normalize(x * y)
area.js
distance from a point
distance.js
arbitrary combinations
maeda.js

polar coordinate examples


simple circle
circlePlot.js
loops and flowers
petals.js
spirals
spiralDouble.js

tiling


use mod to divide a whole unit into tiny versions of itself

tiling the color plot from above, by defining a tile size, and then modding the values of x and y by their respective tiles sizes
coverTile.js