1
votes

New to ExtendScript and trying to finish an automation project for work.

I have a bunch of images that I am combining in photoshop via a script and need to open a image pairing based on an initial image. I know that the name of my paired file will be predictable up until the final character, at which point it will be any uppercase letter A-Z.

For example:

CH-14B1-SP-01-A can pairs with CH-14B1-SP-PV-01-A, but could also conceivably be paired with CH-14B1-SP-PV-01-B. Each paired file has an A-D replicate and we choose the best to be paired.

I have a script working that requires user input to decide what replicate to look for. I want to automate this. My code looks like this:

// ask user input for PV replicate letter
var repLetter =prompt("Which PV replicate would you like to use? (A.. B.. C.. etc.)");

    // get the info out of the source doc
var fileName = srcDoc.name;
var docName = fileName.substring(0,fileName.length -4);
var filePath = srcDoc.path.toString();
var fileExt = fileName.substring(fileName.length -4, fileName.length);

var nameCheck = fileName.substring(0,fileName.indexOf("CH-14B1-SPI-"));

if (nameCheck <1)
{
   var fileNum = fileName.substring(12,fileName.length -5) + repLetter;
   // no underscore so we need to open it's namesake
   // alert(nameCheck)
   var filePair = filePath + "/PV/" + "CH-14B1-SPI-PV-" + fileNum + fileExt;
   openThisFile(filePair)

Is there any way to make the var repLetter just accept any value at all?

Something like

var fileNum = fileName.substring(12,fileName.length -5) + [a-z];

I tried the above hoping it would do the trick (again very new to this) and I was told that "a" was undefined.

1
You first have to fix your implementations of the substring() method. You're returning string.substring(0,0), which returns a string of no length. In other words, nameCheck is "", and fileNum is just "A" (or whatever is chosen at start, which, by the way, you might want to use with a default value, i.e., prompt("prompt", "defaultValue") ) - CRGreen

1 Answers

1
votes

Your fault lies in the incorrect syntax in

var fileNum = fileName.substring(12,fileName.length -5) + [a-z];

... it's just bad syntax, you seem to be mixing GREP into JS. The error is issued because [...] indicates an array, which is valid to 'add' to a string, but the expression a-z ("a minus z") needs variables named a and z. I suppose you simply wanted some sort of wildcard here.

Circumvent the entire issue by reading a list of candidate files, based on the current file name. This is kind of hard to test locally because it requires lots of dummy files (and I'm not entirely sure I understand your procedure). However, the general idea of the following should be clear.

Rather than prompting for 'any' letter, it's more user friendly to show the available choices. I am not sure how you get the list of candidates, so I'll let you fill in that yourself. You need to adjust the getFiles call for this; currently, it reads the files from filePath, with a * after its name to pick up everything starting with nameCheck.

The found list of files is shown in a simple dialog with radio buttons to pick a file from. As it is, it only shows an alert, and if you press Cancel it does nothing.

Please note I have tested this inside InDesign, not Photoshop, as it's an easier test bed for scripts, and so it is possible some of the property names are off.

srcDoc = app.activeDocument;
var fileName = srcDoc.name;
var docName = fileName.substring(0,fileName.lastIndexOf('.'));
var filePath = srcDoc.filePath.toString();
var fileExt = fileName.substring(fileName.lastIndexOf('.')+1);

var nameCheck = fileName.substring(0,fileName.indexOf("CH-14B1-SPI-"));

var filelist = Folder(filePath).getFiles (nameCheck+'*.'+fileExt).sort();

if (filelist.length == 0)
{
    alert ('No files found matching '+nameCheck+'*.'+fileExt);
} else
{
    var fileDialog = app.dialogs.add({name:"Choose a file", canCancel:true});
    with (fileDialog)
    {
        with(dialogColumns.add())
        {
            with (fileSel = radiobuttonGroups.add())
            {
                radiobuttonControls.add({staticLabel:filelist[i].name,checkedState:i==0});
            }
        }
    }

    if (fileDialog.show() == true)
    {
        alert ('you picked '+filelist[fileSel.selectedButton].name);
    }
}