Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Send keystroke from Javascript to Photoshop ?!

Community Beginner ,
Feb 11, 2016 Feb 11, 2016

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?

TOPICS
Actions and scripting
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Feb 11, 2016 Feb 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 11, 2016 Feb 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2016 Feb 11, 2016

Misunderstood, going the other way.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2016 Feb 11, 2016

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.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 12, 2016 Feb 12, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2016 Feb 12, 2016

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.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 11, 2016 Feb 11, 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;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 12, 2016 Feb 12, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 12, 2016 Feb 12, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 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!"

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 16, 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)
        }

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 16, 2021 Jan 16, 2021
Why don't you use the File.execute() method as suggested in this thread above?
 
 
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 16, 2021

I do use a part of that, but through a vbs file. I tried adding the shell command inside the script but cant get it working.

 

When i use File.execute is that even extendedscript language, i thought those were shell commands?

The script above doesnt do anything when i add it as is above.

 

EDIT

i do get it to work when i write to desktop. It doesnt work when i write to the photoshop folder or any other fodler. I did notice this same behavior earlier today. I made a couple vbs files with keystrokes and it wont execute from any folder other that desktop or documents, perhaps some other user folder. Probably dude to admin rules orso. I tried running photoshop as administrator but that doesnt seem to work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 16, 2021

Quite weird i changed permission of the program folder, i unchecked "Read-only" and now i can write files to photoshop folder. It states its "Only applies to files in folders". But this is a new file so its kinda weird it wont allow it. Now with that unchecked it seems to work. using file.execute is much nicer since i dont see that shell window for a split millisec or so.

 

Im working on an animation  panel for photoshop and its quite nice being able to use key combos. This allow me to make a global timeline scrubber. I already had it working for OSX, but Windows i could not test. But now that i got a Windows machine since a couple weeks and finally had the time to test and try this part. Tookme quite some hours to figure this one out. But i think its nice.

 

The video is perhaps not very clear. But when doing animations and you are zoomed in, its quite tedious to use the horizontal scrollbar. Therefor i developed the global time scrubber. Its basically the complete timeline and clicking anywhere will set the playhead to that part and it will jump to that section

 

Example global timeline scrubberExample global timeline scrubber

 

PS is there a command to get the CEP folder. I cant remember it and cant seem to find it no more. I thought i use Folder(fsName).path but that return photoshop folder. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 16, 2021

Well i was cheering to soon. I had placed an old vbs file in that folder with the same code. It does excute now files from that folder, but still doesnt allow me to wright new files to the script folder using this code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 16, 2021 Jan 16, 2021

I found a workaround, using function from _lastLogEntry to create a folder in the roaming folder it now works. I actually prefer this method since users wont need to install file somewhere in the system.

 

// Create AnimDessin folder romaing
function getPrefFolder(){
  var userData = Folder.userData;
  if (!userData || !userData.exists) {
    userData = Folder("~");
  }
  var folder = new Folder(userData + "/animdessin2");

  if (!folder.exists) {
    folder.create();
  }
  return folder;
};

function ketpress(os){
   if (os == "Windows") {
      var mb_keystrF = new File(File(getPrefFolder()+'/amd2-shortcut.vbs'));
            var str = 'Set WshShell = WScript.CreateObject("WScript.Shell")\
                      WshShell.SendKeys "{RIGHT}"';
            mb_keystrF.open('w');
            mb_keystrF.write(str);
            mb_keystrF.close();
            mb_keystrF.execute();
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 17, 2021 Jan 17, 2021

 

I misunderstood your problem.
Give an example of a script (and a situation) when something does not work for you.
 
I also recommend trying the AutoIt program.
An example of a script sending "Ctrl + S"
 
WinActivate("[CLASS:Photoshop]")
send("^s")
 
Also, you can compile any script into an exe file and just run it to transfer keys and other clicks to Photoshop.
 
 
 
 
 
 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 17, 2021 Jan 17, 2021

@r-bin thanks!

 

I was thinking of using apps to do this, but i think that takes away the ease of use of panel. Plus perhaps users could get a bit wary of what will be done.

 

An example, which didnt work earlier, is this one

PS app.path looks to be commented out, but its not, this JavaScript previewer diesnt understand this one it seems

 

 

 

var path = 'cscript //nologo '+app.path+'/Presets/Scripts/right.vbs';
try {
    app.system(path);
} catch(e){
    alert(e)
}

 

 

 

I would see the window flash like millisecond or so, but nothing happens.

Also, when i used userData folder. But know with this other method of opening and executing it some path do work which earlier did not work for me. 

 

My guess was because app.path or Folder.userData return wrong encoded path of folder. It has all slashes and no c:. So i tried adding the path directing, thus copy/paste the address from explorer. Still nothing, i only see the window flash really quick. I though perhaps the issue is the spaces in the folder names. Im not sure if that would matter though?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 17, 2021 Jan 17, 2021

 

First, you cannot use a variable var path outside a function (in the global scope), because it conflicts with the app.path property (which by the way is a file, not a string).
 
Secondly, this corrected script works, I checked.
Inside the vbs file, I put only the MsgBox (1111) command to check.
 
var pth = 'cscript //nologo '+"\""+app.path.fsName+'/Presets/Scripts/right.vbs'+"\"";

alert(pth)

try {
    app.system(pth);
} catch(e){
    alert(e)
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2022 Mar 23, 2022
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines