Intermittent Long Delays with keystroke Command

Hello,

I have a script designed to enter One-Time Passwords into a login field in Firefox. I’ve been using variations on this script for many years without issue, but now any keystroke commands are usually taking ~15 seconds to execute, regardless of the length of the string.

I’m currently running macOS 12.6.8, but started noticing the issues around 12.6.5. The issue occurs intermittently, but once it starts happening, it happens repeatedly. Sometimes it’s fixed by a restart. It seems to happen regardless of what’s running the script (Script Debugger, FastScripts, BetterTouchTool).

A pared-down version of the script is as follows:

use scripting additions
use RL : script "Remote Access Library"

if application "Script Debugger" is frontmost then delay 3 -- For debugging.

-- Generate OTP.
set OTP to RL's getOTP() -- e.g. "123456"

-- Click checkbox.
tell application "System Events" to keystroke tab & space
delay 0.05

-- Focus OTP text field.
tell application "System Events" to keystroke tab using {shift down}
delay 0.05

-- Enter OTP.
tell application "System Events" to keystroke OTP & return

Before this would take ~0.2 seconds to execute; now it takes 45–60 seconds to execute. The hangups happen at the 3 keystroke statements, before any keystrokes are sent (i.e. in the last keystroke, there’s ~15 seconds of waiting, then “123456” all appears almost instantly).

Is anyone experiencing something similar, and does anyone know of any decent workarounds to keystroke? My best guess is that some new security subsystem modification/bug is holding things up.

Hi @tree_frog,

Using the script below with Firefox last version and macOS 12.6.7, the string is correctly entered in a login field. It takes only 0.33 seconds (the 3 delays).

tell application "Firefox"
	activate
	
	-- Generate OTP.
	set OTP to "123456"
	delay 0.1
	
	-- Click checkbox.
	tell application "System Events" to keystroke tab & space
	delay 0.1
	
	-- Focus OTP text field.
	tell application "System Events" to keystroke tab using {shift down}
	delay 0.1
	
	-- Enter OTP.
	tell application "System Events" to keystroke OTP & return
	
end tell

I have several scripts that uses the keystroke or key code command. None is causing any issue of this kind.
Did you try the keystroke command on another app?

1 Like

Thanks for testing, Jonas!

It doesn’t appear to depend on the application. I rebooted a few times & it’s back to working normally today… but it’ll fail again in the next few hours, days or weeks. That’s been the pattern so far. It makes it really hard to track down what’s causing the issue.

There’s no specific code that actually targets a specific GUI application (such as Firefox); it’s all System Events.

A couple of days later, it’s back to happening on my iMac. It’s also happening on my M1 MBP.

It doesn’t appear to be affected by whatever application is frontmost; if I run it with TextEdit frontmost instead of Firefox, the bug still happens. Quitting & re-launching the application running the script does not fix things. BetterTouchTool indicates that Secure Keyboard Input is not active.

If anyone has any thoughts, I’d still really appreciate it. It’s frustrating to have to reboot the machines every time this happens.

I wonder if you can try Keyboard Maestro (or another macro utility) to see if you have the same issue.

If not, then you can switch to the macro utility, which hopefully will eliminate all the frustration.

1 Like

That may be the next step, or perhaps creating a web view with Automator so that I can directly target the text fields with the Accessibility framework.

I have quite a few scripts that rely on keystrokes, though, so I’d still really like to pinpoint the bug.