How can I control keyboard with osascript?

I am trying to work with osascript's System Events from my own program in order to control the Shift key on my keyboard.

To be specific, when I receive certain input from my own program, I press the Shift key, keep it as is, and press the Shift key back when finishing typing. .


The command I am going to run is as follows
Code Block
osascript -e 'tell application "System Events" to key up shift'
 osascript -e 'tell application "System Events" to key down shift'

I recently confirmed that the entire OS freezes for a moment immediately after the above command is executed.
Can you think of any possible causes?


I identified it around January 2020, and I hadn't seen anything specifically like this before that.

If the phenomenon of the whole OS freezing for a second is an osascript specification, can you tell me how to avoid it?

I am trying to work with osascript's System Events from my own program in order to control the Shift key on my keyboard.

Can we take a step back here and talk about your overall goals? This is a very convoluted path you’re taking here and, assuming you’re writing native code, there are APIs you can use to get there a lot more directly. Is there a specific reason you’re using osascript for this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank you for your reply.

The ultimate goal is to "emulate keyboard input by performing specific operations from the application".

Specifically, for the general key (e.g., A key) or the special key (e.g., Shift key), I want to control the taps and toggles from my application.
In summary, there is no special reason to use osascript.

We are currently developing an application using Electron. Because the feasible and easy way to call it from that application is osascript. Therefore we simply use it.

The ultimate goal is to "emulate keyboard input by performing specific operations from the application".

OK. In that case I recommend that you look at either the CoreGraphics event tap mechanism (<CoreGraphics/CGEvent.h>) or the I/O Kit HID mechanism (<IOKit/IOHIDLib.h>). Doing this indirectly via osascript deeply inefficient. You may also run into problems with in your interaction with macOS’s privacy subsystem (see WWDC 2018 Session 702 Your Apps and the Future of macOS Security and WWDC 2019 Session 701 Advances in macOS Security).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank you for the advice.
I have tried creating a Command Line Tool project in Xcode then executing the code [*1], and the Shift key is still being held down.

At that time, I checked how the keyboard was doing by going to "Accessibility > Keyboard > Accessibility Keyboards" and enabling "Enable Accessibility Keyboards".

I pressed the "A" key on the keyboard with the above conditions.
The expected behavior was for it to type "A", but it actually typed "a".

Also, once it accepted the input from the keyboard, the Shift key was released from being held down.
I think there is probably a problem with the code, could you please suggest how to fix it?

[*1] Created code: main.swift
Code Block
import CoreGraphics.CGEvent
let source = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)
let eventShiftKeyDown = CGEvent.init(keyboardEventSource: nil, virtualKey: 0x38, keyDown: true)
eventShiftKeyDown?.post(tap: CGEventTapLocation.cgSessionEventTap)
print("Shift key down.")
sleep(5)
let eventShiftKeyUp = CGEvent.init(keyboardEventSource: nil, virtualKey: 0x38, keyDown: false)
eventShiftKeyUp?.post(tap: CGEventTapLocation.cgSessionEventTap)
print("Shift key up.")

I would avoid doing this in a command-line tool. The problem is that on modern systems this code will trigger an authorisation check, and those checks kinda assume that the call is coming from an app.

Create a new app in Xcode from the macOS > App template and wire up your code into a button in that app. Also, make sure you sign that app using a stable code signing identity (your Apple Development identity will work for this).

I ran this test here in my office (on 10.15.5). The first time I clicked the button in my test app, the system presented an alert:

Code Block
“MyApp" would like to control this computer using
accessibility features.
Grant access to this application in Security & Privacy
preferenecs, located in System Preferences.
[Open System Preferences] [[Deny]]


Once I granted accesses, the program started working.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
How can I control keyboard with osascript?
 
 
Q