Question

Is there any way that you can take away the focus ring of the NSAlert button. Here is what I mean:

Focus ring annoyingly appears on NSAlert button

Here is my code:

[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Quit"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Warning!"];
[alert setInformativeText:@"How do you solve this question?"];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:nil modalDelegate:self didEndSelector:@selector(someMethodDidEnd:returnCode:contextInfo:) contextInfo:nil];
Was it helpful?

Solution 2

I would say you should not do this at all, this will go against apple guidelines.

OTHER TIPS

Just been trying to solve this as well and came up with this workaround:

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"123" alternateButton:@"" otherButton:@"" informativeTextWithFormat:@""];
NSButton *button = [[alert buttons] objectAtIndex:0];

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[button window] makeFirstResponder:nil];
});

[alert runModal];

The first two lines are straightforward - create an alert and get a button. Then the issue is to actually set the alert's panel's first responder to nil, however, it may be only after the alert has been run modal, so this is why the dispatch_after is used.

In your case, though, as you are using a sheet and not running it modal can use just this:

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"123" alternateButton:@"" otherButton:@"" informativeTextWithFormat:@""];
NSButton *button = [[alert buttons] objectAtIndex:0];

[alert beginSheetModalForWindow:window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
[[button window] makeFirstResponder:nil];

Old question, but for the record:

To assign the focus ring to a specific button, use setAccessibilityFocused = true:

alert.addButton(withTitle: "Save Changes").setAccessibilityFocused = true

To avoid the focus ring completely:

Beware, setAccessibilityFocused(false) does not work here. Instead set refusesFirstResponder = true for ALL alert buttons:

alert.addButton(withTitle: "Save Changes").refusesFirstResponder = true
alert.addButton(withTitle: "Don't Save").refusesFirstResponder = true

I just did this with the following one-liner. My alert has three buttons and wanted the middle button to have the keyboard focus. The left button had the keyboard focus by default, the right button is the default button.

[alert.buttons[1] setAccessibilityFocused:YES];

I also disabled the focus ring on the other buttons to be sure there wasn't a cosmetic issue.

A quicker way would be:

for (NSButton* button in [alert buttons])
{
    [button setKeyEquivalent:@""];
}
NSInteger answer = [alert runModal];

For those looking to remove the default blue highlighting of the right NSButton from an NSAlert use this:

let alert: NSAlert = NSAlert()
alert.messageText = messageTitle
alert.informativeText = messageBody
alert.alertStyle = NSAlert.Style.informational
alert.addButton(withTitle: "Button 1").keyEquivalent = ""
alert.addButton(withTitle: "Button 2").keyEquivalent = ""

alert.beginSheetModal(for: self.view.window!, completionHandler: { (modalResponse: NSApplication.ModalResponse) -> Void in

    // handle response

})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top