Macos – Identify Mac OS X Window, Group, and Text Field names for use in AppleScript

applescriptmacos

I have an Applescript in which I wish to set the value of the text fields Name and Password of a window belonging to the process SecurityAgent.

It is similar to the following:

tell window "Authenticate" of process "SecurityAgent"
    tell group 1
        set value of text field 1 to "king"
        set value of text field 2 to "king1"
    end tell
    click button "OK" of group 2
end tell 

While my script is also referring to the process SecurityAgent, the window in question is the one that pops up when you shut down OS X, and another user is logged in. The SecurityAgent window pops up asking for the administrator username and password in order to complete the shut down process. I do not know how to refer to the text fields in said window.

My question is, how do I find out the name of these text fields, the group they belong to, and the window they belong to? Is there a method of mapping OS X windows so I can refer to them in the AppleScript? I can't seem to find information concerning this anywhere.

Best Answer

I once tried to click a button in the Image Capture application and it took me a while to find this particular button. As explained in my question / answer there, there is a way, using the UIElementInspector. You can download it for free from Apple (download link is at the top in a button, direct link here).

It allows you to peek into the UI elements of each window you're hovering. Pressing Cmd-F7 locks its contents to the element you're hovering.

enter image description here

The "fun" part is actually figuring out how the window is grouped, and which elements belong to which parent. The $55 application UI Browser claims to do a better job at this.

When you're manually debugging AppleScript, and you are in the scope of a window, you can try to guess your way down to individual elements using commands like:

UI elements of window 1
UI elements of splitter group 1 of window 1
UI elements of group 1 of splitter group 1 of window 1

These will give you a list that you can use to narrow down your search for elements and their respective titles.

Related Question