kconsiglio / auto-open-screen-mirroring

Open Screen Mirroring menu and select a device using AppleScript
MIT License
10 stars 4 forks source link

How to find out the UI elements for macos automation? #3

Closed duhd1993 closed 1 year ago

duhd1993 commented 1 year ago

I cannot find any tools or documents regarding this. Could you recommend some documents I can look into. Don' t want to wait when macos updates its UI. Thank you!

kconsiglio commented 1 year ago

Something like the apple script debugger might be helpful. They have a useful dictionary that can be browsed to look at properties of various things. Also being able to set breakpoints and look at values of items helps. This may be helpful as well for listing out items, properties, attributes, etc.. A lot of what I did was just in the system apple script editor and doing a lot of logging out to console. An example of outputting the items on a menu bar. You go from there listing properties/attributes of your wanted item.

tell application "System Events"
    tell its application process "ControlCenter"
        set x to its UI elements
        repeat with anitem in x
            log anitem
        end repeat

        set uiElementsMenuBar to UI elements of menu bar 1

        repeat with anElement in uiElementsMenuBar
            log anElement
        end repeat

    end tell
end tell
(*UI element 1 of application process ControlCenter*)
(*menu bar 1 of application process ControlCenter*)
(*menu bar item Clock of menu bar 1 of application process ControlCenter*)
(*menu bar item Clock of menu bar 1 of application process ControlCenter*)
(*menu bar item Control Center of menu bar 1 of application process ControlCenter*)
(*menu bar item Wi‑Fi, connected, 3 bars of menu bar 1 of application process ControlCenter*)
(*menu bar item Battery of menu bar 1 of application process ControlCenter*)
(*menu bar item Screen Mirroring of menu bar 1 of application process ControlCenter*)