Photoshop Automation using JavaScript #3 — Working with Data

Rishabh Gupta
3 min readJun 7, 2020

--

This is the third and final part in the Photoshop Automation series. In this post we will go through how to work with data in photoshop and export multiple documents of the same layout with varying content.

  1. The Setup
  2. First Script: “Hello Photoshop!”
  3. Working with Documents
  4. Working with Layers
  5. Exporting Documents
  6. Working with Data
  7. Putting it all together: The Avengers
  8. References

Working with Data

Before we move on to create the first useful automation script, we need to be able to read data from files into our script. One of the most common uses cases of photoshop automation is creating multiple files of the same design with different data. In our Avengers script, we will read the cast of the movie from a JSON file. Download data file from here.

In order to read this file, we will first open this file in read mode and store the JSON data in a variable.

Javascript comes with JSON parser by default, however, while writing scripts for photoshop we will have download and include it explicitly. Download the JSON parser from here.

// Include the json parser
#include "json2.js"
const jsonFile = File("cast.json");// Open the JSON file in read mode
jsonFile.open ('r');
// Store the data
const data = jsonFile.read();
// Close the file as we no longer need it
jsonFile.close();
// Use the parser to perse json data
const castData= JSON.parse(data);
alert(castData['cast'][0].name);

When this script is executed, a photoshop alert dialog should open with the text “Robert Downey Jr”.

Putting it all together: The Avengers

By now you have equipped yourself with the skills required to automate repetitive tasks in Photoshop. Lets put them into action by creating a script that would read a list of cast members of Avengers from a JSON file and for each actor create a card consisting of the actor’s name followed by the character’s name. Download the base PSD file from here and open it in photoshop.

#include "json2.js"// Suppress dialogs
app.displayDialogs = DialogModes.NO;
// Read cast data from json file
const jsonFile = File("cast.json");
jsonFile.open ('r');
const data = jsonFile.read();
jsonFile.close();
// Close the file as we don't need it anymore
var castData= JSON.parse(data);
castData = castData['cast'];
// Export JPEG Configuration
var jpegConfig = new JPEGSaveOptions();
jpegConfig.quality = 10;
var document = app.activeDocument;
var nameTextLayer = document.layerSets.getByName('TEXT').layers.getByName('actorName');
var characterTextLayer = document.layerSets.getByName ('TEXT').layers.getByName('characterName');
for(var actor = 0; actor<castData.length; actor ++)
{
// For each actor create a jpeg file
var filename = 'path' + castData[actor]['character'] + '.jpg';
var file = File(filename);
// Set text of the name layer to actors name
nameTextLayer.textItem.contents = castData[actor]['name'];
// Set text of character layer
characterTextLayer.textItem.contents = castData[actor ['character'];
// Save JPEG file
document.saveAs (file, jpegConfig);
}

When this script is executed, 12 JPEG files should get created in the specified path.

References

This is just a small example of what can be accomplished through scripting in Photoshop, check out the following links for more.

--

--

Rishabh Gupta

Tech Lead @moengage. My expertise and interest primarily lie in Photoshop, JavaScript, AngularJS, ReactJS, Python, MongoDb, AWS & Photography