Introduction To Photoshop Scripting

Advertisement

Automation is useful in the work of every designer. It saves precious time on repetitive tasks and helps us solve certain problems more quickly and easily.

You can automate your workflow in Photoshop with actions, which are pretty popular and which most of you already know about and use. Today, we’ll introduce you to an advanced automation technique: scripting. All you need for this is basic knowledge of JavaScript, which some of us Web designers already have.

I’ve known about Photoshop scripts for years but decided to really dive in a few months ago. I had avoided it because I thought it was the domain of smart math-minded programmers. I was wrong, and today I’ll show that, although it requires some basic programming skills, scripting isn’t hard to grasp.

But first, we have to answer the obvious question.

Why Do We Need Scripts?

Why should we would learn to script if Photoshop already has pretty nice actions? The answer is interactivity. When you use an action, you can’t really control how it behaves in different situations; it is like a videotape that just keeps playing again and again without any change.

Why We Need Scripts?

A script is more dynamic; its behavior changes according to the parameters you input or the context of its application. Sounds useful, no?

Requirements

You don’t have to be an advanced programmer to be able to write scripts; I’m just a graphic designer, like most of you. But you should at least have a basic understanding of JavaScript and some experience with properties and methods to get the most out of this article.

Requirements

If you are not familiar with JavaScript at all, fear not! There are plenty of places to learn the basics of programming. Codecademy1, for example, has pretty neat interactive lessons.

I work in Adobe Photoshop CS5, but everything we’ll cover applies to newer versions, too; Adobe hasn’t made any major updates to its scripting API since CS5. I will refer to the latest version of the scripting documentation, though, which is CS6.

Getting Started

When you record actions in Photoshop, you set the order of the steps to achieve a certain result — that’s your algorithm. Then, you press “Record” and replicate them in Photoshop one by one. Scripting is similar, but instead of doing these steps in Photoshop, you write them down as lines of code. Most actions that you do in Photoshop have their own script equivalent as a function.

Start here

Let’s say you are creating an action that scales a document to 150% of its original size. You’d go through these steps:

  1. Open Image → Image Size.
  2. Enter 150% in width and height.
  3. Hit “OK.”

The same process with a script would look like this:

  1. Call the application: app
  2. Target a document: activeDocument
  3. Call the function to resize the image: resizeImage(width, height)

And the code would look like this:

app.activeDocument.resizeImage("150%", "150%");

Language

There are three ways to write scripts for Photoshop: using AppleScript on Mac, VBScript on Windows or JavaScript on either platform. I use the third because it is cross-platform and I already have some experience with it.

Tools

Adobe has its own utility for writing scripts, called ExtendedScript Toolkit.

Adobe ExtendedScript Toolkit2
The main window for Adobe’s ExtendedScript Toolkit. (View large version.3)

The toolkit comes with Photoshop, and you can find it in the following folder:

  • Mac OS X
    /Applications/Utilities/Adobe Utilities CS6/ExtendScript Toolkit CS6/
  • Windows
    C:Program FilesAdobeAdobe Utilities - CS6ExtendScript Toolkit CS6
    (or Program Files (x86) for 64-bit machines)

The user interface of the ExtendedScript Toolkit is pretty straightforward. To start writing scripts, first select the target application in the drop-down menu. If Photoshop is running, then look for the green chain icon near the drop-down menu:

Application Select

Now you can write something like this:

alert("Hello Photoshop!");

Press cmd + R (or just hit the “Play” button in the toolbar) to run your script. ExtendedScript Toolkit should switch to Photoshop and show an alert box:

Hello Photoshop!

ExtendedScript Toolkit has some other neat features for debugging scripts, but this is enough for this article. You can learn more about how to use it by going to Help → JavaScript Tools Guide.

You can use any plain-text editor to write a script; just save it as a .jsx file. To run it, you’ll have to go to File → Scripts → Browse in Photoshop and select it. Alternatively, just open the script file with Photoshop. You can also add a line of code at the top of the script so that the file always opens in Photoshop:

#target photoshop

Save your scripts in the Photoshop/Presets/Scripts/ directory, and access them with File → Scripts. You can also set up a hotkey; just go to Edit → Keyboard Shortcuts, navigate to File → Scripts → [your script’s name], and set the shortcut you want.

ExtendedScript Toolkit can run and debug code from the integrated development environment, and it has an object model viewer built in, which is useful. So, I recommend using the toolkit to write your scripts. Unfortunately, the Mac version crashes sometimes, so keep that in mind.

Photoshop Object Model

To make writing scripts easier, you should understand how things relate to each other in Photoshop’s Document Object Model (DOM). Understanding it is not so hard if you look at Photoshop itself. The main object in Photoshop’s DOM is the application. In the application, we have a collection of documents that are currently open in Photoshop.

Each document contains elements — such as layers (called ArtLayers), groups of layers (LayerSets), channels, history states and so on — just like in a regular PSD document.

A simplified visualization of Photoshop’s DOM is below. A more detailed containment hierarchy can be found on page 12 of “Adobe Photoshop CS6 Scripting Guide144” (PDF).

Simplified visualization of Photoshop API
A simplified visualization of Photoshop’s DOM.

Each of these objects has its own properties and methods that you can work with. For example, to change the opacity of the selected layer in a document, you would go to Application → Document → Layer → Opacity and set the desired value. The code would look like this:

app.activeDocument.activeLayer.opacity = 50;

As you may have guessed, activeDocument and activeLayer determine the currently selected document and layer.

You can find descriptions of most objects and their properties and methods in “Adobe Photoshop CS6 JavaScript Scripting Reference5” (PDF), or in ExtendedScript Toolkit by going to Help → Object Model Viewer.

Let’s see how this works in a real-world example. In this next section, we’ll write our own script based on an action.

Remastering The RotateMe Action As A Script

A few years ago at Christmas time, I had an idea for an action to help me draw snowflakes.

Drawing Snowflake 101

  1. Draw one stem of the snowflake with a pattern.

    Step One

  2. Duplicate the stem, and rotate it a few degrees.

    Step Two

  3. Repeat the second step until you have a full circle.

    Step Three

Duplicating and rotating each stem manually is tedious, so I came up with an action to automate it6. The algorithm looks like this:

  1. Duplicate the stem.
  2. Rotate it by however many degrees you’ve chosen, using the Transform tool.
  3. Duplicate the layer.
  4. Use the “Repeat Transform” function.
  5. Repeat steps 4 and 5 until you have a full circle.

Pretty neat. But the action had a disadvantage: You can set only a certain number of stems for the snowflake, according to the number of degrees you set in third step of the algorithm.

Back when I wasn’t familiar with scripting, I just made a few versions of the action, each of which produced a snowflake with a different number of stems.

Today, we will remaster this action as a dynamic script that takes your input on the number of stems. Let’s get started!

Algorithm

When you start writing a script, defining the algorithm first before digging into the code itself is always a good idea. In our case, the algorithm will work like this:

  1. Ask the user to enter the number of stems.
  2. Calculate the rotation angle.
  3. Duplicate and rotate the layer by the number set in the first step.

Let’s start with saving the current or selected layer to a variable for further use:

// Save selected layer to variable:
var originalStem = app.activeDocument.activeLayer;

Note that in JavaScript, you can mark a line with double slashes (//) to make it a comment. Comments are used to describe parts of code for future reference and don’t affect the behavior of the script.

Let’s move on to our algorithm now.

1. Ask User for Input

We can take input from the user with the prompt(message, default value[, title]) function. This function shows a dialog box with the message and an input field that contains the default value. When the user hits “OK,” the function returns the inputted value; so, we have to save it to the variable to be able to be used.

// Ask user for input by showing prompt box and save inputted value to variable:
var stemsAmount = prompt("Processing ""+originalStem.name+""nHow many stems do you need?", 12);

Note that I used originalStem.name in the message, so the dialog box will show the name of selected layer.

On Mac OS X, the first line of the message is in bold and functions as the title. So, our main message should be on the second line. To make a new line, type n.

In Windows, you can specify a third argument in the function to set the title:

// Ask user for input by showing prompt box and save inputted value to variable:
var stemsAmount = prompt("How many stems do you need?", 12, "Processing "+originalStem.name);

If we run the code in Photoshop, it will show this dialog box:

Prompt dialog

When the user hits “OK,” the inputted value will be saved to the stemsAmount variable. If the user clicks “Cancel,” then the function will return a null value. We’ll use this later.

2. Calculate the Rotation Angle

To calculate the rotation angle, we have to divide 360 degrees (a full circle) by the number of stems:

// Calculate the rotation angle
var angle = 360 / stemsAmount;

3. Duplicate and Rotate

Now we have everything we need to make duplicates of our stem. To do this, we’ll use the for7 loop. It lets us repeatedly run lines of code as many times as we’d like. Our loop will look like this:

for(var i = 1; i < stemsAmount; i++){
	// This code will run "stemAmount - 1" of times
};

Note that the first instance of an object in programming has the value of 0, but because our first layer is already on the canvas, we’re starting the loop from 1 instead.

To duplicate and rotate our layer, we will use the duplicate() and rotate(angle, AnchorPosition) methods: the number of layers to be rotated in angle multiplied by the indexed number of duplicates. AnchorPosition determines the point around which the layer will rotate. You can see this point when you use the Transform tool in Photoshop — it looks like a small circle with a crosshair. In scripting, it has only 9 specified values — i.e. the 9 positions of the anchor point:

AnchorPosition visualization

In our case, it is the bottom center of the layer, BOTTOMCENTER. Photoshop uses a lot of other constants here and there in some of the functions, which you can find on page 197 of “Adobe Photoshop CS6 JavaScript Reference108” (PDF).

So, our loop will look like this:

// Duplicate and rotate layers:
for(var i = 1; i < stemsAmount; i++){
	// Duplicate original layer and save it to the variable 
	var newStem = originalStem.duplicate();

	// Rotate new layer
	newStem.rotate(angle * i, AnchorPosition.BOTTOMCENTER);
};

And the completed code will look like the following. You can try to run it.

// Save selected layer to variable:
var originalStem = app.activeDocument.activeLayer;

// Ask user for input by showing prompt box and save inputted value to variable:
var stemsAmount = prompt("Processing ""+originalStem.name+""nHow many stems do you need?", 12);

// Calculate the rotation angle:
var angle = 360 / stemsAmount;

// Duplicate and rotate layers:
for(var i = 1; i < stemsAmount; i++){
	// Duplicate original layer and save it to the variable
	var newStem = originalStem.duplicate();

	// Rotate new layer
	newStem.rotate(angle * i, AnchorPosition.BOTTOMCENTER); 
};

Final Touches

I’ll usually try to achieve the main goal with a script, and when everything works correctly, I’ll start to refine the code. In our case, we have to make sure that the user inputs a valid number in the prompt box — i.e. a positive integer, greater than one.

Also, to prevent Photoshop from going crazy, we will restrict the number of stems — let’s say, to 100. To do this, we will use a while9 loop to show the user an error message in the event of an invalid submission, and the prompt box will continue to be shown until the user enters a valid value or hits the “Cancel” button (remember that the prompt returns null if the user hits “Cancel”).

The new code looks like this:

// Save selected layer to variable:
var originalStem = app.activeDocument.activeLayer;

// Ask user for input by showing prompt box and save inputted value to variable:
var stemsAmount = prompt ("Processing ""+originalStem.name+""nHow many stems do you need? (From 2 to 100)", 12);

// Check that user entered a valid number and, if invalid, show error message and ask for input again
while(isNaN(stemsAmount) || stemsAmount <= 0 || stemsAmount > 100){
	// If user clicks "Cancel" button, then exit loop
	if(stemsAmount == null) break;

	// Show error message…
	alert("Please enter number in range from 2 to 100");
	// …and ask for input again
	stemsAmount = prompt("Processing ""+originalStem.name+""nHow many stems do you need? (From 2 to 100)", 12);
};

// Run the copying process
if(stemsAmount != null){ 
	// Calculate the rotation angle
	var angle = 360 / parseInt(stemsAmount);

	// Duplicate and rotate layers:
	for(var i = 1; i < stemsAmount; i++){
		// Duplicate original layer and save it to the variable
		var newStem = originalStem.duplicate();

		// Rotate new layer
		newStem.rotate(angle * i, AnchorPosition.BOTTOMCENTER);
	};
};

As you may have noticed, we’re using the isNaN(value) function, which returns true if value is “not a number” and parseInt(value) to convert the value to an integer when we calculate the rotation angle.

The next thing we will do is manage the layers, renaming our new layers by adding an index to them. Also to make sure that we do not mess up document’s layers, let’s place our stems in a group.

Renaming the layers is not a hard task. We will just use the name property of the layer and add an index number to it:

// Add index to new layers
newStem.name = originalStem.name + " " + (i+1);

A group in Photoshop’s API is called a LayerSet and we can access all groups of the document by calling the layerSets property. To add a new group to a document, we have to call the layerSets’ method add():

// Create a group for stems
var stemsGroup = app.activeDocument.layerSets.add();
	stemsGroup.name = originalStem.name + " ("+stemsAmount+" stems)";

Then, to add a layer to the group, we will use the move(relativeObject, ElementPlacement) function. Note that the move() function moves a layer in the layer stack, not on the canvas. (You can move a layer on the canvas with the translate(deltaX[, deltaY]) function.)

ElementPlacement is another constant, this one determining how we will place our layer relative to… well, relativeObject. In our case, we will use ElementPlacement.INSIDE to place the original layer inside a group:

// Place original layer in group
originalStem.move(stemsGroup, ElementPlacement.INSIDE);

We will place each new copy of the layer at the bottom of all layers in the group using ElementPlacement.PLACEATEND. The result is all of our layers arranged in ascending order, the first layer at the top and the last at the bottom:

// Place new layer inside stems group
newStem.move(stemsGroup, ElementPlacement.PLACEATEND);

You can read more about the ElementPlacement constant on page 202 of “Adobe Photoshop CS6 JavaScript Reference108” (PDF).

Final Code

That’s it! RotateMe.jsx is done. Our final code looks like this:

// Save selected layer to variable:
var originalStem = app.activeDocument.activeLayer;

// Ask user for input by showing prompt box and save inputted value to variable:
var stemsAmount = prompt ("Processing ""+originalStem.name+""nHow many stems do you need? (From 2 to 100)", 12);

// Check that user entered a valid number and, if invalid, show error message and ask for input again
while(isNaN(stemsAmount) || stemsAmount <= 0 || stemsAmount > 100){
	// If user clicks "Cancel" button, then exit loop
	if(stemsAmount == null) break;

	// Show error message…
	alert("Please enter number in range from 2 to 100");
	// …and ask for input again
	stemsAmount = prompt("Processing ""+originalStem.name+""nHow many stems do you need? (From 2 to 100)", 12);
};

// Run the copying process
if(stemsAmount != null){ 
	// Calculate the rotation angle
	var angle = 360 / parseInt(stemsAmount);

	// Create a group for stems
	var stemsGroup = app.activeDocument.layerSets.add();
		stemsGroup.name = originalStem.name + " ("+stemsAmount+" stems)";
	// Place original layer in group
	originalStem.move(stemsGroup, ElementPlacement.INSIDE);

	// Duplicate and rotate layers:
	for(var i = 1; i < stemsAmount; i++){
		// Duplicate original layer and save it to the variable
		var newStem = originalStem.duplicate();

		// Rotate new layer
		newStem.rotate(angle * i, AnchorPosition.BOTTOMCENTER);

		// Add index to new layers
		newStem.name = originalStem.name + " " + (i+1);

		// Place new layer inside stems group
		newStem.move(stemsGroup, ElementPlacement.PLACEATEND);
	};

	// Add index to the original layer
	originalStem.name += " 1";
};

That wasn’t too hard, was it?

Finish

Now you can put it in the Photoshop/Presets/Scripts/ folder and run it by going to File → Scripts in Photoshop. Using different shapes with different values can yield interesting results:

Conclusion

As you can see from the number of links in the resources section below, there’s much more to say about scripting than can fit in an introductory article. But I hope the little that we’ve described today piques your interest and shows how powerful and helpful scripting is.

Community Power!

If you decide to dive into it, let’s learn together and share our experience. Ask your questions and share what you’ve done in the comments. If you are not a coder, consider leaving an idea for a script; maybe another reader will make it happen.

Let’s make Photoshop more useful together!

Resources

I’m still learning about Photoshop scripts, too, and here are some resources that are helping me along the way:

  • Adobe Photoshop Scripting12,” Adobe Developer Connection
    All of the documentation and utilities for scripting.
  • Adobe Introduction to Scripting13” (PDF), Adobe
    Here are the basics on scripting for Adobe applications. The nice thing about scripting for Photoshop is that you can apply your knowledge to other Adobe products; you just need to learn the application’s DOM, and you’ll be ready to go.
  • Adobe Photoshop CS6 Scripting Guide144” (PDF), Adobe
    In this introductory guide to scripting for Photoshop, you’ll find the basics on getting started with scripting.
  • Photoshop CS6 JavaScript Reference15” (PDF), Adobe
    This describes all of the objects and their functions and methods that you can use in scripting for Photoshop. This is one of the documents I use most when writing scripts.
  • JavaScript16,” Mozilla Developer Network
    Here are answers to all kinds of questions about general JavaScript functions and usage.
  • JavaScript Tools Guide17” (PDF), Adobe
    This has basic information about ExtendedScript Toolkit and some advanced techniques, such as file system access and ScriptUI and working with XML, sockets and more.
  • PS-Scripts18
    An independent forum about scripting for Photoshop. I wasn’t able to sign up to participate in discussions (because of some kind of pre-moderation system), but it has plenty of answered questions and solved problems to discover.
  • Photoshop Scripting19, Adobe Community
    Adobe’s official forum for Photoshop scripting has some good discussion on problems encountered by users.

(al) (ea)

Footnotes

  1. 1 http://www.codecademy.com/en/tracks/javascript
  2. 2 http://www.smashingmagazine.com/wp-content/uploads/2013/07/smashingpost_extendedtoolkit_big_mini.png
  3. 3 http://www.smashingmagazine.com/wp-content/uploads/2013/07/smashingpost_extendedtoolkit_big_mini.png
  4. 4 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-Scripting-Guide.pdf
  5. 5 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-JavaScript-Ref.pdf
  6. 6 http://blog.kam88.com/en/rotateme-photoshop-actions.html
  7. 7 https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for
  8. 8 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-JavaScript-Ref.pdf
  9. 9 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
  10. 10 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-JavaScript-Ref.pdf
  11. 11 http://cl.ly/Q7jt/file
  12. 12 http://www.adobe.com/devnet/photoshop/scripting.html
  13. 13 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/Adobe_Intro_to_Scripting1.pdf
  14. 14 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-Scripting-Guide.pdf
  15. 15 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/cs6/Photoshop-CS6-JavaScript-Ref.pdf
  16. 16 https://developer.mozilla.org/en/docs/JavaScript
  17. 17 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf
  18. 18 http://ps-scripts.com/bb/index.php
  19. 19 http://forums.adobe.com/community/photoshop/photoshop_scripting

↑ Back to topShare on Twitter

Kamil Khadeyev is a graphic designer specializing on icon and UI design. You can see his latest works at his Dribbble account. Kamil also has a Blog where he writes about design related themes, such as workflow tips, scripts for Adobe Photoshop and more.

Advertising
  1. 1

    Great Article. Thanks for sharing.

    1
  2. 2

    Awesome article. I’ve always wanted to dive into creating my own scripts. Fantastic reference for a new comer! Great work.

    8
  3. 3

    Thanks for your useful article about scripts. Now we can make our own useful scripts as per our requirements by learning such a useful lesson. Thanks Kamil Khadeyev. Keep it up and want to see more of your articles like this!

    0
  4. 5

    Pretty wonderful piece to save ‘time’. More than your article, I loved the illustrations of it – simple, clean and bewitching

    1
  5. 6

    Thanks for the article. I honestly think that it really touched the tip of the iceberg. In a good meaning. I was working on Photoshop scripts for a short time but I already see a lot of benefits you get from using them. I am really curious how people will use them and what time saver scripts can be generated.

    0
  6. 7

    We used scripts for image processing once before at my old job, you can do very complex things with scripting, ours would process the image, save and send it to the database; I didn’t know how it all worked :) But it was pretty awesome to see photoshop do these things, I’m sure they had a script call another script or something, since I did have two or three script files but only called one of them each time I wanted to process the image.

    0
  7. 8

    Adobe has done a great thing by making all (?) of their apps scriptable – not just with one language, but three, and provided very good (but far from deep enough) documentation from each. Unfortunately, each language has limitations. Javascript has no access to the filesystem. If it did, that would be my first choice. Applescript is awful at so many levels. That leaves VBScript, which is a very robust solution, but only for Windows. When I was developing web software on Windows, I had dozens of automation programs in VBScript that easily replaced the labor of a full-time junior designer.

    Since I moved to Mac, however, I could no longer use VBScript. Applescript is a dead-end, and Javascript has its security restrictions. So I turned to appscript (google it). That expanded the language options to six! ObjectiveC, Ruby and Python. I migrated all of my scripts to Ruby, and over the past few years I’ve developed so many automations that it’s like I have a staff of graphic designers working for free. I have full access to the Adobe object model, and it works so smoothly that sometimes I forget that Adobe is not supporting Ruby directly.

    Sadly, the appscript mechanism is no longer being supported and may stop working in a future release of OSX.

    0
    • 9

      Hey, John

      Thanks for your comment. Never heard about “appscript”, I will check it out.

      But as much as I know, JSX has File System Access.

      Few of my scripts are using this advantage, for example “LayoutWrapper” (http://blog.kam88.com/en/layout-wraper-for-photoshop.html ) uses PSD file that is stored in “templates” folder for wrapping documents with safari window.

      Or, sometimes I use external XML document with script’s configuration settings and script can read/write values to it.

      You can review “JavaScript Tools Guide” for more information about File System Access: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/JavaScriptToolsGuide_CS5.pdf (page 39)

      2
      • 10

        I don’t recall the limitations, but they were deal-breakers. One of my workflow requirements is to execute shell commands and ghostscript binaries after export, something that is not possible from within the Extendscript app, but is trivial from Ruby. Javascript has an atrocious object model to boot.

        0
        • 11

          I am not sure about shell commands, but scripts can execute files. At least I have script that executes *.link and *.webloc files to open URL in user’s browser.

          0
        • 12

          At my previous job, I’d written some JSX scripts that would write a .bat file and execute it. The JSX language Adobe offers with Photoshop has filesystem read/write and execute. So you can create a shell script on the fly, save it, then file.execute() to fire off external processes. Worked beautifully for our purposes.

          0
  8. 13

    I have to be honest. How long did it take you to write the script vs how long would you have taken doing the RotateMe figure?

    -3
    • 14

      Hey, Marco

      I understand your skepticism.
      If we talk about RotateMe and if your goal is to get one exact figure and you know how it should look: how many stems you’ll need, what pattern it should have etc.—then probably it will be easier to do this manually, rather than writing script for it.

      Automation is useful on repetitive tasks: if you do one thing X times again and again, then probably this task could be automated.

      Back in the days, when I made RotateMe action, I had a goal to make a set of different snowflakes, duplicating and rotating stems manually was kind of boring. Moreover I wanted fast result to see how snowflake will look with one or the other pattern. So, for me using some help of automation was helpful.

      RotateMe solves not most popular problem, I agree. But I used it as an example to show the basics of writing script in clear way: algorithm is easy to understand, but at the same time code is not too primitive, so readers can learn how to use some functions and properties on the real world example.

      8
      • 15

        It was a good demo to introduce to the concept of PS automation. It’s true power lies in automating your workflow. I worked in a prepress shop, and we used it to read/write file metadata saved by the in-house designers that would trigger scripts to prepare files for printing and generating online proofs. They would create proofs from the artwork, place it into a template (using the metadata to tag the proofs with artist info, market location, date, etc) and save thumbnails/jpgs/pdfs to the webserver. For the artists, it was a 1-click process to go from design to online proof, and it ran in < 30 seconds.

        1
  9. 16

    I’ve been trying to learn Scripting in Ps and another program for the past few months. Learning the scripting guides / SDK has been my biggest road block.

    0
  10. 17

    Thanks for all this information! I will take this action on my own project – thanks again!

    1
  11. 18

    Such a wonderful post ! Thanks for sharing with us

    2
  12. 19

    Brilliant article, thanks. For my homework this weekend, I’m going to read it twice and make a couple of simple scripts. Great stuff! ★★★★★

    3
  13. 20

    Daniel Montgomery

    July 26, 2013 9:37 pm

    Thanks for the article.

    Being able to use JS (can’t believe I’m saying this) is a great option. I’ve struggled with just the same issue (rotating an object a set number of times) and found PS to be very awkward.

    As others have said, great intro and great illustrations / examples.

    0
  14. 21

    Deepak Bhagchandani

    July 26, 2013 9:52 pm

    Thats like Processing within Photoshop.

    1
  15. 22

    “you would go to Application → Document → Layer → Opacity and set the desired value…” Or you can put the opacity in the layer window at the bottom right :)

    0
  16. 23

    Great Article!

    0
  17. 24

    Fantastic article Kamil! Much appreciated!

    From Portland, OR
    —André M.

    0
  18. 25

    Yes Its Indeed a great Article but Why should we use Js in Photoshop? I mean Is there really a special need to make it interactive, although we can do it separately!

    1
    • 26

      Of course it is not a solution for every automation problem in Photoshop. It is just another tool in our toolbox, that can be useful in some cases.

      There can be different situations which can require different solutions. Sometimes it easy to make something manually, sometimes we can use actions, but sometimes there can be situations where we just can’t automate our process without scripting.

      For example, I have a script, that wraps out current document into safari browser window (http://blog.kam88.com/en/layout-wraper-for-photoshop.html ). We can’t make something like this just with actions, because documents can have different dimensions and we have to take this in mind and perform specific actions depending on this. Scripts can solve such problem.

      0
  19. 27

    Great article … but I don’t think a pure designer will use scripting to rotate something as he/she will be more comfortable using Photoshop. Its just my opinion :)

    2
    • 28

      I agree that scripting can be not for everyone, but still it is pretty fun and useful thing in workflow automation.

      0
  20. 29

    Hey Kamil,

    I read your article earlier today, and I just wanted to see it really inspired me to look into how scripts can be integrated into my workflow and already I’ve found some solutions to problems that I invest a lot of time and manual labor to do. This is a great article for beginners because it introduces a concept that could otherwise feel intimidating. I’ve yet to write any of my own scripts but I want to learn more, and I already feel more confident adapting and tweaking the works of others.

    Thanks for taking the time to write this article, the links you referenced also look very promising and informative.

    1
  21. 30

    Does this work in Illustrator? That program has no autosave and a bad habit of crashing at the wrong time!

    0
    • 31

      Yes.
      But you have to learn Adobe Illustrator’s Document Object Model first. It differs a bit from Photoshop’s DOM. You can get documentation about it here: http://www.adobe.com/devnet/illustrator/scripting.html

      I didn’t worked too much with scripts in Adobe Illustrator, but as much as I saw, it has more powerful API (i.e. you can do more interesting things there). I wish Photoshop will have same level of access and amount of functions someday too.

      By the way, you can even write a script, that will do something in Adobe Illustrator and then switch to Adobe Photoshop to do something else. It’s pretty fun thing.

      1
  22. 32

    Amazing article! Good explanation and nice sources. Congratulations and thanks!

    0
  23. 33

    This is the craziest article I’ve ever read! Kudos. Now if only I could convince my boss that a humble JavaScript technician could also do photoshop….

    0
  24. 34

    Great article! I especially appreciated the resources.

    Yes, *.jsx does have file system access.

    Here’s a cs5 .jsx I wrote that creates a web page from photoshop layers:

    https://github.com/kenpenn/psdExport

    0
  25. 35

    Thanks a lot for such a great article. this is so useful to me, I am looking forward for many such wonderful article from you.

    0
  26. 36

    Alejandro Heredia

    August 7, 2013 4:08 pm

    This article is awesome. I really like JavaScript because it is very easy to extend. I didn’t know that PhotoShop Scripting was JS compatible. I think this also has many uses when it comes to an app that handles images (uploading, resizing, cropping, watermarking, etc.) Thanks for all the information provided in this article.

    0
  27. 37

    Excellent guide! Unfortunately object model is quite poor in Photoshop, basic things like working with selected layers, resizing using pixel units, and working with vector is missing. Many things are requiring ‘low level’ scripting using action reference. If also find that provided API is not sufficient, please add your requests to http://forums.adobe.com/community/photoshop/photoshop_scripting
    who knows, maybe adobe will improve it :)

    0
  28. 38

    Great article, I read it a few weeks ago and and it really pushed me to pursue making a plugin for photoshop – here is the result in just a few weeks time: http://www.youtube.com/watch?v=_ThXuPhcgNw
    Overcame many obstacles on the way but it was worth it!

    0
  29. 39

    Hi author, i have read your tutorial which makes sense. But i am stuck where you left. Meaning i can’t do what i wanted to do. Can you please do it for me.

    I want a script that does something very simple.
    when it’s ran..
    > if move tool is selected than switch between (group or layer).

    0
    • 40

      Hey, Umer
      Unfortunately, you can’t access group/layer selection switch through the scripts. At least I didn’t found any way to do it.

      0
      • 41

        What you would do is to create two presets then switch between them.

        0
        • 42

          These functions should be of use…
          function getCurrentTool(){
          var ref = new ActionReference();
          ref.putEnumerated( charIDToTypeID(‘capp’), charIDToTypeID(‘Ordn’), charIDToTypeID(‘Trgt’) );
          return typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID(‘tool’)));
          };
          function selectToolPreset(TOOL) {
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putName( stringIDToTypeID(‘toolPreset’),TOOL );
          desc.putReference( charIDToTypeID(‘null’), ref );
          executeAction( charIDToTypeID(‘slct’), desc, DialogModes.NO );
          };
          function selectTool(tool) {
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putClass( app.stringIDToTypeID(tool) );
          desc.putReference( app.charIDToTypeID(‘null’), ref );
          executeAction( app.charIDToTypeID(‘slct’), desc, DialogModes.NO );
          };

          0
  30. 43

    the code was hard and i dont see any reason to do this

    0
  31. 44

    the author say you dont have to know coding. Look at the code you do have to understand some form of coding. I wont be able to write this code of the top of my head. You would have to understand what a var is and i ++ and syn text ect.

    0
  32. 45

    Scripting isn’t for people who like to work slow.

    This comment is for everyone on this thread who doesn’t ‘get it’. Don’t hate on it just because you don’t understand it. Challenge yourself to identify your repetitive tasks and take your design productivity to the next level. Kamil used a basic and highly visual example for this intro, so you need to look past that to realize the power scripting has to offer.

    Think of how many things you repeat daily as a designer. If you use the ‘Save for web’ dialogue – what a time suck. You could do that with one click (or hotkey). You can export to any file with 1 click, using the layer name (or appending it to the PSD file name). Making a series of images the same size? Put them in layers in one document. You can export all those layers to files in 1 click as apposed to going to ‘File > Save for web…’ for each. There are so many examples of how you can optimize your workflow.

    I can honestly say that my Photoshop workflow for web work is 20% faster, just from the two examples I gave above.

    Excellent article Kamil, thanks for sharing.

    0
  33. 46

    Scripting is useful for my work.Thx for share.I have a question about the script, I hope you can help me.
    When I select one layer in ps,I can use “app.activeDocument.activeLayer” to handle this layer in script.Than I use mouse and ctrl key to select Multi-layer,and can’t find a field or method to handle the multi-layer in script.I have read the script introdution,and no find a result.Is that I miss someting? I hope you can help me to solve this problem.

    0
  34. 48

    Thanks for the great article…

    0
  35. 49

    After reading this I made a similar script which rotates the angle nob of the satin effect, which changed a lighting type effect, in a loop by 6 degrees each time and saves them out to a new sub folder based on the file name of the file, with padded incrementing file names (000.jpg, 001.jpg etc) creating a basic animation. Your tutorial gave me a good basic understanding, thanks.

    0

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top