Skip to main content
Participating Frequently
February 11, 2016
Question

Send keystroke from Javascript to Photoshop ?!

  • February 11, 2016
  • 2 replies
  • 4225 views

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?

This topic has been closed for replies.

2 replies

Inspiring
February 12, 2016

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;

    }

}

schroef
Inspiring
January 16, 2021

Could you elaborate how to do the Windows version. I tried this and nothing happens. No errors, nothing. i used app.system() with the code between the brackets. PS im running it from an jsx file.

 

i got a working OSX version which is this

 

app.system( 'osascript -e \'tell application "System Events" to key code "124"\'' ); 

 

EDIT

I just found this and tried a simple example. it opens notepad and adds the text. Now i jsut need to find the code to use keypress. source

app.system('cscript //logo C:/Users/romboutversluijs/Desktop/example.vbs')

this is the vbs file source

'VBScript Example
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "%windir%\notepad.exe"
WshShell.AppActivate "Notepad"

WshShell.SendKeys "Hello World!"

 

Brainiac
January 17, 2021

@cbuliarca 

 

I found something which sort of works, it will show a the cscript window for a millisecond but it does run the vbs file. The only thing i dont understand is why the vbs file wont run from inside roaming folder or photoshop folder. I only get it to work running from desktop or documents folder. Probably some admin issue i guess.

ive tested lots of folders, userdata, commonfiles, appfolder. I wont run, ill see if i can find more info about cscript

 

// alert(File("C:/Program Files/Common Files/Adobe/CEP/extensions/com.sbaril.animdessin2/jsx/arrowright.vbs").exists)
        var path = 'cscript //nologo C:/Users/romboutversluijs/documents/Adobe/CEP/AnimDessin2/arrowright.vbs'; // ROmaing folder
        try{

            app.system(path);
        } catch(e){
            alert(e)
        }

 

 


Why don't you use the File.execute() method as suggested in this thread above?
 
 
Chuck Uebele
Community Expert
February 11, 2016

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.

Participating Frequently
February 11, 2016

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.

Chuck Uebele
Community Expert
February 11, 2016

Misunderstood, going the other way.