9 Replies Latest reply on Feb 12, 2016 9:03 AM by JJMack

    Send keystroke from Javascript to Photoshop ?!

    frankb28754382 Level 1

      Is it possible to send keystrokes from Javascript to Photoshop ?

      I write a HTML5/Javascript panel. E.g. if I want to simulate a keystroke as "5", "50" or "altG"

      which changes e.g. the opacity of a brush. Is there a way in javascript, maybe jQuery to send keystrokes to photoshop?

        • 1. Re: Send keystroke from Javascript to Photoshop ?!
          Chuck Uebele Adobe Community Professional & MVP

          There might be, but I think only some keys can be captured due to security threats of key logging. People don't want scripts that can capture what they type.

          • 2. Re: Send keystroke from Javascript to Photoshop ?!
            frankb28754382 Level 1

            I do not want to capture what people type. If I press a certain button in a panel, I want the javascript to send the according keystroke to Photoshop. As the user would type it by hand. This is to change e.g. opacity, blend modes of the brush/clonestamp tool by a button in a HTML5 panel.As I did not figure out how to change the opacity/blendmode dirctly within javascript, emulating the according keystrokes may be an alternative.

            • 3. Re: Send keystroke from Javascript to Photoshop ?!
              Chuck Uebele Adobe Community Professional & MVP

              Misunderstood, going the other way.

              • 4. Re: Send keystroke from Javascript to Photoshop ?!
                JJMack Most Valuable Participant

                You can program buttons in Photoshop scripts dialogs that when clicked on a function will be triggered to do some Photoshop function like change opacity. change screen mode etc.  Keys need not be passed to Photoshop. The script is in control. The script should provide the function you want.

                • 5. Re: Send keystroke from Javascript to Photoshop ?!
                  cbuliarca Level 3

                  If you want to mimic the pressing of a key you can just run a vbs script or a .command on mac to send a keystroke.

                   

                  if(IsWindowsOS()){
                    var mb_keystrF = new File(File($.fileName).path +"/kestr.vbs");
                      var str = 'WScript.Sleep 1500\
                  Set WshShell = WScript.CreateObject("WScript.Shell")\
                  WshShell.SendKeys "5"\
                  WScript.Sleep 1500\
                  WshShell.SendKeys "%g"';
                      mb_keystrF.open('w');
                      mb_keystrF.write(str);
                      mb_keystrF.close();
                    mb_keystrF.execute();
                  }
                  if(IsMacintoshOS()){
                    var mb_keystrF = new File(File($.fileName).path +"/kestr.command");
                      var str = 'echo\'\
                  delay 1.5\
                  tell application "Photoshop" to activate\
                  delay 0.1\
                  tell application "System Events" to keystroke "5"\
                  delay 0.1\
                  tell application "System Events" to keystroke "g" using option down\
                  delay 0.02\
                  \' | osascript';
                      mb_keystrF.open('w');
                      mb_keystrF.write(str);
                      mb_keystrF.close();
                      mb_keystrF.execute();
                  }
                  
                  
                  function IsMacintoshOS() {
                      if ( $.os.search(/macintosh/i) != -1 ) {
                          return true;
                      } else {
                          return false;
                      }
                  }
                  function IsWindowsOS() {
                      if ( $.os.search(/windows/i) != -1 ) {
                          return true;
                      } else {
                          return false;
                      }
                  }
                  
                  • 6. Re: Send keystroke from Javascript to Photoshop ?!
                    Level 1

                    I have this error

                    permission I checked them and are just

                     

                    Unable to "kestr.command" file because you do not have appropriate access privileges.

                     

                    how can I fix

                    • 7. Re: Send keystroke from Javascript to Photoshop ?!
                      cbuliarca Level 3

                      By default the.command files are not executable you just have to run this command in your terminal:

                      chmod +x path to your file keystr.command

                      • 8. Re: Send keystroke from Javascript to Photoshop ?!
                        frankb28754382 Level 1

                        I will try the WshShell script. I also read about it. One has to activate the window script host.

                        I think I used to read, that this may potentially cause any safety problems? Any general opinions on that ?!

                        Good to know how it should run on macintosh as well.

                        I also tried to send keystrokes via a small program "sendkeys.exe" on windows. That also works, but then

                        the Dos window always pops up, which is not so nice yet.

                         

                        JJMack: I can already call several actione etc from my panel. The problem I have is, that

                        I could not figure out yet how to change the opacity/blendmode of the brush/clonestamp .

                        How to change properties of layers is well documented, but to set those properties for the brush tools

                        is unfortunately not documented, as I see it. I am also not sure if one can directly change them within javascript at all.

                        Therefore, my idea is to send keystrokes to Photoshop, since the opacity/blendmode can be changed by shortcuts.

                        • 9. Re: Send keystroke from Javascript to Photoshop ?!
                          JJMack Most Valuable Participant

                          frankb28754382 wrote:

                           

                          JJMack: I can already call several actione etc from my panel. The problem I have is, that

                          I could not figure out yet how to change the opacity/blendmode of the brush/clonestamp .

                           

                          Script and actions normally only select a tool and initialize its setting for a user to use manually. The Action or Script would then end and the user will use the tool selected the way the action or script set it up. The user would use Photoshop UI to modify the tools setting as needed. 

                           

                          Extension panels are like Photoshop Palettes they are persistent they do not terminate like actions and scripts.  So a panel could have buttons to change tools settings. However; they would be nothing more than additional UI you add for tools.  As you found out, there also is not an easy way to select a tool and set a tools settings in a script.  The only way I know how to select tools and set their setting in Actions and Scripts is, to define Tools Presents then select these tools presets by name in Actions and Scripts.