2

I have been trying to figure this out for a while now and was wondering if you could help.

With AppleScript, there are two hidden functions available in the System Events application which allow you to push a key down (keeping it down while other actions, like clicking are done), and then bringing the key back up again.

e.g. see the answer here Applescript to run Detect Displays whereby "key up" and "key down" are used while a click is being performed.

But for the sake of this example, let's use something very simple, a Save shortcut.

The AppleScript looks like this:

tell application "System Events"
    key down command
    keystroke "s"
    key up command
end tell

The closest I've gotten to this in JXA is as follows, but no matter what I pass to keyDown, it sends the letter "a":

events = Application("System Events")
events.keyDown("command")
events.keystroke("s")
events.keyUp("command")

Any ideas?

Thanks!
Fotis

Community
  • 1
  • 1
Fotis Gimian
  • 538
  • 5
  • 13
  • Never use `key up` / `key down` in `System Events` unless you have no other choice. Here you have: `keystroke "s" using command down` – vadian Aug 17 '15 at 07:45
  • As per my post, I only used cmd + s as an example. The originally aim was to reproduce the AppleScript at the post http://stackoverflow.com/questions/12640643/applescript-to-run-detect-displays which I believe must use key down and up actions as the script clicks a button while the key is held down. You can see my completed script at https://github.com/fgimian/macbuild/blob/master/detectdisplays.js – Fotis Gimian Aug 18 '15 at 08:53
  • 1
    Got it, I just wanted to mention that the usage of `key up` / `key down` is pretty dangerous. If an error occurs while the key is down, it stays down! – vadian Aug 18 '15 at 08:58
  • @vadian absolutely agreed! This is why it's critical to use a try / catch block as I have in my detectdisplay.js script. I tested this quite thoroughly and confirmed that it works very reliably, but it is indeed dangerous without exception handling. – Fotis Gimian Aug 20 '15 at 00:29

2 Answers2

7

Ok, the solution was tricky to find but I've got it :)

You may use the special strings eCmd (command), eOpt (option) and eCnt (control) to accomplish this as defined in SystemEvents.h.

As such, the following code works!

events = Application("System Events")
events.keyDown("eCmd")
events.keystroke("s")
events.keyUp("eCmd")

Hope this helps someone out there.

Fotis Gimian
  • 538
  • 5
  • 13
1

what about:

events = Application("System Events")
events.keystroke("s", {
    using:"command down"
});

Have a nice day, Michael / Hamburg

ShooTerKo
  • 2,242
  • 1
  • 13
  • 18
  • Appreciate the response, and this would indeed be a better way to implement the code I posted. However, as per my post, I was attempting to reproduce the AppleScript at http://stackoverflow.com/questions/12640643/applescript-to-run-detect-displays and only provided my Cmd + s script as an example to simplify my question. – Fotis Gimian Aug 18 '15 at 08:55