Anyone doing large scale image handling with Adobe Photoshop will occassionally want to automate some tasks. The easiest way to do this is by writing an action. However, Photoshop actions don’t allow you to ask questions, or act in different ways based on different circumstances. In the terms a computer programmer might use, Photoshop actions don’t allow you to ask if-then statements, like “If the file is bigger than 200k, then resize it.” When you want to write actions that contain if-then statements, then you need to learn how to write scripts. I’m going to introduce you to Photoshop scripting by walking you through a simple but very useful script.
Last year I helped a large company that was involved in the jewelry business. This company both refined rare metals (gold, silver, platinum), which it sold to retail jewelers, and also produced its own line of jewelry (necklaces, wedding rings, pendants). The company had been around since 1912, but only recently had it gotten serious about managing its inventory from its database. They hired a photographer to help the graphic designer photograph their line of jewelry and get it onto their website and into their paper catalog. The graphic designer had more than 10,000 images of jewelry. The tech department, upgrading the info in the database, was trying to standardize certain naming conventions for the products. The graphic designer often had to rename several hundred images at once to bring the image names in line with the new naming conventions. Good thing she didn’t have to do this by hand.
Adobe Photoshop CS already had a good method of adding or modifying a file name, but it lacked a way to strip something out of a file name. That’s why I came up with this script. It helped the graphic designer strip out of the file names the parts of the name that no longer matched the new naming conventions.
Below is the script, with comments written in a language that I hope beginners can understand:
This first line simply creates a variable where we will, later on, store the names of the files whose names we change. We do this so that at the end of the script we can say, hey, we changed these names. You can think of a variable as a container, a place where we store stuff till we need it later on.
var filesRenamed = "We changed the names of these files: ";
This next line causes a dialog box to open on screen and asks the user to choose a folder. Every file in the folder that the user chooses will be effected by this script.
var selectedFolder = Folder.selectDialog("Select the folder to run this script on. All files in the folder will be affected.");
This next line makes sure that we really do have a reference to a folder. The user might have hit “Cancel” instead of having choosen a folder, and if so, the variable selectedFolder will be empty, in which case there is no point going on with the script.
if (selectedFolder instanceof Folder) {
What should be removed from the file name? If the user has a file name like “P42_WTTI98_Ochre_Settings.psd” and the user wants to get rid of “_Settings” then in this dialog box, when it opens on screen, they’d type “_Settings”.
var stringToBeRemoved = prompt("What portion of the file name do you wan to remove (example: '_p52' to remove '_P52' from the file name)", "Type the letters or numbers you'd like removed from file names.");
This next line checks to see that the user actually typed something in. If they didn’t then there is no point proceeding. This says “if stringToBeRemoved does not equal an empty string then continue so we can rename the files”.
if (stringToBeRemoved != "") {
The variable selectedFolder is now a reference pointing at the folder whose files we want to rename. The getFiles command returns a list of every file in the folder.
var allFilesInFolder = selectedFolder.getFiles();
This next line simply opens an alert box on the screen and tells the user how many files we are going to now rename. allFilesInFolder is a list (technically, an array) of all the files we will now process, and you can always get the length of any list by by asking for its length property, which is done by putting a dot and then “length” after the name of the list: ‘allFilesInFolder.length’
alert("This folder contains this many files: " + allFilesInFolder.length);
This next line is a special line that causes the script to loop through the list of files one at a time. All the lines of code inside these brackets below are then executed for each file, one at a time. To say that another way, between this bracket that opens at the end of this line, and the closing bracket 11 code lines below, we have some code that will be applied to each file. This next line is starting what is known as a “for loop”, which creates a variable called “i” and then increases “i” by 1 every time the loop happens. It keeps looping till “i” equals the length of allFilesInFolder, which is another way of saying till every file has been renamed.
for (var i=0; i < allFilesInFolder.length; i++) {
This next lines gets a reference to one particular file from the list. Each time we go through the loop, the next file in the list will be choosen. (The first time we go through list the variable “i” equals zero, the second time we go through the list “i” equals 1, the third time we go through the list “i” equals 2. And so on, till we’ve gone through every file in the list allFilesInFolder. The first file name in the list is at position 0, the next is at position 1, the next is at position 2, etc. If there are 67 files in a folder, we will get the files from positon 0 to position 66, which is 67 files in all.)
var thisFile = allFilesInFolder[i];
This next line gets the name of the current file that we just got out of the list. We store the name, for safekeeping, in the variable called oldName.
var oldName = thisFile.name;
It’s important that the user doesn’t erase the entire filename. The file will disappear if the user typed “09-12-05.jpg” as the string they wanted stripped from every file name and a particular file is called “09-12-05.jpg”. This next line makes sure that they’re not erasing the whole file name. This next line basically says “Proceed only if the letters and numbers to be removed don’t equal the whole file name.”
if (stringToBeRemoved != oldName) {
The replace command, down below, uses a special variable called a “regular expression”. This is created through the special command RegExp. The next line basically formats the stringToBeRemoved in a way that allows the replace command to understand it.
regEx = new RegExp(stringToBeRemoved, "g");
This next line takes whatever value is in stringToBeRemoved and replaces it with nothing in the file name. Thus the string is removed from the name. However, we have not yet changed the file name, we’ve just created a variable called newName that has stored the name we’d like the file to have.
var newName = oldName.replace(regEx,"");
This next line is the one that actually changes the file name. We use the rename command.
thisFile.rename(newName);
This next line checks what we just did. The file name should now equal the new name. If it doesn’t, then something went wrong.
if (thisFile.name != newName) {
alert("Unable to rename " + thisFile.absoluteURI + "->" + newName);
} else {
filesRenamed += newName + ", ";
}
}
}
}
}
Finally, we open another alert box on screen, and tell the user the names of the files that we just renamed.
alert("The script is done now." + filesRenamed);
Since 2003, Adobe has been using Javascript as its cross-platform scripting language. The above script is written in Javascript. Although Javascript started its life as a language for scripting websites, it has in recent years picked up new uses, including this one.
Those of you who’d like to use this script are free to. A script is just an ordinary text file, of the kind you’d write using Notepad on a Windows machine or Simple Text on a Macintosh. After downloading the script (or copying it and then pasting it into any blank document created with Notepad or Simple Text), you’ll need to save it to the correct folder on your machine. On a Windows machine, you’ll find the right folder by going to the Program Files folder (which is normally on the C drive), then into the Adobe folder, then into the Photoshop CS folder, then into the Presets folder and then into the scripts folder. On a Macintosh you will look in the Application folder for Photoshop CS, then follow the same instructions as for Windows. After you’ve saved the script to the right folder, you’ll need to restart Photoshop. You can save the file with any name, just make sure it ends with “.js”. Whatever name you give the file is how it will show up in the menu bar in Photoshop. After you’ve restarted Photoshop, click on the File menu bar and scroll down to scripts, you should see it listed.
Any questions?