Adobe Photoshop: programming for designers – Introduction

with No Comments

March 2019

This article is an introduction to a series of articles about programming for ‘Adobe Photoshop’. As ‘Adobe Photoshop’ is mainly used by graphic designers I will first tough upon a few basic concepts and programming principles. This won’t be an extensive programming course, because there are already a lot of courses how to learn programming.

This series of articles will learn you what tools to use and how to make scripts that help you the most.

How to automate ‘Adobe Photoshop’

 

Actions

Next to programming I also use ‘Adobe Photoshop’ a lot. And being a programming I always try to automate repeating tasks. ‘Adobe Photoshop’ facilitates several mechanisms to do this. The most known one is the use of ‘Actions’. An ‘Action’ can be recorded during the actual execution of the action. E.g. When I resize and save an image, I can record this as an ‘Action’. By replaying the action for another image, it will resize and save that particular image. Actions are very useful when a lot of steps needs to be executed in a particular order.

Scripting

An even more flexible way to automate tasks in ‘Adobe Photoshop’ is by programming them. This is less known way of doing it. I am using ‘Adobe Photoshop’ for more than 15 years and until a few years ago I never realized that this was possible. I never considered this even being possible.

Benefits of using javascript

As said, scripting gives the user a lot of extra advantages:

  1. Changing a javascript file is a lot easier than making changes to a complicated action
  2. A programming language gives you more flexibility in making a set of output files with slight, known variations.

    E.g. save a set of files for the same image use different exposure settings

‘Adobe Photoshop’ supports the use of three programming languages ‘AppleScript’, ‘VBScript’ and ‘javascript’.
The first two are platfrom specific, but javascript can be used on both Apple and Windows. In these articles I will concentrate on javascript.

Contrary to the actions, javascript files cannot be created with ‘Adobe Photoshop’. Javascript is written in a flat-file format. You can write your javascript files in ‘Notepad’ or a similar program. Programs like ‘Word’ or not suitable, because they also store layout features, like underlining.

The javascript file can be called from ‘Adobe Photoshop’ by selecting:

  1. Select File – Scripts – Browse…
  2. Locate your script and execute it

Writing your script in ‘Notepad’ can work well, but probably only for short scripts, where there is a small change of errors.
Once the scripts get longer there is more change of errors in the script and another program will suit the task better.

Before I go into the details about the tools that can be used, a short explanation about errors for non-programmers.

Errors

There are two types of errors.

  1. The first one is we give the wrong commands. E.g. we want to resize the width, but the command we use resizes the height. The script will be executed, but the end result is not what we want.
  2. The second one is we want to resize the width, but we make typo and instead of ‘width’ we type ‘wdth’. During execution the script will stop when it reads ‘wdth’, because it doesn’t know what to do.

Especially when your scripts get longer the first type of error will be more difficult to solve. The second type of error will be easier, because when the script is executed it will stop and give some information about the error. This makes it easier to track.

Adobe ExtendScript Toolkit

Software developers have to track errors every day, because writing code is a gradual process and it involves making errors. Over time tools are developed to make it easier to track errors. The mechanism of tracking and solving errors is called debugging. When I started looking into programming for ‘Adobe Photoshop’ I first tried to find a tool that could help me with debugging my scripts. I found one close to home. Adobe has developed ‘Adobe ExtendScript Toolkit’. This toolkit is developed especially for writing scripts for Adobe products. It is a basic developer’s tool, but it most importantly it facilitates debugging.

The ‘Adobe ExtendScript Toolkit’ doesn’t come with the latest versions, but you can still install it and it works fine with your running version of ‘Adobe Photoshop’ or other Adobe products.

If you are interested in using scripts in ‘Adobe Photoshop’ I recommend installing the ‘Adobe ExtendScript Toolkit’
It is a bit hidden away, but the steps below will help you to install it.

  1. Open ‘Adobe Creative Cloud’
  2. Open ‘Preferences’ – ‘Creative Cloud’
  3. Check ‘Show Older Apps’
  4. Open the ‘Apps’ tab

    The ‘ExtendScript Toolkit’ should now be showing.
  5. Install Adobe ExtendScript Toolkit

In the next articles, or videos, I will dive deeper into the ‘Adobe ExtendScript Toolkit’ and how to use the debugging facility.
If you want to play around a bit yourself you can use this code:

var doc = activeDocument; 

if(doc.width.value > doc.height.value){ 
    alert("This is a landscape image"); 
} 
else { 
    alert("This is a portrait image"); 
}

Copy it in a text file and save it as ‘detectOrientation.jsx’
Now, you can run it in ‘Adobe Photoshop’ as described above, or you can run it in the ‘Adobe ExtendScript Toolkit’:

  1. Open ‘Adobe Photoshop’
  2. Open an image file
  3. Open ‘Adobe ExtendScript Toolkit’
  4. Open the detectOrientation.jsx file
  5. Connect the Toolkit to Photoshop, by selecting ‘Photoshop’ in the left selectbox.

    The tooltip on the icon next to the selectbox says ‘Click to connect to target application’
  6. Run the script by pressing the green playbutton.

    The tooltip on the buttons says ‘Start running the script’

If you want to prepare a bit for programming itself. These two pages describe the use of a ‘conditional statement’ and the use of ‘functions’

If-else
Functions