Front Window Analysis Tool Using AppleScript System Events (List UI Elements)

Note that @CJK's script fails in macOS 10.14.6 Mojave.

Although there is a version which does work on that OS, which I have and use now and then.

Any details on the nature of the failure ?

This line fails to compile:

set the UIElement is missing value then return {}

Presumably should be:

if the UIElement is missing value then return {}
2 Likes

Thanks.

Right you are. I've made the correction and a few other edits, mostly stylistic, with the exception of the class list for the at parameter of infoForUIElement where I've swapped out point for list.

3 Likes

Hello @ccstone I followed your advice. Is there any reference you could suggest to help me convert the output to an AppleScript

For example, simply to press the Notes Menu Item, how would i write the AppleScript based on the following

1- I imagine that you use a standard AppleScript wrapper

2- how do you add the info below into the wrapper ?

thanks very much

application Process "Bear"
	window Bear
		splitter group 1
			scroll area 1
				outline 1
					row 1
						UI element "Notes"
							static text "Notes"
							image 1
							UI element 3

What notes menu item? The one in the menu bar?

1 Like

I will make my question more generic.
You offered a solution for non expert users in order to avoid having to resort to UI browser.
With UI browser, the user records the screen → is given the AppleScript on a silver platter (the report to which one has to add the wrapper).
What I am asking is what the non expert should do with the output of your macro.
Let's say that I am in Bear (could be any app), and I want to write an AppleScript to click on Note in the Menu, how do I go from the output of your macro to an AppleScript ?
thank you Chris

Screenshots?

1 Like

1 Like

At a complete guess: The simplest would be that each indent is a tell bock until the last, which is the action to take. So try this:

tell application "System Events"
    tell application process "Bear"
        tell window Bear
            tell splitter group 1
                tell scroll area 1
                    tell outline 1
                        tell row 1
                            tell UI element "Notes"
                                click UI element 3
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

But I'm no UI-scripting guru -- I'd take the above as the start point and try various tweaks until it works!

1 Like

thanks very much for giving the issue some thought.

This is the error message in KM Engine log

2022-10-17 16:53:44 Action 11497178 failed: Execute an AppleScript failed with script error: text-script:112:117: script error: Expected end of line but found identifier. (-2741)
2022-10-17 16:53:44 Execute an AppleScript failed with script error: text-script:112:117: script error: Expected end of line but found identifier. (-2741). Macro “zz test” cancelled (while executing Execute AppleScript).

Hey @ronald,

The first thing to do is to examine the title of the macro.

It says: "Front Window Analysis Tool..." – it does not say menu analysis tool – or application analysis tool.

After doing that you might come to the (correct) conclusion that my macro will not give you the information needed to select the "Note" menu from the Bear menu bar.

Can it be done? Sure. Can my macro as written do it? Nyet.

You may want to try out @CJK's script which allows one to return a reference to the object under the mouse cursor to the clipboard. (Although wait a bit – there's a glitch in the code that needs fixing first.)

Front Window Analysis Tool Using AppleScript System Events (List UI Elements) - #40 by CJK

Your other alternative is to buy a copy of Script Debugger, which gives you tools to visualize the GUI hierarchy. These work particularly well with @CJK's script.

If I feel like it I might adapt @CJK's script to provide a hierarchical view like my macro does in addition to the objects it returns.

I'll have to think that over and decide it its worth my time.

-Chris

2 Likes

OK. Thank you. I have a better understanding now.

1 Like

So take it from basics and use AppleScript to find out what's available. Not having Bear, let's try in Script Editor itself and look for the "Find..." menu item.

We'll need to start with a menu bar -- how many of those are there?

tell application "System Events"
	tell application process "Script Editor"
		return every menu bar
	end tell
end tell
--> {menu bar 1 of application process "Script Editor" of application "System Events"}

Just the one, which is handy. We could look at everything in menu bar 1, but let's take a guess that the menu is named the same the screen displays it and try and get the contents:

tell application "System Events"
	tell application process "Script Editor"
		return every menu item of menu "Edit" of menu bar 1
	end tell
end tell
--> {menu item "Undo" of menu "Edit" of menu bar item "Edit" of menu bar 1 of application process "Script Editor"...

Looks promising. And if we search the results we find menu item "Find" of menu "Edit"..., which looks even more promising. So:

tell application "System Events"
	tell application process "Script Editor"
		return every menu item of menu item "Find" of menu "Edit" of menu bar 1
	end tell
end tell
--> {}

Whoops! Not so good. Ah, but "Find" has a sub-menu, so we have to get the menu before the menu's items:

tell application "System Events"
	tell application process "Script Editor"
		return every menu of menu item "Find" of menu "Edit" of menu bar 1
	end tell
end tell
--> {menu "Find" of menu item "Find" of menu "Edit"...

And we're back on track. And we know menus have menu items, so:

tell application "System Events"
	tell application process "Script Editor"
		return every menu item of menu "Find" of menu item "Find" of menu "Edit" of menu bar 1
	end tell
end tell
--> {menu item "Find…" of menu "Find"...

...and that first item looks to be the one we want. And remembering that "..." is an ellipsis, not three periods:

tell application "System Events"
	tell application process "Script Editor"
		click menu item "Find…" of menu "Find" of menu item "Find" of menu "Edit" of menu bar item "Edit" of menu bar 1
	end tell
end tell

And it works!

OK, so it's a long-winded way of doing things -- but it is a very cheap way :wink: And doing similar in Bear might get you started on finding what you want.

4 Likes

Hey Guys,

When I'm manually exploring an app, and I don't jump right to an object using @CJK's script I use this method:

tell application "System Events"
   tell application process "Finder" -- the process you're exploring...
      
      set diagnosticsList to {¬
         "----- PROPERTIES -----", ¬
         properties, ¬
         "----- UI ELEMENTS -----", ¬
         UI elements, ¬
         "----- ATTRIBUTES -----", ¬
         attributes, ¬
         "----- ACTIONS -----", ¬
         actions, ¬
         "----- END -----"}
      
   end tell
end tell

This method gets painful in the Script Editor though – which is one reason I created the analysis tool in the first place.

Script Debugger makes this much easier, because it can pretty print lists and also present an object in an object-browser format.

List:

Object-Browser:

Using @CJK's script I also have a method of jumping straight to a specific object in Script Debugger. It's not quite as reliable as UI Browser, but it works 99.x % of the time.

-Chris

2 Likes

I will give it a try with Bear. thank you

Just curious: do you have script debugger ?

Not me. I do try it every now and then but can never get into it and go back to Script Editor -- though I think that would change if I made an effort and/or was more than an occasional AppleScripter.

Before digging deeper into Bear's UI with the above, check out @appleianer's post about UI Browser. And rejoice!

2 Likes

thanks very much