4 Replies Latest reply on Oct 20, 2017 5:49 AM by Feast

    Activate a button when pressing Enter [Photoshop]

    Feast Level 1

      Hello!
      I would like that when the user presses the Enter key, it presses the button called "Export" in my script.
      Or if I could just know when the player presses Enter to call a function that would work too.

       

      Here's the script I'm trying to use it for (javascript), it exports PNG's from all the groups in your active document. If someone's interested:

      // for saving files
      var doc = app.activeDocument;
      var Path = doc.path;  // set the .png next to .psd
      //Path = "~/Desktop/test"; // personal folder
      Path = "D:\\GameDevelopment";
      
      
      var Name = doc.name.replace(/\.[^\.]+$/, '');
      var Prefix = "PROP_";
      var Suffix = "";
      
      
      var doc = app.activeDocument;
      
      
      // AUTOMATIC OPTIONS <= CHANGE HERE
      var x = 512;
      var y = 512;
      var ActivateBlur = true;
      var BlurStrength = 10;
      var saveFile = File(Path + "/" + Name + Suffix + ".jpg");
      // AUTO OPTIONS
      
      
      var dlg = new Window('dialog', 'PNG Batch Save');
      dlg.onEnterKey = function() { Algorithm();};
      
      
      var G1 = dlg.add('group', undefined);  
      dlg.msgSt = G1.add('statictext', undefined,
      'Path:');
      dlg.addEventListener('enterKey', Algorithm);
      
      
      dlg.msgSt.alignment = [ScriptUI.Alignment.LEFT,
      ScriptUI.Alignment.TOP]
      
      
      dlg.msgEt = G1.add('edittext', [0,0,400,20],
      Path, {name: "path",multiline:false,noecho:false,readonly:false});
      
      
      // Resolution UI
      dlg.msgPnl = dlg.add('panel', undefined, 'Resolution');
      dlg.msgPnl.orientation = "row";
      dlg.titleSt = dlg.msgPnl.add('statictext', undefined,
      'X:');
      dlg.titleEt = dlg.msgPnl.add('edittext', undefined,
      x, {name: "x"});
      
      
      dlg.titleSt = dlg.msgPnl.add('statictext', undefined,
      'Y:');
      dlg.titleEt = dlg.msgPnl.add('edittext', undefined,
      y, {name: "y"});
      
      
      
      
      // Blur UI
      dlg.msgPnl = dlg.add('panel', undefined, 'Blurred');
      dlg.msgPnl.orientation = "row";
      dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,
      '+ Blurred Version', {name:"isBlur"});
      dlg.msgSt = dlg.msgPnl.add('edittext', undefined,
      BlurStrength, {name:"blurStrength"});
      dlg.hasBtnsCb.value = ActivateBlur;
      ActivateBlur = dlg.hasBtnsCb.value;
      
      
      // Buttons UI
      dlg.btnPnl = dlg.add('panel', undefined, 'Save');
      dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Export');
      dlg.btnPnl.buildBtn.addEventListener('click', Algorithm);
      dlg.btnPnl.onClick = Algorithm;
      dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Cancel');
      
      
      dlg.show();
      
      
      // Algorithm must be called when clicking on Export button
      function Algorithm() 
      {
          GetUIValues();    
          
          dlg.close();
          
          var doc = app.activeDocument;
          // need an array to know which grp was on or off
          var oldLayers = new Array();
          for (var i = 0; i < doc.layerSets.length; i++) {
              if (doc.layerSets[i].visible == true) {
                  oldLayers.push(1);
              };
      
      
              if (doc.layerSets[i].visible == false) {
                  oldLayers.push(0); // not visible
              };
          };
      
      
      
      
          // set all groups off
          for (var i = 0; i < doc.layerSets.length; i++) {
              doc.layerSets[i].visible = false;
          }
      
      
          for (var i = 0; i < doc.layerSets.length; i++) {
              // if the layer used to be visible
              if (oldLayers[i] == 1) {
                  //alert("Found a visible grp!");
                  // we set it visible
                  doc.layerSets[i].visible = true;
      
      
                  TrimResizeAndSave(doc.layerSets[i]);
                  
                  if (ActivateBlur) CreateBlurredVersion(doc.layerSets[i]);
      
      
                  // we set it invisible again
                  doc.layerSets[i].visible = false;
              };
          }
      
      
          // set whichever groups were originally on visible again
          for (var i = 0; i < doc.layerSets.length; i++) {
              if (oldLayers[i] == 1)
                  doc.layerSets[i].visible = true;
          }
      }
      
      
      
      
      function GetUIValues() {
          //alert(dlg.findElement("x").text);
          x = dlg.findElement("x").text;
          //alert(dlg.findElement("y").text);
          y = dlg.findElement("y").text;
          //alert(dlg.findElement("blurStrength").text);
          BlurStrength = dlg.findElement("blurStrength").text;
          //alert(dlg.findElement("isBlur").value);
          ActivateBlur = dlg.findElement("isBlur").value;
      
      
          Path = dlg.findElement("path").text;
          
          var c = /\\/g;
          if (Path.match(c)) {
      //~         alert("found \\");
      Path = Path.replace(c, "/");
          };
      
      
          c = ':';
          if (Path.match(c )) {
      //~         alert("found :");
              Path = Path.replace(c , '');
          };
      
      
          Path = "/" + Path;
      }
      
      
      function SaveJPEG(saveFile, jpegQuality) {
          jpgSaveOptions = new JPEGSaveOptions();
          jpgSaveOptions.embedColorProfile = true;
          jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
          jpgSaveOptions.matte = MatteType.NONE;
          jpgSaveOptions.quality = jpegQuality;
          activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
      }
      
      
      function SavePNG(saveFile) {
          pngSaveOptions = new PNGSaveOptions(9, false);
          activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
      }
      
      
      function TrimResizeAndSave(group) {
          var doc = app.activeDocument;
          var savedState = doc.activeHistoryState;
      
      
          // trim
          doc.trim(TrimType.TRANSPARENT, true, true, true, true);
      
      
          // resize algorithm
          if (doc.height > doc.width) {
              doc.resizeImage(null, UnitValue(y, "px"), null, ResampleMethod.AUTOMATIC);
          }
          else {
              doc.resizeImage(UnitValue(x, "px"), null, null, ResampleMethod.AUTOMATIC);
          }
      
      
          // we save the ouput
          saveFile = File(Path + "/" + Prefix + group.name + Suffix + ".png");
          SavePNG(saveFile);
          
      
      
          doc.activeHistoryState = savedState;
      }
      
      
      function CreateBlurredVersion(group)
      { 
          var doc = app.activeDocument;
          var savedState = doc.activeHistoryState;
      
      
          // trim
          doc.trim(TrimType.TRANSPARENT, true, true, true, true);
      
      
          // resize algorithm
          if (doc.height > doc.width) {
              doc.resizeImage(null, UnitValue(y, "px"), null, ResampleMethod.AUTOMATIC);
          }
          else {
              doc.resizeImage(UnitValue(x, "px"), null, null, ResampleMethod.AUTOMATIC);
          } 
          
          // blurred output
          var blurredPath = File(Path + "/" + "BLURRED_" + Prefix + group.name + Suffix + ".png");
          
          // transform document to smart object
           var result = createSmartObject(group);
           result.applyGaussianBlur(BlurStrength, BlurStrength);
          
          // save
          SavePNG(blurredPath);
          
          doc.activeHistoryState = savedState;
      }
      
      
      // create smartobject from specified layer or layerSet/group
      function createSmartObject(layer)
      {
         var doc = app.activeDocument;
         var layer = layer != undefined ? layer : doc.activeLayer;
      
      
         if(doc.activeLayer != layer) doc.activeLayer = layer;
      
      
         try
         {
            var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
            executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
            return doc.activeLayer;
         }
         catch(e)
         {
            return undefined;
         }
      }// JavaScript Document