Failure to access UI scripting in Terminal not resulting in error

I have a script that call UI scripting on Terminal to create a new tab and since moving to Monterey it looks like it’s not working “as well” as before. It’s kind of random. Sometimes I get the new tab creation, sometimes I get the script-app floating and not resuming anything, but I never get the alert that I set in the script for failing to accessing the UI.

I get the same when I try to debug in SD plus the status bar message that SD can’t send keystrokes but that does not trigger that alert message, plus SD is allowed to send keystrokes in the Accessibility settings.

try
	set myCommand to text returned of (display dialog "" default answer myCommand with title "Launch OmegaT in Terminal") as text
	set the clipboard to myCommand
	display dialog "The OmegaT launch command:

" & myCommand & "

has also been copied to the clipboard."
	tell application "Terminal" to activate
	tell application "System Events" to tell application process "Terminal"
		set frontmost to true
		delay 0.2
		try
			keystroke "t" using {command down}
		on error
			display alert "The script can't create a Terminal tab because it does not have UI access rights.
Modify Preferences > Accessibility to open the command in a new tab."
		end try
		tell application "Terminal"'s front window
			delay 0.2
			do script myCommand in its last tab
			activate
		end tell
	end tell
	return
on error
	display alert "Canceling"
	return
end try

The randomness of the failure and the absence of alert tells me that there is something more than this “can’t send keystroke” thing here…

You can try to add myCommand.command in directory “Scripts”) after creating your App
NOTE: you can use executable file of .command or .sh
you can also use an executable file without extension

See the code bellow, it work on Monterey 12 and Ventura 13

display dialog "
Before selecting OK button you have to close all terminal windows if any are open.
*****************************" with icon note buttons {"Quit", "OK"} cancel button "Quit" default button {"OK"}

set the_command to quoted form of POSIX path of (path to resource "myCommand.command" in directory "Scripts")
tell application "Terminal"
	activate
end tell

tell application "Terminal"
	quit
	
end tell
delay 1
tell application "Terminal"
	activate
	do script the_command in window 1
	
end tell

Thank you for your reply.

I can’t close all the terminal windows when I launch that script. I work with plenty of terminal windows open all the time. Also, I fail to see why your script would work and not mine. Have you identified something in my process that does not work and that is fixed in yours?

That script used to function fine before upgrading to Monterey. Now, it will work for a while until a random moment, I’ll have to remove the app from the Accessibility authorizations, add it again, and it will work again. Until it fails.

I’ve also noticed that with Monterey, the “open .” command does not put the Finder front. It keeps the Terminal front and the Finder is opened without being set to the front.

Same for that command. It used to put the launched application front and now it doesn’t.

I guess I could solve the UI scripting issue by using “do shell script” but that would leave the launching app open while I work…

No, I can’t use “do shell script” because it is a resource hog…

ok I understand but if it works for a while and it fails then there may be other authorization to perform? you say you have several terminal windows running; maybe if you find the logs of this process and find the reason why it fails.
From your script I can’t see what is causing the problem ,surely someone with more experience will be able to better understand the problem.

The script just stalls. Nothing happens. It just waits for something while not triggering the error. I need to force-quit it.

Also, it’s a script/app I’ve used for years. And it’s the Monterey upgrade that messed it.

In the meanwhile, I’ve tried to modify a few things and it looks like the script is less prone to error:

	tell application "System Events" to tell application process "Terminal"
		set frontmost to true
		try
			delay
			keystroke "t" using {command down}
		on error
			display alert "The script can't create a Terminal tab because it does not have UI access rights.
	Modify Preferences > Accessibility to open the command in a new tab."
		end try
	end tell
	tell application "Terminal"'s front window
		delay 0.2
		do script myCommand in its last tab
		activate
	end tell

basically remove the

	tell application "Terminal" to activate

part, and remove the Terminal tell block from the System Events tell block (no reason to have it there).

From which version of macOS? Very old one?
I don’t think /usr/bin/osascript can get GUI Scripting authentication.

It require bundle id or executer’s file path, I assume.

How about to make AppleScript applet and call from terminal?

I’m in Monterey and there’s no issue for me.

1 Like

I was on Big Sur. I changed of machine (same MBP 2015 model, but with a 1tb Apple SSD). But besides for that the system is the same as before.

Show us your System Preferences (Security & Privacy) settings if you can GUI Scripting functions from /usr/bin/osascript.

I was not aware that /usr/bin/osascript needed to be there. I’ve registered it now (is AEServer relevant there ?) I’ll see how it goes.

No​​​​​​​​​​​​​​​​​​​​​​​​​​.

Well, even with /usr/bin/osascript registered it fails to reach the on error part and keeps floating.

That’s really weird.

We got a new worthless knowledge.

/usr/bin/osascript can be registered to System Preferences (Security & Privacy) settings but can not execute GUI scripting function.

I don’t think I have to change AppleScript runtime data.

One thing that is weird is that when I edit the script, it runs fine for a while. Until something happens (machine sleep ? I don’t know).

Anyway, I figured (and it’s probably silly and wrong) that since the on error was blocking the whole thing, I’d rather have the thing crash than float around eternally, I removed that particular GUI scripting test.

I’ll report if I see improvements…

Yes it can.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Ok, interesting. The script worked for a bit, and then failed, and now it does stop at the final on error block (linked to the global try)

Summary: System Events will not recognize its on error block, but the global one works…

What in System Events are you expecting might throw an error? I can see you’ve posted two scripts in this thread, both of which enclose a keystroke command in a try block. In what situation do you anticipate this command not executing successfully?

From what I can see, the only on error block that could conceivably be entered is the global one from your first script, either by cancelling the dialog, or if Terminal doesn’t have any open windows. The inner try block doesn’t appear to have anything to catch.

I’m only loosely following this thread, but, that is not going to work, because the keystroke command and the try block are not being sent to system events.

Try this:

tell application "Terminal" to activate
tell application "System Events"
   set frontmost of application process "Terminal" to true
   delay 0.2
   try
      keystroke "t" using {command down}
   end try
end tell