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
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…
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).
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.
Yes it can.
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