7 Replies Latest reply on Feb 20, 2016 9:47 AM by pixxxel schubser

    Rename layers - unsaved file name + suffix

    kajzica

      Hi

      I have jsx script which can rename layers with current file name + suffix. Script select layer by names. But, this work if file is saved to disk.

      Need to modify script to use name even if file is not previously save to disk, like freshly opened pdf file?

       

      var doc = app.activeDocument;

      doc.activeLayer = doc.artLayers.getByName("Layer 2");

      app.activeDocument.activeLayer.name = decodeURI(app.activeDocument.name).match(/(.*)(\.[^\.]+)/)[1] + "_Zadnjica";

      doc.activeLayer = doc.artLayers.getByName("Layer 3");

      app.activeDocument.activeLayer.name = decodeURI(app.activeDocument.name).match(/(.*)(\.[^\.]+)/)[1] + "_Hrbat";

      doc.activeLayer = doc.artLayers.getByName("Layer 4");

      app.activeDocument.activeLayer.name = decodeURI(app.activeDocument.name).match(/(.*)(\.[^\.]+)/)[1] + "_Prednjica";

        • 1. Re: Rename layers - unsaved file name + suffix
          pixxxel schubser MVP & Adobe Community Professional

          This is not really the best way. But you can do something like that with your existing code:

          var doc = app.activeDocument;
          var yourWishNamePart2Save = "NewSaveName";
          // you have to check if the following layers really exists !!!
          doc.activeLayer = doc.artLayers.getByName("Layer 2");
          app.activeDocument.activeLayer.name = yourWishNamePart2Save + "_Zadnjica";
          doc.activeLayer = doc.artLayers.getByName("Layer 3");
          app.activeDocument.activeLayer.name = yourWishNamePart2Save + "_Hrbat";
          doc.activeLayer = doc.artLayers.getByName("Layer 4");
          app.activeDocument.activeLayer.name = yourWishNamePart2Save + "_Prednjica";
          
          // additional you need as save function with yourWishNamePart2Save + extension
          
          

           

          Have fun

           

          • 2. Re: Rename layers - unsaved file name + suffix
            kajzica Level 1

            but this wont get current name from image window

            Untitled-1.png

            • 3. Re: Rename layers - unsaved file name + suffix
              pixxxel schubser MVP & Adobe Community Professional

              Ok.

              Try this one

              var doc = app.activeDocument;
              var docName = doc.name;
              var namePart = decodeURI(docName).match(/(.*)(\.[^\.]+)/)[1];
              // you have to check if the following layers really exists !!!
              doc.activeLayer = doc.artLayers.getByName("Layer 2");
              app.activeDocument.activeLayer.name = namePart + "_Zadnjica";
              doc.activeLayer = doc.artLayers.getByName("Layer 3");
              app.activeDocument.activeLayer.name = namePart + "_Hrbat";
              doc.activeLayer = doc.artLayers.getByName("Layer 4");
              app.activeDocument.activeLayer.name = namePart + "_Prednjica";
              
              // additional you need a save function
              
              

              :

              • 4. Re: Rename layers - unsaved file name + suffix
                kajzica Level 1

                script is ok but with changing all "namePart" to doc.name

                 

                tnx

                • 5. Re: Rename layers - unsaved file name + suffix
                  pixxxel schubser MVP & Adobe Community Professional

                  kajzica schrieb:

                   

                  … but with changing all "namePart" to doc.name

                   

                  tnx

                  I thought, this is not what you want? Or?

                   

                  In this case you can remove line #03 and replace namePart with docName.

                  Than is this the code you need:

                  var doc = app.activeDocument;
                  var docName = doc.name;
                  // you have to check if the following layers really exists !!!
                  doc.activeLayer = doc.artLayers.getByName("Layer 2");
                  app.activeDocument.activeLayer.name = docName + "_Zadnjica";
                  doc.activeLayer = doc.artLayers.getByName("Layer 3");
                  app.activeDocument.activeLayer.name = docName + "_Hrbat";
                  doc.activeLayer = doc.artLayers.getByName("Layer 4");
                  app.activeDocument.activeLayer.name = docName + "_Prednjica";
                  
                  // additional you need as save function
                  
                  
                  
                  • 6. Re: Rename layers - unsaved file name + suffix
                    kajzica Level 1

                    Ok, name is added

                    Now, is it possible to add size in cm as suffix? Cropped layer size, without transparent pixels

                    Name_Zadnjica_23,5cm x 50,4cm?

                     

                    I have some modified code from export layers to files for exporting with dimension in name but want to have it in layer name.

                     

                            var fileNameBody = fileNamePrefix;

                            fileNameBody += "_" + zeroSuppress(i, 4);

                            fileNameBody += "_" + layerName;

                            fileNameBody = fileNameBody.replace(/[:\/\\*\?\"\<\>\|]/g, "_");  // '/\:*?"<>|' -> '_'

                            if (fileNameBody.length > 120) {

                      fileNameBody = fileNameBody.substring(0,120);

                      }

                     

                     

                             var x = 1;

                             var Width = duppedDocumentTmp.width.as('cm')/x; 

                             var Height = duppedDocumentTmp.height.as('cm')/x;

                             Width = Width.toFixed(1).toString().replace(/\./g,',');

                             Height = Height.toFixed(1).toString().replace(/\./g,',');

                             var suffix = Width +"cm x "+Height+"cm";

                             fileNameBody += "_" + suffix;

                     

                     

                            saveFile(duppedDocumentTmp, fileNameBody, exportInfo);

                            docRef.flatten();

                            duppedDocumentTmp.close(SaveOptions.DONOTSAVECHANGES);

                     

                     

                            dupObj.artLayers[i].visible = false;

                        }

                    • 7. Re: Rename layers - unsaved file name + suffix
                      pixxxel schubser MVP & Adobe Community Professional

                      I'm sure, this is enough stuff for a new thread.