4 Replies Latest reply on Feb 27, 2017 1:04 PM by evangelos.vlasopoulos

    Jsx code to find a specific Folder and Rename it

    evangelos.vlasopoulos

      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!

        • 1. Re: Jsx code to find a specific Folder and Rename it
          pixxxel schubser MVP & Adobe Community Professional

          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[i][0]);
                  //alert ("GOALL!!! I see the " + arr[i][0] + " Folder");
                  aSet.name = arr[i][1];
              }
              catch (e) {
                  //alert ("LayerSet " + arr[i][0] + " not found")
                  }
          }
          

           

          Have fun

           

          1 person found this helpful
          • 2. Re: Jsx code to find a specific Folder and Rename it
            evangelos.vlasopoulos Level 1

            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.
            Screen Shot 2017-02-27 at 14.56.34.png
            Here you can see how it looks as a document

            Screen Shot 2017-02-27 at 15.01.53.png

            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.

            Screen Shot 2017-02-27 at 15.04.05.png

             

            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[i][0]); 

                    aSet.name = arr[i][1]; 

             

             

                } 

                catch (e) { 

                    //alert ("LayerSet " + arr[i][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.

            • 3. Re: Jsx code to find a specific Folder and Rename it
              pixxxel schubser MVP & Adobe Community Professional

              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[i][0]);
                      //alert ("GOALL!!! I see the " + arr[i][0] + " Folder");
                      aSet.name = arr[i][1];
                      aSet.translate(arr[i][2],0)  
                  }
                  catch (e) {
                      //alert ("LayerSet " + arr[i][0] + " not found")
                      }
              }
              

               

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

              • 4. Re: Jsx code to find a specific Folder and Rename it
                evangelos.vlasopoulos Level 1

                Thank you very much for your reply and your valuable help!


                It's a bit complicated to explain what I want from the code when you don't see my Panel.
                in User Interface of my Panel, there are two buttons <-- (left) and --> (right).
                The first thing I need  is when I click on left or right button to rename the basic layerSet "Photos" to "Left Page" or "Right Page" and move this folder left (-20cm) or right (20cm).

                The second and probably more advanced feature is to be able to select the (old "Photos" and newily renamed) "Left Page" LayerSet and when I press the --> (right) button to rename it to "Right Page" and move it accordingly to the right side.

                 

                So actually I need a set of buttons that do more than two things, that's why I thought the "if/else" would be ideal.

                 

                I believe your code will help me build that!