tell application “System Events” to set frontApp to name of first process whose frontmost is true
tell application “System Events”
tell application process frontApp
set _selection to value of attribute “AXFocusedUIElement”
tell _selection to perform action “AXShowMenu”
Someone helped me with the above script to simulate right mouse click. I use it as a voice command.
It works quite well in native Mac applications.
However, its operation in other applications is unpredictable, almost always without response.
For example, it doesn’t work if it’s invoked on top of an image from a YouTube video; But if the right button has been pressed previously and then the command that calls the script that simulates the right button press is invoked, then it does work even if only momentarily. (It is as if it had been “loaded” when actually performing the right click and then answering the voice command call)
Any ideas to improve the code and extend it to other applications?
Thanks in advance.
It is logical. GUI scripting works only with Cocoa applications. Most of native Mac applications is Cocoa applications. So, your code works with them. With non-Cocoa application you should use some mouse tool, like CliClick or Mouse Tools.
When you press previously (using left click or right click) some element of Cocoa application, this element takes focus. And, as you can see, you code works above the currently focused element. To right-click none focused element, you should indicate full reference to it. Then, you can right click it programatically. Example:
tell application "System Events"
tell application process "Script Editor"
set _selection to text area 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1
tell _selection to perform action "AXShowMenu"
NOTE: As you can see, full reference defers for elements and applications, so your first approach is more universal, when something is focused manually.
Thank you very much for answering.
It is a pleasure to receive perfectly explained and reasoned answers like yours (if you are not already a teacher, I think you have good qualities to be one).
Thanks to them I have understood the reasons that make it practically impossible to write a code to be used as a voice command whose sole purpose is to invoke the simulation of the right mouse button click in any area.
A different issue (which is not the one I want) is that the simulation of the right mouse button press is only part of a script that has another purpose.
I have tried to avoid the use of CliCick, but it seems that it is an alternative to consider.
Thank you very much again and receive my regards.
P.S .: I beg you to apologize for any errors in the writing of the text, as it is the result of using Google Translate.