Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Javascript and Photoshop.

Some Photoshop versions ago (actually I can’t tell which Photoshop version featured scripting first) Adobe developed the capability of executing scripts inside Photoshop.

A script is a series of commands that tells Photoshop to perform a set of specified actions, such as applying different filters to selections in an open document. These actions can be simple and affect only a single object, or they can be complex and affect many objects in a Photoshop document. The actions can call Photoshop alone or invoke other applications.

Scripts automate repetitive tasks and are often used as a creative tool to streamline tasks that might be too time consuming to do manually. For example, you could write a script to generate a number of localized versions of a particular image or to gather information about the various color profiles used by a collection of images.

My case was a bit different: I was looking for a background full of numbers to use in the enhanced version of my Flash math game in less than 2KB so I started googling a bit until I stumbled upon a series of images like these ones:

I wanted something like that but all images I found were taken from sites like Depositphotos or similar sites so I told to myself: “are they really charging for some stuff I can do in a couple of minutes”? And I decided to create a Photoshop script to do something similar on my own.

Let me show you how i managed to do a background with numbers:

First, Photoshop lets you choose the language you prefer among VBScript, AppleScript and JavaScript. I used JavaScript because it’s the only one working both on Windows and Apple machines.

Then, Adobe provides a tool called ExtendScript Toolkit you can find in your Program Files -> Adobe -> Adobe Utilities folder and which allows you to test your scripts on the fly, but you can use your favourite text editor as long as you save your scripts with jsx extension then load them in Photoshop with File -> Scripts -> Browse.

So, I made a script to write 1000 random numbers in random positions with a random color and a random alpha, with this script:

// setting pixels as unit of measurement
app.preferences.rulerUnits = Units.PIXELS;
// creating a new 700x520 pixels document
var doc = app.documents.add(700,520);
// preparing to write down 1000 numbers
for(i=1;i<=1000;i++){
    // defining a new random color
    var textColor=new SolidColor();
    // a completely random color
    textColor.rgb.red  = Math.round(Math.random()*255);
    textColor.rgb.green =Math.round(Math.random()*255) ;
    textColor.rgb.blue = Math.round(Math.random()*255) ;
    // adding a new layer
    var myLayer = doc.artLayers.add();
    // setting a random opacity from 10 to 50
    myLayer.opacity=10+Math.round(Math.random()*40) ;
    // defining the newly created layer as a text layer
    myLayer.kind = LayerKind.TEXT;
    // randomly rotating the layer from -10 to 10 degrees
    myLayer.rotate(-10+Math.round(Math.random()*20));
    // the text itself
    var myText = myLayer.textItem;
    // defining text size
    myText.size=64+Math.ceil(Math.random()*128);
    // placing the text at a random position
    myText.position = Array(-50+Math.random()*800,-50+Math.random()*620);
    // writing a random number from 0 to 9
    myText.contents = Math.ceil(Math.random()*9);
    // assigning the text the random color created before
    myText.color = textColor;
}

And once executed here’s an example of what I got:

numberz

Now I am able to make virtually any kind of numbers-letters background in a matter of seconds.

Download the source code and the resulting PSD.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.