Dup Ver Goto 📝

SystemEvents

PT2/macos/applescript does not exist
To
60 lines, 163 words, 1181 chars Page 'SystemEvents' does not exist.

Examples

Note you have to enable permissions for this sort of thing.

Cmd-Tab

#!/usr/bin/osascript
tell application "System Events"
  key code 48 using (command down)
end tell

Key sender

#!/usr/bin/env python

import json
import subprocess
import sys

with open("mackeys.json") as f:
  mackeys = json.load(f)

mods = {
  "S": "shift",
  "A": "option",
  "M": "command",
  "C": "control"
}

args = sys.argv[1:]

lines = []
for arg in args:
  xs = arg.split("-")
  k = xs.pop().lower()
  ms = []
  for x in xs:
    if x in mods:
      x = mods[x]
    ms.append(x)
  if not k in mackeys:
    print("Don't know",k)
    continue
  kc = mackeys[k]
  line = f"  key code {kc}"
  if len(ms) > 0:
    y = ", ".join(f"{t} down" for t in ms)
    line += f" using ({y})"
  lines.append(line)

lines = "\n".join(lines)
script = f'''tell application "System Events"
{lines}
end tell'''

subprocess.run(["osascript"],input=script.encode())

Uses mackeys.json (which was produced from the table found at the macbiblioblog link below).

Resources