Menu Close

Photoshop scripts

What’s a Photoshop script and why do we need scripts when retouching product images for online stores, especially when we have something as powerful as actions? Well, the deal is that Photoshop is not a perfect program. I usually resort to scripting when I need to do something that cannot be achieved by using actions. Like what? Like when I need to use a conditional. That is – to make Photoshop choose between different options instead of just running a sequence of commands. Photoshop’s built-in conditions are very limited, and a situation where you need to run specific commands depending on your image state is very typical in product image retouching. But before we start discussing any particular cases when scripts might be helpful, let’s first talk about scripting itself.

Scripting means coding or programming. This fact makes people think that it's a very difficult thing. But it's not true. Scripts are great fun and writing them can be an easy and exciting process, and anyone can do it. Don't be scared and you'll see it for yourself.

So what’s a script in Photoshop and how is it different from an action? An action is a sequence of commands written in such a format that they can be accessed from within the Photoshop interface. Scripts are basically the same thing, as they also contain commands, but written with the use of a programming language, which is JavaScript. There’s also VBA for Windows and Applescript for Mac, but if you want cross-platform compatibility, JavaScript is the language of choice. A script is basically a text file that contains commands, conditionals, functions and tells Photoshop what to do, when and how. Photoshop scripts can be loaded and run separately, or from within actions as if they were simple commands. Let’s see how to do that.

There are two typical Photoshop script formats: the first one is *.jsx, which is just a text file. You can open it with any document editor and edit, and save, and it will work. Another option is *.jxbin or a binary file, and in this case, the script itself is encrypted and cannot be viewed as text. This is good when you don’t want users to edit your scripts or to even know how they work. To create binary files, you need a script editor. It's a good idea to get one anyway – it's better than using Notepad. It’s available for free and you can download it from the Adobe website or from the Creative Suite. The name of the software is the ExtendScript Toolkit or ESTK for short.

To be able to write scripts, you have to be familiar with the principles of programming and have at least some knowledge of JavaScript. As this is not a programming course, and I'm a lousy programmer myself, I will try to keep it short, but script writing is not rocket science, and any person with a desire to learn can master it. I’ll show you an example after we've learned how to run scripts.

Running scripts

There are two ways to do so. Well, three, as you can also run scripts via the Events Script Manager, but that's another story. The first one is very straightforward: go to the File menu, then to the Scripts submenu and click on the Browse command. Then find the script you need to run, click on it and press Load or just double-click it. This is a good way if you need to do something like that once, but in case you need this script often it’s better to put it in an action. To do so, create a new action and make sure you are recording, and then just go and do the whole browsing thing again. This time it will be recorded. So the next time you need this script you just run the respective action and it will be loaded. It doesn’t have to be a separate action with just one command inside, it can be a part of an action as well depending on what you need.

There's one thing that you should be aware though if you're going to use the aforementioned method. When you run a script via the Browse command, it won't be recorded if the script gives you an error when it runs. So if there are some special conditions in the script, you need to fulfill them before running it. For example, when a script works with selections, you might need an active selection on an image to make it run. Most scripts work on currently open and active documents, so you might need an image opened to make it work. Some scripts only work on saved images though, so you can't just use an Untitled image and so on. There might be a lot of reasons why a script refuses to run. If a script comes with a manual, it's usually stated there whether you need special conditions for it to run or not.

There's also another way how you can load scripts. Copy the .jsx or .jxbin files into a special folder where your Photoshop is installed, and you'll be able to access them from the Scripts menu by names. Here are the folder's locations on both Mac and PC platforms.

C:\Program Files\Adobe\Adobe Photoshop (your version here)\Presets\Scripts

Macintosh HD:Applications:Adobe Photoshop (your version here):Presets:Scripts

After you've copied your scripts there, they will appear in the Photoshop File menu, Scripts submenu. If you record an action that runs a script from this menu, you'll see that it doesn't actually specify a path, and just calls the script by name. It means that if you give this script and the action that runs it to some other person, they will be able to use them straight away if they put the script in the specified folder. It's not a huge difference, but it's important for the cross-platform compatibility and for people that don't know how to rewrite a step in an action if it addresses a wrong place like a non-existing folder.

If an action runs a script from a specific location, like C:\Scripts\script1.jsx, another person can use the same action as long as they put it in the same folder. That is, if they work on PC, because if they worked in a Mac environment, they wouldn't be able to create or access such a folder. Instead of the “C:\” hard drive, they have “Macintosh HD:”. Mac users would have to rewrite the action to make it run the script from somewhere else on their hard drive. But as my experience revealed, most users are not able to do that. When they try to run the action and an error message pops up, they think: “Oh, boy, the thing is evidently not Mac-compatible”, or even: "There's something wrong with the script!". To avoid this confusion, it's better to tell them where they should put the script, so they can use the action that runs it without any problems. But that's a pain in the head for people like me, who write their own scripts and actions and make other people use them. If you've just started, you should only care about yourself being able to run the scripts and actions in question, and you can put them virtually anywhere.

Why scripts and how they can be used

Does scripting for Photoshop require you to have programming skills? Well, yes and no. You definitely will be programming if you're going to write any scripts, but the truth is that modern programming languages are basically technical English with special grammar and syntax. I'm a very amateur programmer myself, but even my extremely limited knowledge of Javascript has opened endless possibilities for e-commerce product image retouching automation. It's not as hard as it might sound.

But why scripts? This is not something you can see in a regular product image retouching tutorial. And it's a pity because scripts can simplify our retouching work in most astonishing ways possible. I guess the reason why scripting for Photoshop is not so common is that most tutorials and articles available online are not dedicated to product image retouching. Let's see how we can use scripts in a typical situation where you have to make sure all your images you make are the same size in pixels – this is quite a common situation in catalogue retouching. Any other size will be rejected by the client or the website engine, so you have to be careful about that and check if your images are all of the correct size in pixels. It can be done in two ways. The first one is easy: when you finish, open Adobe Bridge (if you don't have it, you can download it, it's freeware), select all the images at once and check if they all have the same size. But I prefer another way, a fool-proof way so that I don’t need to check at all so that I am sure that all my images are correct.

How can I achieve that? Well, here’s an idea. In product image editing, we usually save our images when they are finished. We don’t need to save copies and keep our bulky *.psd files in case something goes wrong because retouching for online stores is all about working fast and efficiently. When you have to retouch several hundred images in a single day, there’s no time for playing around with them. So we save finished images, and this is the final step of retouching.

To get rid of the necessity to check my images for size, I want Photoshop to help me not to mess it up in the first place. I want it to check the image size when I’m trying to save an image and give me an alert message if the size is wrong. In this case, I will only be saving correct images, and I will instantly be aware if I’m doing something wrong. Cool, right? But how can we do that?

Let's get acquainted with the editor first. To be able to write scripts, you don't need any special software. You can just create a text file, write a script, change the file extension to .jsx and run it in Photoshop. But this is not the most convenient way, so I advise you to download and install the ExtendScript Toolkit for Photoshop (ESTK). It's free and you can get it from your Creative Suite or just download it from the Adobe website. When it's open, it looks like a text editor, which it actually is. You write your scripts there, and you can press the “play” button and run them in Photoshop without having to load them there. To do so, switch from the ExtendScript to your version of Photoshop in the dropout menu in the upper left corner:

If you don't do that, you'll most probably get an error. As the ESTK doesn't switch to Photoshop by default, you can target Photoshop from within the script you're going to run by adding a special command to the first line of the script:

#target photoshop

If you do that, ESTK will automatically run it in Photoshop. But there's nothing to run yet. Let's write a command. Make sure you get familiar with JavaScript if you wish to learn how to write scripts because, in programming, your writing syntax is essential. Commas, dots, semicolons, capital letters – it all matters. I once spent 3 hours tweaking a script that didn't work as intended because I've used a comma to separate a decimal, and it had to be a dot instead. But unlike product image retouching courses, Javascript programming courses are widely available online, and for free. If you're interested, you can always take a free course and immerse yourself in the world of programming.

Let's now get back to our task at hand. We want to write a script that will perform a size check every time you're trying to save an image, and if the size is wrong, it has to alert you somehow. Alerting in Javascript is easy. You just write:

alert("Hello!");

That's it. I've put the “Hello!” word in between the quotation marks, otherwise, it won't be recognized as text. Now when I run the script, a message pops up in Photoshop. Easy, right?

Now let me show you how a saving script works. Just watch the following video and you'll see how easy it is and how much time and effort it can save you in the long run.

Check image size with a script

Some contents or functionalities here are not available due to your cookie preferences!

This happens because the functionality/content marked as “Google Youtube” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

To avoid complicated programming stuff, since we've just started, I'll use actions as well, because the concept of actions is more familiar to you. But the whole thing can be done in a single script, it would just be more complicated. First of all, let’s record an action that saves images. To make things easy, I will just use the Save command from the File menu, and I will specify a folder where I will be saving all my images, and I will choose *.jpeg as my final image format, and I will save my images with the maximum quality, which is 12. So, next time I run the action it will save the respective image into the specified folder, and that’s pretty much it. Now, what about the size? Let’s say that I need all my images to be 1000 pixels wide and 1400 pixels high. Next thing I need to do is to write a script that will check the image size and alert me if it’s not right. Believe me or not, but the most simple version of a script like this is just four lines long. I wrote it like this:

if (app.activeDocument.width == 1000 && app.activeDocument.height == 1400) {
doAction('save', 'TEST');
} else {
alert("Incorrect image size: should be 1000x1400px", "Fail!");
}

If I wanted to interpret it from JavaScript language to human language, it would go like this: when this script is run check if the image width is exactly 1000 pixels and height is 1400 pixels. If both height and width are as stated above, run the “save” action from the “TEST” set. Otherwise alert the user by displaying an error message in a window called “Fail!” which says: "Incorrect image size: should be 1000x1400px".

If I try to run this script on a random sized image, it will give me an error, it and will not run the “save” action until the size is right. The script that does it can be saved and loaded as a part of an action. So if I have to retouch a batch of images of a certain size, I will be sure that the images I saved are correct, otherwise, they will just not appear in the saved folder. And if I am not just a retoucher, but the head of a retouching department, and I have multiple retouchers, like 10 or 20 of them, it might be a great improvement of the process. Because instead of making everyone check their images for size or doing it myself, I can be pretty sure that all the images I get to check already have the right size. If, of course, all the retouchers use the respective script. This is just one example of how scripts can simplify the process of making catalogue images.

Open

Four lines of code and a couple of simple actions is all it took. Pay attention to the fact that the command “doAction('save', 'TEST');” is telling Photoshop to run the action named “save” from the action set named “TEST”, which should be readily available in your Actions panel, otherwise it won't work. And it's case sensitive, too.

When you write Photoshop scripts, you might forget what the particular lines of code are supposed to do. Or maybe you'll want others to be able to use and understand your scripts. If that's the case, you can use comments to clarify things a little bit. This is how it works: all you need to do is add a double slash “//” at the end of the line or anywhere in the script body.

All the text after the double slash won't be run and it can be used for comments. The effect is only valid for a single line though.

alert("Hello!"); // this will make a “hello” window pop up

You can also put “//” before some actual lines of code if you don't want it to be run, which is useful when you're testing something.

// alert("Hello!"); // if you leave “//” at the beginning of the line, the alert command won't be activated when you run the script

So when you have a .jsx script, make sure you check if there are any useful comments. In ESTK, they are visible as green text, easy to distinguish from regular lines of code.

Next: Useful JavaScript commands

Top

On this website, we use first or third-party tools that store small files (cookies) on your device. Cookies are normally used to allow the site to run properly (technical cookies), to generate navigation usage reports (statistics cookies) and to suitable advertise our services/products (profiling cookies). We can directly use technical cookies, but you have the right to choose whether or not to enable statistical and profiling cookies. Enabling these cookies, you help us to offer you a better experience. If you choose to disable cookies, you won’t be able to watch the embedded videos. Cookie policy