objective c - How to get global screen coordinates of currently selected text via Accessibility APIs. -
i need find out, how dictionary app showing following popup dialog selected text on pressing cmd+ctrl+d on application. want implement same kind of functionality cocoa app, app run in background , showing suggestions on hot key press selected text.
i have implemented hot key capturing, need have code rectangle area of selected text on screen, can show dialog dictionary app.
thanks
you can use accessibility apis that. make sure "enable access assistive devices" setting checked (in system preferences / universal access).
the following code snippet determine bounds (in screen coordinates) of selected text in applications. unfortunately, doesn't work in mail , safari, because use private accessibility attributes. it's possible work there well, requires more work , possibly private api calls.
axuielementref systemwideelement = axuielementcreatesystemwide(); axuielementref focussedelement = null; axerror error = axuielementcopyattributevalue(systemwideelement, kaxfocuseduielementattribute, (cftyperef *)&focussedelement); if (error != kaxerrorsuccess) { nslog(@"could not focussed element"); } else { axvalueref selectedrangevalue = null; axerror getselectedrangeerror = axuielementcopyattributevalue(focussedelement, kaxselectedtextrangeattribute, (cftyperef *)&selectedrangevalue); if (getselectedrangeerror == kaxerrorsuccess) { cfrange selectedrange; axvaluegetvalue(selectedrangevalue, kaxvaluecfrangetype, &selectedrange); axvalueref selectionboundsvalue = null; axerror getselectionboundserror = axuielementcopyparameterizedattributevalue(focussedelement, kaxboundsforrangeparameterizedattribute, selectedrangevalue, (cftyperef *)&selectionboundsvalue); cfrelease(selectedrangevalue); if (getselectionboundserror == kaxerrorsuccess) { cgrect selectionbounds; axvaluegetvalue(selectionboundsvalue, kaxvaluecgrecttype, &selectionbounds); nslog(@"selection bounds: %@", nsstringfromrect(nsrectfromcgrect(selectionbounds))); } else { nslog(@"could not bounds selected range"); } if (selectionboundsvalue != null) cfrelease(selectionboundsvalue); } else { nslog(@"could not selected range"); } } if (focussedelement != null) cfrelease(focussedelement); cfrelease(systemwideelement);
Comments
Post a Comment