Monday, March 19, 2007

PhotoShop Scripting

You’ve probably wanted at one time or another to combine the power of Photoshop’s tools to those of programming scripts. Adobe has made this possible for you with the ‘Scripting Plug-in’, a nifty plug-in that allows users to write little scripts (in Javascript, VBScript or AppleScript) that will take over Photoshop and do what you couldn’t with you mouse and keyboard…

This tool is also aimed at designers which wish to increase the workflow speed (you'll see example scripts by the end of the tutorial which allow you to save all your current images, or save each layer to a separate file etc...). Even though we do use a scripting language, it is not hardcore programming and designers which hate programming should not be scared of this, the step by step tutorial will help you create your first simple scripts and you will later be able to move on to more advanced scripts.




Photoshop scripting is much more than what actions and batching can do; for example you can use the ‘if…then’ commands which already multiplies possibilities… Imagine you have a text database, hundreds of pictures, and want to dynamically create business cards – seems tough ? Scripting will do it. It is a unique and excellent tool to automate your needs and save you time, or create something that a human and a mouse would take ages to do.

This tutorial will, through a series of projects, demonstrate some of the capabilities of scripting. This tutorial is aimed at intermediate/advanced users who use Photoshop extensively. I assume you know about batching & actions, and most of Photoshop already. The tutorial is set this way: a) example of what we will create. b) full code c) code explanation... So don't worry if at first you only see a bunch of scripted lines.

All you'll need for that tutorial is a text file editor, Photoshop and to install the plug-in


Contents:
I have broken this tutorial into many sections, start from the beginning if you have never met scripting.




I. Preliminary steps

If you have a version of Photoshop CS, you can skip the downloading instructions below, since the scripting plug-in is installed with CS by default. The tutorial uses scripting with Photoshop 7 – some scripting capabilities have been improved since then, so don’t be alarmed if you CS version can do in two steps what I’ve scripted in ten :P… And, on a sidenote,

Downloading instructions: you can download the plug-in for version 7 of PS on Adobe’s web site by searching for ‘scripting plug-in’ – the latest links were here (for Mac) or here (for PCs). Installation will create a ‘Scripts’ folder in the ‘Presets’ folder of Photoshop.
   


Getting familiar with scripting: you can now check your scripts; which you can test by using the menu File>Automate>Scripts (you can try to alt/apple click in the dialog box to run your script with a debugger)
(Note
Edit your scripts with simple text applications (textpad) otherwise Photoshop might have trouble processing them …)


I will use ONLY JavaScript in my examples because it’s the only cross-platform language. Keep in mind that the same scripts can be written in VBScript and Applescript (and much more, since you can script more applications and call them from these scripts, and not in JS) but the syntax is a bit different (check the doc for changes).


III. Using Text Layers and Saving Files


Creating and editing text options, saving your file.











It already looks a tad more interesting doesn't it ?

The keys in doing it are:

  • Opening a new 600x900px canvas

  • Creating an array full of text

  • Creating a loop that will create text layers according to the array

  • Setting specific color & text size for each text layer

  • Rotating with a 45? angle each layer

  • Saving the document

  • Closing the document
i. Code (explained below)




  • ((     var defaultRulerUnits = preferences.rulerUnits;
    preferences.rulerUnits = Units.PIXELS;

    bgGrey = new SolidColor();
    var lightness = 255 - Math.round(Math.random()*100);
    bgGrey.rgb.red = lightness;
    bgGrey.rgb.green = lightness;
    bgGrey.rgb.blue = lightness;

    backgroundColor = bgGrey;

    var newDocumentRef = documents.add(600,900, 72.0, "Working With Text",DocumentMode.RGB, DocumentFill.BACKGROUNDCOLOR);
    newDocumentRef = null;

    var textArray = [
    "Silence prevailed as",
    "those mochi balls",
    "screamed in agony",
    "and unbearable disbelief",
    "…"
    ] ;

    var AD = activeDocument ;

    for(a=1;a<=textArray.length;a++){
    var TextLayer = AD.artLayers.add();
    TextLayer.kind = LayerKind.TEXT;
    TextLayer.opacity = Math.round(Math.random()*50)+50;
    //TextLayer.name = textArray[a-1];
    var txtRef = TextLayer.textItem;
    txtRef.font = "Impact";
    txtRef.contents = textArray[a-1];
    txtRef.size = Math.round(1/(Math.random()*0.2+0.02))+10;
    var textPosition = [0,Math.round(Math.random()*880)+10];
    txtRef.position = textPosition; TextLayer.rotate(-45)
    }

    AD.flatten();

    activeDocument.crop(new Array(25,25,600,800), 0, 600-25, 800-25, 72);

    saveFile = new File("//scriptingTest.jpg");
    saveOptions = new JPEGSaveOptions();
    saveOptions.embedColorProfile = true;
    saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    saveOptions.matte = MatteType.NONE;
    saveOptions.quality = 9;

    AD.saveAs(saveFile, saveOptions, true,Extension.LOWERCASE);
    AD.close(SaveOptions.DONOTSAVECHANGES) ;

    preferences.rulerUnits = defaultRulerUnits;
    ) )

Well I hope the script brought some light to you. Make sure to play with it, invent new crazy scripts.











[ Here's what a modified version of the script can do. Check it out !]


1 comment:

Mayank Verma said...

Its good and showing morphing in photoshop .Impressive!