Jan
10
photoshop logo

Photoshop Scripting

I needed one day to make a mirror effect (you know when you place two mirrors one in front of the other and look at them) for 50 photos and started to make them by hand in my PS. I soon give up because it was taking to long and tried to find a solution that will be more easy to use and more quicker. Lucky me that Adobe thought of this kind of problems and offers us two ways to solve this issue.

The Methods

The first one: ACTIONS are very easy to use directly from PS. No programming required, no nothing, just record what you want to do and then apply that steps to a bunch of photos.

Another way is to use SCRIPTING is PS to achieve our desired output. Basically PS let’s us use scripts to automate most of the processes that we make by hand.

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…

Why use scripts instead of actions?

First of all because this is what my article is about, scripting, but in a more serious manner we use scripts because they let use extend the great benefits of actions by allowing you to add functionality that is not available for PS Actions. For example, you can do the following with scripts but you can’t do it with actions:

  • Conditional logic, very important if you need to take a decision based on your current situation. For example, you could write a script that decides which color border to add depending on the size of the selected area in an image.
  • You can open, save, and rename files using scripts.
  • You can copy scripts from one computer to another. If you were using an Action and then switched computers, you’d have to recreate the Action.
  • A single script can perform actions that involve multiple applications. For example, depending on the scripting language you are using, you could target both Photoshop and another Adobe Creative Suite Application, such as Adobe Illustrator, in the same script.
  • Scripts provide more versatility for automatically opening files. When opening a file in an action, you must hard code the file location. In a script, you can use variables for file paths.
  • You can create minimalistic (or complex) forms to help you input your options or preferences.

Don’t get me wrong actions are not an obsolete feature, you need to know what you have to do first and according to each one’s limitation you can adopt a method or another.

Photoshop Scripting Languages

ps-scripting-open-script

Photoshop supports 3 scripting languages: AppleScript, VBScript, and JavaScript. There are 3 of them because many reasons that I won’t talk about right now and because AppleScript and JavaScript runs on Mac OS and VBScript and JavaScript runs on Windows.

In our article we will use JavaScript. In PS in order for a JavaScript file to be recognized as a valid file it must have either .js or .jsx as an extension. It doesn’t matter which one but in Windows if your file has .jsx and you double click it will be automatically recognized by Photoshop, so it’s better to name this files with this extension format.

To execute a script directly from Photoshop just go to File \ Scripts \ Browse and select the file where the scripting was done. Another way to execute scripts is to place them in the PS folder PATH_TO_ADOBE_PS\Presets\Scripts\ and now your scripts will be automatically loaded and you can access them from File \ Scripts \.

How to make a Mirror Mirror Effect

First of all let’s create a file where we will code our script called: PSMirrorMirror.jsx Now we need to know what is the object model so we can work with it in Photoshop.

psmirrormirrorexample

The Photoshop DOM consists of a hierarchical representation of the Photoshop application, the documents used in it, and the components of the documents. The DOM allows you to programmatically access and manipulate the document and its components. For example, through the DOM, you can create a new document, add a layer to an existing document, or change the background color of a layer. Most of the functionality available through the Photoshop user interface is available through the DOM.

Knowing this let’s take it step by step, what we need to do is a mirror mirror effect, for the simplicity of thing I’m just gonna imagine how I will do this by hand and then try to translate it into scripting.

  1. Open the image in a new layer
  2. Select all using CTRL+A
  3. Copy the selection and paste it to create a new layer CTRL+C > CTRL+V
  4. Resize the new layer to be smaller
  5. Center in the middle of the document

Repeating this steps for each new layer we achieve the same effect as in the example above. Now let’s translate this into scripting.

function createMirrorMirror()
{
  var docRef = activeDocument;
  var layerRef = null;
  var noOfMirrors = 5;
}

We created a function caled createMirrorMirror and instanced 3 variables.

  • docRef – is a reference to our active document from PS, with this we will be able to call other functions to manipulate that document.
  • layerRef – is a reference to the active layer from our document, with this we will be able to resize only the layer from our active document.
  • noOfMirrors – the number of mirrors we want to create inside our document.

No we need to create 5 layers (because our noOfMirrors is 5) with the same image and make them smaller just as we did in the previous steps by hand.

for(var i=1; i<=noOfMirrors; i++)
{
    docRef.selection.selectAll();
    docRef.selection.copy();
    docRef.paste();

    layerRef = app.activeDocument.artLayers[0];
    layerRef.resize(80,80,AnchorPosition.MIDDLECENTER);
}

ps-scripting-layersresult Using the docRef we call a function defined by the PS scripting API and simulate the CTRL+A effect using docRef.selection.selectAll(), we copy the contents and then paste it over the document. This will generate a new layer in PS with the same content as the source.

The next step is to resize the new layer and center it, and this is done by calling resize from our layer reference layerRef. The first two parameters are the percents for the the new size width and height, so enetering lower the 100% values will shrink the layer and bigger values will enlarge the layer. And the last one is the position of the new layer, in our case will be MIDDLECENTER.

All this is done 5 times in our for iteration and the resulted document will be a 5 layer document as above.

That is it, easy right? Actually is very easy to script using the Photoshop API and the results are very good once you get the hang of it.

The final script should look like this:

function createMirrorMirror()
{
  var docRef = activeDocument;
  var layerRef = null;
  var noOfMirrors = 5;

  if (noOfMirrorsDlg.result == 1)
  { // OK
    noOfMirrors = noOfMirrorsDlg.noText.text;
  }

  for(var i=1; i<=noOfMirrors; i++)
  {
    docRef.selection.selectAll();
    docRef.selection.copy();
    docRef.paste();

    layerRef = app.activeDocument.artLayers[0];
    layerRef.resize(80,80,AnchorPosition.MIDDLECENTER);
  }
}
// call the main function
createMirrorMirror();

Now save this script in our PSMirrorMirror.jsx and you can call it every time you want from File \ Scripts \ Browse or copy the file to PATH_TO_ADOBE_PS\Presets\Scripts\ and you will be able to select it every time you need it from the Scripts menu.

The full script I made can be downloaded from GIT-Hub here.

For further reading and for a complete guide to Photoshop scripting visit this resources sites:

I hope you liked the article and learned a little bit on how to use scripting in Photoshop.

If you like this post, share it with your friends!

You can leave a response, or trackback from your own site.

3 Responses to “Adobe Photoshop Scripting Guide – Mirror Effect”

 
  1. arthritisguy says:

    I run a small photo studio and i also work as a web designer and Adobe Photoshop has been the bread and butter of my job. Photoshop is really the best photo editing tool that i have ever used.

  2. Radu says:

    Thanks guys :) I will be following with some other examples in a few days so keep an eye on this.

 

Leave a Reply