Repository navigation
pip install --upgrade pymacos1.40.0 adds macos.ui, which reads what an app shows (its buttons, text fields, links, checkboxes, menus) and uses it, by name rather than by screen coordinates.
New
-
macos.ui, a module that reads the Accessibility tree VoiceOver reads, so a script needs no screenshot to find a control. It needs the Accessibility permission, the same onemacos.windowsneeds. (#144)for key in ("7", "Multiply", "6", "Equals"): macos.ui.find("Calculator", name=key, role="button")[0].press() macos.ui.find("Safari", role="text field")[0].set_value("example.com") macos.ui.click_menu_item("Format > Font > Bold", app="TextEdit")
elements(app)returns what the app shows on screen, top to bottom: the app in front by default. Only what can be seen is listed: nothing scrolled out of view, behind another of its windows, or in a minimized one. An open menu, a modal dialog or a sheet is listed alone, since nothing else can be used meanwhile.controls_only=Falseadds text, headings and described images.find(app, name=..., role=...)keeps the elements whose name containsname, those named exactly so first. A role is macOS's ("AXButton"), said plainly ("button","text field"), a subrole ("search field"), or in the user's language.wait_for(...)waits until one shows up.focused()returns the element keystrokes go to, andat(x, y)the one at a point.- An
Elementhas itsrole,subrole,name,value,checked,frame,center,enabled,focused,selectedandactions, as they were when it was found. Its methods act on it:press(),click(),set_value(),focus(),perform(action),children()andtext().press()goes through Accessibility, so the pointer doesn't move and the window needn't be in front. menu_items(app)lists every item of an app's menus, with its path, whether it's enabled and checked, and its shortcut asmacos.keyboard.presstakes it ("cmd+shift+s","fn+f").click_menu_item("File > Save As…", app=...)chooses one; the ending…may be left out.- Web pages are read too, in Safari, Chrome and Electron apps. A page of thousands of elements is cut after a few thousand, or 15 seconds.
The UI Elements guide has the details.