Creating your own backgrounds with Adobe Photoshop Scripting

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers