2

I'm working on a script for my team that will allow them to batch rename artboards in Photoshop. We use the following naming convention for our artboards: "ProjectTitle_BannerType_WidthxHeight_LG" where "LG" is a 2-letter language code.

My script seems to work fine if you have a layer selected, but doesn't work at all if there are no layers selected (8007 error - so not a very helpful error). I'm new at this and not sure why this is happening, or how to go about fixing it. Any help would be appreciated.

//Written by Ryan Walker
//Version 1.0.2


////////////////////////
// Dispatch
////////////////////////

main();


///////////////////////
// Functions
///////////////////////


//////////////////////////////
// Function: main
// Usage: core routine
// Input: <none>
// Return: <none>
/////////////////////////////

function main() {

    var doc = app.activeDocument;   // set active document
    var artboards = doc.layerSets;  // the top levelSet - **This contains all artboards AND folders that are not in artboards**


    var projName = getProjectName(artboards[artboards.length-1]);    // gets the project name from the last layerSet
    var langCode = getLang(artboards[artboards.length-1]);           // gets the current Language code from the last layerSet

    var newName = reqNewTitle(projName);                             // requests a new project name
    var newLangCode = reqNewLang(langCode);                          // requests a new Language Code


    var curArtboard = artboards[0];                                  // define the current artboard
    doc.activeLayer = curArtboard;                                   // sets the first layerSet as the active layer


    var i;                                                          

    for (i = 0; i < artboards.length; i++) {                         // iterate through artboards layerSet

        curArtboard = artboards[i];                                  // set current layerSet to iteration

        replaceName(curArtboard,projName,newName);                   // replace old project name with new project name
        replaceLang(curArtboard,newLangCode);                        // replace language code with new language code

    };

};


//////////////////////////////////////////////////////////////////////////
// Function: getProjectName
// Input: LayerSet - Artboard 1
// Return: String - Project Name
//////////////////////////////////////////////////////////////////////////
// pulls the project name from an artboard layerSet name
//////////////////////////////////////////////////////////////////////////

function getProjectName(artboard) {

    var usIndex = artboard.name.indexOf('_');               // index of the first underscore
    var projName = artboard.name.substring(0,usIndex);      // substring containing all letters before the first underscore

    return projName;
};

//////////////////////////////////////////////////////////////////////////////////////////
// Function: reqNewTitle
// Input: String - Project Title
// Return: String - New Title for the project
//////////////////////////////////////////////////////////////////////////////////////////
// prompts the user for a new project name, defaults to old project name
////////////////////////////////////////////////////////////////////////////////////////////

function reqNewTitle(OldTitle) {

    var NewTitle = prompt("What is the project title?",OldTitle,"New Title?");

    return NewTitle;

};


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function: replaceName
// Input: LayerSet - Arboard, String - Old Name, String - New Name
// Return: <none>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// receives an layerSet and replaces the old project name with the new project name if that layerSet name contains it
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function replaceName(artboard,projName,newProjName) {

    artboard.name = artboard.name.replace(projName,newProjName);
    return;

};


//////////////////////////////////////////////////////////////////////////////////
// Function: getLang
// Input: LayerSet - Last Artboard
// Return: String - Language Code
//////////////////////////////////////////////////////////////////////////////////
// pulls the language code from the last two characters of the last artboard name
//////////////////////////////////////////////////////////////////////////////////

function getLang(artboard) {

    var langCode = artboard.name.substring(artboard.name.length-2,artboard.name.length);  // Substring of last 2 characters

    return langCode;
};

//////////////////////////////////////////////////////////////////////////////////////////
// Function: reqNewLang
// Input: String - Language Code
// Return: String - New Language Code
//////////////////////////////////////////////////////////////////////////////////////////
// prompts the user for a new Language Code, defaults to old Language Code
////////////////////////////////////////////////////////////////////////////////////////////

function reqNewLang(langCode) {

    var NewLang = prompt("What is the language code (EN, FR, ES, or PT)?",langCode,"Language Code");

    return NewLang;

};


//////////////////////////////////////////////////////////////////////////////////////////////
// Function: replaceLang
// Input: LayerSet - Arboard, String - New Language Code
// Return: <none>
////////////////////////////////////////////////////////////////////////////////////////////
// receives a layerSet and removes the last 2 characters and adds the new Language Cod
////////////////////////////////////////////////////////////////////////////////////////////

function replaceLang(artboard,newLang) {

    artboard.name = artboard.name.substring(0, artboard.name.length-2) + newLang;
    return;

};
  • I've managed to solve this problem using a function generated by script event listener to select the top layer, but I'm still confused as to why a layer needs to be selected in order for the script to work. – Ryan Walker May 22 '18 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.