Skip to main content
Known Participant
February 27, 2017
Answered

Jsx code to find a specific Folder and Rename it

  • February 27, 2017
  • 1 reply
  • 1654 views

Hi everybody, this is my first message in this community which I follow for quite sometime! As many I've started working with Js for Photoshop and I am stuck, so I would love to see your help!

I have created a set of Layers in a Ps document. A Layer called Photo #1 and a Folder called Photos.

I need a piece of code, that tries and search the folders (LayerSets) of my document, try to find if a specific folder name is met and then change it's name to a new.

So far I have found the way to change the name of my Photos Folders, with the following code

var doc = app.activeDocument;

if (doc.layerSets.getByName("Photos") ) {

alert ("GOALL!!! I see the Photos Folder")

doc.activeLayer.name = "New Folder Name";

}

This is ok for one folder, if this folder exists and it's named Photos, but sometimes I need the system to search for 3 folder names (Photos, Left Page, Right Page).
If I use the getByName("a folder") and the specific "a folder" doesn't exist, it creates an error.

So I need the system to try to find if one of those 3 Folder names exists, and it one at least exists to continue without errors.

Please help!

This topic has been closed for replies.
Correct answer pixxxelschubser

I do not understand what do you really want. What you describe: everey time you run the script the foldernames (layer set names) are changing from Photos to Left Page and translate this folder x-20cm and Right Page will also be a Left Page and translate x+40cm.

Sorry. I do not understand this workflow.

But you can do that with a few simple changes in the code I posted before:

var aDoc = app.activeDocument;

var arr = [["Photos","Left Page",-20],["Right Page","Left Page",40]/*,["Left Page","Left Page",0]*/];

for (i =0; i <= arr.length-1; i++) {

    try {

        aSet = aDoc.layerSets.getByName(arr[0]);

        //alert ("GOALL!!! I see the " + arr[0] + " Folder");

        aSet.name = arr[1];

        aSet.translate(arr[2],0) 

    }

    catch (e) {

        //alert ("LayerSet " + arr[0] + " not found")

        }

}

You don't need more if's and else's

1 reply

pixxxelschubser
Community Expert
Community Expert
February 27, 2017

Hi evangelos.vlasopoulos,

try this one

var aDoc = app.activeDocument;

var arr = [["Photos0","New Folder Name 00"],["Photos1","New Folder Name 01"],["Photos2","New Folder Name 02"]];

for (i =0; i <= arr.length-1; i++) {

    try {

        aSet = aDoc.layerSets.getByName(arr[0]);

        //alert ("GOALL!!! I see the " + arr[0] + " Folder");

        aSet.name = arr[1];

    }

    catch (e) {

        //alert ("LayerSet " + arr[0] + " not found")

        }

}

Have fun

Known Participant
February 27, 2017

Thank you very much  for your helpful code! If I can understand correct, this code takes my folders and rename them to other names. I have managed to add your code to mine and I would like to show it to you and also try to explain my "headache"...

I have a document with the following structure:
1 Folder (Photos) - it contains 1 or more Layers (Photo #1, Photo #2 etc) and the Background.

Here you can see how it looks as a document

The next step is to click a button (left or right) to send the "Photos" folder accordingly left by 20cm and rename it to Left Page.

So far I was able with my poor Js knowledge and YOUR HELP to make it work with the following code:

function Move_Page_Left() {

// Save the user's default ruler settings

var DefaultRulerUnits = app.preferences.rulerUnits

// Change the user ruler units to CM

    preferences.rulerUnits = Units.CM;   

   

var aDoc = app.activeDocument; 

var arr = [["Photos","Left Page"]]; 

for (i =0; i <= arr.length-1; i++) { 

    try { 

        aSet = aDoc.layerSets.getByName(arr[0]); 

        aSet.name = arr[1]; 

    } 

    catch (e) { 

        //alert ("LayerSet " + arr[0] + " not found") 

        } 

// Select the Folder

var layerSetRef = aDoc.layerSets.getByName( 'Left Page' ); 

aDoc.activeLayer = layerSetRef; 

// Transform (move) the Folder by 20cm

aDoc.activeLayer.translate(-20,0) 

// Load the user's default ruler settings

app.preferences.rulerUnits = DefaultRulerUnits

}

// The Function

Move_Page_Left();

So this is the ending point for me, I can live with that but it will be great to make it more advanced. For instance, to use the "Go to left Button" for multiple purpoces, not only to do one job.

So I was thinking that a way of if/else command would probably work better. For example I would like to ask the system to try to find the Folder "Photos" and then rename it (Left Page) and move it left 20cm. Or if the folder has different name (example Right Page), rename it (Left page) and move it 40cm.

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
February 27, 2017

I do not understand what do you really want. What you describe: everey time you run the script the foldernames (layer set names) are changing from Photos to Left Page and translate this folder x-20cm and Right Page will also be a Left Page and translate x+40cm.

Sorry. I do not understand this workflow.

But you can do that with a few simple changes in the code I posted before:

var aDoc = app.activeDocument;

var arr = [["Photos","Left Page",-20],["Right Page","Left Page",40]/*,["Left Page","Left Page",0]*/];

for (i =0; i <= arr.length-1; i++) {

    try {

        aSet = aDoc.layerSets.getByName(arr[0]);

        //alert ("GOALL!!! I see the " + arr[0] + " Folder");

        aSet.name = arr[1];

        aSet.translate(arr[2],0) 

    }

    catch (e) {

        //alert ("LayerSet " + arr[0] + " not found")

        }

}

You don't need more if's and else's