Thursday, June 4, 2020

#1 2020-03-08 03:52:22 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 270

GUI contextual menu

To make GUI Scripting and access the contextual menu could be tricky.

I have test this on Safari and Finder and every application is different.

This example use 'Finder' so the first line in the code will activate it.
activate application "Finder"

This is the code that give the information we need.

Applescript:

tell application "System Events"
   set theApplication to first application process whose frontmost is true
   set theControl to value of attribute "AXFocusedUIElement" of theApplication
end tell

This is the result of the above code.

outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Downloads" of application process "Finder" of application "System Events"



We will copy the code from outline 1 -> window and include 1 so we will have a line of this

outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1



and include in the end of above line with

to perform action "AXShowMenu"




Now we will have a function to access contextual menu in 'Finder'

Applescript:

tell application "System Events" to tell application process "Finder"
   set frontmost to true
   tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to perform action "AXShowMenu"
end tell

It could be a good advice to select a item first in Finder before execute below code.

Applescript:

tell application "System Events" to tell application process "Finder"
activate
--> COPY THE CODE BELOW TO HERE
end tell

To access the 'Services' menu could look like this...

Applescript:

tell window 1
       tell splitter group 1
           tell splitter group 1
               tell scroll area 1
                   tell outline 1
                       perform action "AXShowMenu"
                       tell menu 1
                           get the name of every menu item
                           {"Open in New Tab", "Open in New Window", missing value, "Move to Trash", missing value, "Rename", "Compress", "Compress", "Quick Actions", missing value, "Copy “”", "Import from iPhone", missing value, "Use Groups", "Sort By", "Show View Options", missing value, missing value, missing value, "Services"}
                           click menu item "Services"
                       end tell
                   end tell
               end tell
           end tell
       end tell
   end tell

Last edited by Fredrik71 (2020-03-09 06:35:01 am)


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#2 2020-03-20 02:16:34 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: GUI contextual menu

Very useful snippet. Thanks.

Before that, I used the "Follow me..." function (that is, recording user actions) of the Automator application. As I can see, with the help of your simple snippet, everything is simpler and faster.

Only I would have improved it a little. It is necessary to hide the script editor itself in the background. Then your script can be used as is, from script editor. Now I will show how this can be done (assuming that the needed process was frontmost before running the script editor process itself):

Applescript:


set ScriptEditors to {"Script Editor", "Script Debugger", "Smile", "ThisApp"}

tell application "System Events"
   set myFrontMost to name of the first process whose frontmost is true
   if myFrontMost is in ScriptEditors then set visible of process myFrontMost to false
   set theApplication to first application process whose frontmost is true
   set theControl to value of attribute "AXFocusedUIElement" of theApplication
end tell

Now, no need to activate again the application with hardcoded name before running the script (or script application). To experiment, right-click on above snippet here (on web page) to open contextual menu. Then run my script in the script editor. You should get this AXWebArea:

UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "MacScripter / GUI contextual menu" of application process "Safari"

With hardcoded activation makes little sense finding the frontmost process, because you can do this:

Applescript:


set appName to "Safari"

tell application appName to activate
tell application "System Events" to ¬
   set theControl to value of attribute "AXFocusedUIElement" of application process appName

Last edited by KniazidisR (2020-03-20 03:36:06 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-03-20 07:24:03 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 270

Re: GUI contextual menu

There is not so much information here on Attribute AXFocusedUIElement

Also when I did so much GUI Scripting, I run...

get its properties of UI elements
get its properties of attributes

I also did

perform action "AXShowMenu"

and after ask for properties.

Sometimes the information I look for are hidden in menu somewhere and need to be activated
before extractions could happen. Thats why it's so easy to break GUI Scripting if wrong
element are active one.

Recently i discover that if I comment out a pop up button in Numbers the pop up button that
are still in the code will have different index number. It means the index will change
and not be the same.

I got lot of information and I was wonder what AXFocusedUIElement was all about.

===>

But one day I find information on Apple's AppleScript List and from Stackoverflow
https://stackoverflow.com/questions/552 … t-points-t

You did something similar in this post
https://macscripter.net/viewtopic.php?p … 69#p196969

Here is other post
https://macscripter.net/viewtopic.php?p … 34#p148834

KniazidisR wrote:

Very useful snippet. Thanks.


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#4 2020-03-22 07:26:35 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: GUI contextual menu

I thought a little and realized that the code I proposed (to hide used script editor or AppleScript application) can be further improved. It will allow you to analyze other script editors from the script editor or AppleScript application used, unlike my previous script:

Applescript:


set theFrontMost to name of current application

tell application "System Events"
   set visible of process theFrontMost to false
   set theFrontMost to first application process whose frontmost is true
   set theControl to value of attribute "AXFocusedUIElement" of theFrontMost
end tell

Last edited by KniazidisR (2020-03-22 07:32:05 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-03-22 12:41:02 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 270

Re: GUI contextual menu

I have use this to also include the class of UI element

Applescript:

axFocusedUIElement("APPLICATION_NAME")

on axFocusedUIElement(_appName)
   activate application _appName
   tell application "System Events"
       set theApplication to first application process whose frontmost is true
       set theControl to value of attribute "AXFocusedUIElement" of theApplication
       tell theControl to set {theClass} to {its class of UI element}
   end tell
end axFocusedUIElement


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)