Example: Scripting

I often get various questions at the forum for how to use the program and how to configure it for various tasks. Sometimes the particular task is so complex and so specialized that the batch methods supplied with Advanced Renamer cannot help the user. This is why I added JavaScript support to the program and in the next paragraphs I will show you how it works.

I will base my example on a simple task. Imagine we have a lot of files where we want all odd files to end with A and all even to end with B. The files could have names like this:
img2001.jpg
img2002.jpg
img2003.jpg
img2004.jpg
img2005.jpg
img2006.jpg

We want the resulting filenames to end up like this:
img2001_A.jpg
img2002_B.jpg
img2003_A.jpg
img2004_B.jpg
img2005_A.jpg
img2006_B.jpg

First add the files to Advanced Renamer. Then add the Script method to the method panel. Make sure this is the only method in the panel. In the script editor we can create the script needed for producing the new filenames. The script is executed once for each item in the list. The input parameter item contains the item from the list and its many properties. Typing item and hitting dot will show a list of properties on the item object.

Before creating the actual script we need to setup a global variable. This is done by clicking the Pre Batch Script button. A window displaying a new script editor will appear. The script created here will only get run once for every batch run. This is a good place to setup a global state variable. Type this into the Pre Batch Script editor:

var odd = true;

Now for the actual item script. In the script we will use the global variable named odd setup in the pre batch script, and use it to insert either A or B into the filename. Insert this script into the editor:

var str = odd ? 'A' : 'B';
odd = !odd;
return item.newBasename + '_' + str;

statement is the line telling Advanced Renamer what the new filename of the item should be. Notice that we return a complete filename including the extension. Most methods in the program is by default configured to only work with the name part of the filename, but when it comes to the scripting method, you need to return a complete filename including the extension.

This example is really simple. But to demonstrate the pre batch script I made it a bit more complicated than necessary. Try to remove the pre batch script and instead use this script in the script panel:

var str = index % 2 ? 'B' : 'A';
return item.newBasename + '_' + str;

Notice how the last line is the same but the two first lines for calculating the str variable has changed. This script will by using the index input parameter, produce the exact same result as the previous script.

So what else can we do? By using the properties of the item object we can access all information about the particular item available to the Advanced Renamer batch runner. If we also want to add the image dimensions to the filename in the script we can use a script like this (assuming we are renaming image files):

var str = index % 2 ? 'B' : 'A';
return item.newBasename + '_' + item.imgWidth + 'x' + item.imgHeight + '_' + str;

Any tag available to the program is also available in a script by using the parseTags methods of the app global object. To add an incrementing number after the base name use this script:

var str = index % 2 ? 'B' : 'A';
var i = app.parseTags('');
return item.newBasename + '_' + i + '_' + str;

The parseTags method will actually take a string containing both normal characters and tags, just like the New Name method. The rule of thumb here is, if it works with New Name method it will also work with app.parseTags.

What else can scripting method do? The answer is: Almost anything you want! It is a very powerful way of customizing the application behavior, but it requires a much larger skill set than the other methods of Advanced Renamer. For those who find the program too complicated, the scripting method will only make it even more complicated. But for those who are already skilled with programming or scripting skills, the scripting method can be of great help.