Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Misunderstood, going the other way.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!"
Copy link to clipboard
Copied
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)
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
var pth = 'cscript //nologo '+"\""+app.path.fsName+'/Presets/Scripts/right.vbs'+"\"";
alert(pth)
try {
app.system(pth);
} catch(e){
alert(e)
}
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now