Skip to content

v1.40.0

Latest

Choose a tag to compare

@JeanExtreme002 JeanExtreme002 released this 16 Nov 18:24
3eb3a49
pip install --upgrade pymacos

1.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 one macos.windows needs. (#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=False adds text, headings and described images.
    • find(app, name=..., role=...) keeps the elements whose name contains name, 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, and at(x, y) the one at a point.
    • An Element has its role, subrole, name, value, checked, frame, center, enabled, focused, selected and actions, as they were when it was found. Its methods act on it: press(), click(), set_value(), focus(), perform(action), children() and text(). 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 as macos.keyboard.press takes 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.

Docs: https://macos.readthedocs.io