Dalen Dalen - 9 months ago 43
Python Question

How to find cursor position of a currently focused text editable element on Mac OS X?

I need to perform the following task on Mac OS X:

  1. Find an application window that user is currently working in

  2. Find a currently focused element within it

  3. If the element is text editable, find current cursor position

I'd like to use ATOMac, but I cannot figure out how to do this, i.e. what are correct attributes etc.
Any ideas?

Answer Source
from atomac import getFrontMostApp

def GetInsertionPoint ():
    try: app = getFrontmostApp()
    except: return -1
    for element in app.textFieldsR()+app.textAreasR():
        if element.AXFocused:
            return element.AXSelectedTextRange[0]
    return -1