How Do I Use AppleScript UI Scripting to get Name/Title/Text on Window?

By running Applescript I want to grab the name and save it as a KM variable, I know how to save the result as a KM variable but I do not know how to get to the name of the object.

I want to save the static text result as a KM variable ideally the variable will hold 'American Exrpress DSAR'

Can anybody assist?

Judging by your screenshot you are trying to get the name of an UI element.

Often an element doesn’t have a name, unfortunately. In that case try to go for the Description, or Subrole, or whatever seems to be unique in the given context. If all fails go for the element by number, e.g. menu item 3 or whatever.

1 Like

Hey Ali,

What app are you working with?

Set the keyboard shortcuts for UI Browser, so you can place the cursor over what you want to read and hit the appropriate keyboard shortcut.

Open the Script Editor.app and place the cursor in the empty script.

Activate your keyboard shortcut in your app as needed.

When UI Browser comes up hit Cmd-Opt-R.

You should then have a reference to the item in your script.

I never use these one-liner references, because they are terribly hard to read – AND I always find myself needing to discover or do something else in the hierarchy.

tell application "System Events"
   tell application process "Safari"
      tell (first window whose subrole is "AXStandardWindow")
         tell toolbar 1
            tell group 2
               {¬
                  properties, ¬
                  UI elements, ¬
                  attributes, ¬
                  actions, ¬
                  null}
            end tell
         end tell
      end tell
   end tell
end tell

You never want to use a named window, UNLESS there’s a good reason.

This usually works for normal windows:

tell (first window whose subrole is "AXStandardWindow")

I use this construct to discover information at the given level of the hierarchy.

{¬
  properties, ¬
  UI elements, ¬
  attributes, ¬
  actions, ¬
  null}

** The null is just a marker, so I can see where my query ends in the result.

Working with System Events and GUI-Scripting tends to be terribly confusing when you start doing it, but the UI Browser, experience, and some mentoring makes it a lot less painful.

-Chris

1 Like

Thank you for your responses, I just can’t seem to get the result to be American Express DSAR, I am now guessing that this is a bug with Daylite.

Hey Ali,

No bets…

Try running my window analysis tool:

Window Analysis Tool for System Events v1.50

It will bring up everything System Events can see about the front window in TextEdit, or TextWrangler, or BBEdit (whatever is on your system).

Then you can use “find” to search for your desired item.

If you can find it in the text then it’s possible it can be used in a gui script.

-Chris