3 Replies Latest reply on Nov 20, 2013 7:26 AM by Ghoulfool

    Adding a layer, filling it and removing it again

    loopstar

      Hi

      I am trying to understand scripting, but I guess I am not getting it.

      I have script made by Paul Riggott (thank you, again Paul) which takes a png icon and makes the three versions I need the file in for our desktop java application.

      Now I need a diffferent version of the script for some of these icons, where a opaque background is added in the "h" version. This is for mouse hovering over the icon, giving it a bit more button-feel.

       

      I have been hacking and stealing script-bits from around the forum, and of course it is not working.

      Here's the code:

       

      #target photoshop
      main();
      function main(){
      var inFolder = Folder.selectDialog("Please select folder to process"); 
      var fileList = inFolder.getFiles("*.png");
      if(fileList.length == 0 ) return;
      var outputFolder = Folder (inFolder+"/Commit icons");
      if(!outputFolder.exists) outputFolder.create();
      for(var a in fileList){
      var png = open(fileList[a]);
      var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
      var saveFile = File(outputFolder + "/" + Name + "_16_n_p.png");
      SavePNG(saveFile);
      //stuff I added
      activeDocument.artLayer.add();
      var myColor = new SolidColor();
      myColor.rgb.red = 160;
      myColor.rgb.green = 160;
      myColor.rgb.blue = 160;
      activeDocument.selection.fill( myColor);
      docRef.artLayers[0].move(docRef.artLayers[docRef.artLayers.length-1], ElementPlacement.PLACEAFTER);
      saveFile = File(outputFolder + "/" + Name + "_16_h_p.png");
      SavePNG(saveFile);
      activeDocument.activeLayer.remove;
      //end of stuff I added
      activeDocument.activeLayer.opacity=60;
      activeDocument.changeMode(ChangeMode.GRAYSCALE);
      saveFile = File(outputFolder + "/" + Name + "_16_d_p.png");
      SavePNG(saveFile);
      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          }
      };
      function SavePNG(saveFile){
          pngSaveOptions = new PNGSaveOptions(); 
      activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
      };
      

      I hope someone can help me with this, and maybe even explain what I am doing wrong.

       

      Thanks

        • 1. Re: Adding a layer, filling it and removing it again
          loopstar Level 1

          this made it work:

           

          #target photoshop

          main();

          function main(){

          var inFolder = Folder.selectDialog("Please select folder to process");

          var fileList = inFolder.getFiles("*.png");

          if(fileList.length == 0 ) return;

          var outputFolder = Folder (inFolder+"/Commit icons");

          if(!outputFolder.exists) outputFolder.create();

          for(var a in fileList){

          var png = open(fileList[a]);

          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

          var saveFile = File(outputFolder + "/" + Name + "_16_n_p.png");

          SavePNG(saveFile);

           

          activeDocument.artLayers.add();

          var myColor = new SolidColor();

          myColor.rgb.red = 160;

          myColor.rgb.green = 160;

          myColor.rgb.blue = 160;

          activeDocument.selection.fill( myColor);

          activeDocument.artLayers[0].move(activeDocument.artLayers[activeDocument.artLayers.length- 1], ElementPlacement.PLACEAFTER);

          saveFile = File(outputFolder + "/" + Name + "_16_h_p.png");

          SavePNG(saveFile);

          activeDocument.activeLayer.remove();

           

          activeDocument.activeLayer.opacity=60;

          activeDocument.changeMode(ChangeMode.GRAYSCALE);

          saveFile = File(outputFolder + "/" + Name + "_16_d_p.png");

          SavePNG(saveFile);

          app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

              }

          };

          function SavePNG(saveFile){

              pngSaveOptions = new PNGSaveOptions();

          activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

          };

          • 2. Re: Adding a layer, filling it and removing it again
            loopstar Level 1

            Wish I could give "correct" to my self, as I feel like a superstar right now :-)

            • 3. Re: Adding a layer, filling it and removing it again
              Ghoulfool Level 1

              You're nearly there:

              #target photoshop
              main();
              function main(){
              var inFolder = Folder.selectDialog("Please select folder to process"); 
              var fileList = inFolder.getFiles("*.png");
              if(fileList.length == 0 ) return;
              var outputFolder = Folder (inFolder+"/Commit icons");
              if(!outputFolder.exists) outputFolder.create();
              for(var a in fileList){
              var png = open(fileList[a]);
              var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
              var saveFile = File(outputFolder + "/" + Name + "_16_n_p.png");
              SavePNG(saveFile);
              
              
              //stuff I added
              var layerRef = app.activeDocument.artLayers.add()
              layerRef.name = "fill";
              var myColor = new SolidColor();
              myColor.rgb.red = 160;
              myColor.rgb.green = 160;
              myColor.rgb.blue = 160;
              activeDocument.selection.fill( myColor);
              
              // Move the new layer after the current layer
              // =======================================================
              var id3870 = charIDToTypeID( "move" );
              var desc660 = new ActionDescriptor();
              var id3871 = charIDToTypeID( "null" );
              var ref421 = new ActionReference();
              var id3872 = charIDToTypeID( "Lyr " );
              var id3873 = charIDToTypeID( "Ordn" );
              var id3874 = charIDToTypeID( "Trgt" );
              ref421.putEnumerated( id3872, id3873, id3874 );
              desc660.putReference( id3871, ref421 );
              var id3875 = charIDToTypeID( "T   " );
              var ref422 = new ActionReference();
              var id3876 = charIDToTypeID( "Lyr " );
              ref422.putIndex( id3876, 0 );
              desc660.putReference( id3875, ref422 );
              var id3877 = charIDToTypeID( "Adjs" );
              desc660.putBoolean( id3877, false );
              executeAction( id3870, desc660, DialogModes.NO );
              
              
              
              //app.activeDocumentdocRef.artLayers[0].move(docRef.artLayers[docRef.artLayers.length-1], ElementPlacement.PLACEAFTER);
              saveFile = File(outputFolder + "/" + Name + "_16_h_p.png");
              SavePNG(saveFile);
              activeDocument.activeLayer.remove;
              //end of stuff I added
              activeDocument.activeLayer.opacity=60;
              activeDocument.changeMode(ChangeMode.GRAYSCALE);
              saveFile = File(outputFolder + "/" + Name + "_16_d_p.png");
              SavePNG(saveFile);
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                  }
              };
              function SavePNG(saveFile){
                  pngSaveOptions = new PNGSaveOptions(); 
              activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
              };