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

喜夏-厌秋 提交于 2021-01-29 08:03:48

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/40178040/how-to-find-cursor-position-of-a-currently-focused-text-editable-element-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!