Shortcut for cursor context menu (spelling suggestions)

An option to map keyboard shortcut to show context menu (spelling suggestions) based on the current cursor position (not a mouse pointer position) would be incredibly useful.
36%20PM

I could've used this a few times just while typing this suggestion itself.

unfortunately macos doesn’t offer any acceptable way to access the position of the text cursor :cry:

CFTypeRef system = nil;
system = AXUIElementCreateSystemWide();
CFTypeRef application = nil;
CFTypeRef focusedElement = nil;
CFRange cfrange;
AXValueRef rangeValue = nil;
// Find the currently focused application
if(AXUIElementCopyAttributeValue(system, kAXFocusedApplicationAttribute, &application) == kAXErrorSuccess)
{
    // Find the currently focused UI Element
    if(AXUIElementCopyAttributeValue(application, kAXFocusedUIElementAttribute, &focusedElement) == kAXErrorSuccess)
    {
        // Get the range attribute of the selected text (i.e. the cursor position)
        if(AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextRangeAttribute, (CFTypeRef *)&rangeValue) == kAXErrorSuccess)
        {
            // Get the actual range from the range attribute
            if(AXValueGetValue(rangeValue, kAXValueCFRangeType, (void *)&cfrange))
            {
                CFTypeRef bounds = nil;
                textRect = NSZeroRect;
                if(AXUIElementCopyParameterizedAttributeValue(focusedElement, kAXBoundsForRangeParameterizedAttribute, rangeValue, (CFTypeRef *)&bounds) == kAXErrorSuccess)
                {
                    CGRect screenRect;
                    AXValueGetValue(bounds, kAXValueCGRectType, &screenRect);
                    if(bounds)
                    {
                        textRect = [DHAbbreviationManager cocoaRectFromCarbonScreenRect:screenRect];
                        CFRelease(bounds);
                    }
                }
            }
            if(rangeValue)
            {
                CFRelease(rangeValue);
            }
        }
    }
    if(focusedElement)
    {
        CFRelease(focusedElement);
    }
}
if(application)
{
    CFRelease(application);
}
if(system)
{
    CFRelease(system);
}
+ (NSPoint)cocoaScreenPointFromCarbonScreenPoint:(NSPoint)carbonPoint
{
   return NSMakePoint(carbonPoint.x, [[[NSScreen screens] objectAtIndex:0] frame].size.height - carbonPoint.y);
}
1 Like

Would this work? Pretty please!

Unfortunately that code works in almost no app (as it gets the selected text, not the text cursor), but something like it is already included in BTT. If you want to try whether it works with your apps you can do it like this:

1.) Assign the "move mouse to position" action. Configure that action to move the mouse to the center of the text cursor.
2.)) Attach an additional right-click

I think on old versions of macOS the text selection did return something even if no next was selected, and for some apps this still works (mostly only Xcode). For most it will fail though and I don't know of any other way to detect the position of the text cursor.

Actually it seems to be a bug in macOS that the code is not working (https://openradar.appspot.com/14285519 ) unfortunately it has been there for at least 10 years, thus very unlikely Apple will ever fix this.

@Andreas_Hegenberg is it still unpossible ?

I don't see the 'move to center of TEXT cursor' option. Has it been removed? Is there a way to move the mouse pointer to the text cursor or a highlighted item?

OMG - first I was put off because this thread indicates that it is not possible. But then I still gave it a try and this totally works!

This is what my shortcut does:

  • Move to (from top left text selection) X: 0 Y: 0
  • Right Click

And the I get the context menu with the suggestion and can use the up and down keys to chose the suggestion

Yay. My life is better now!