Cue
Cue

Reputation: 3092

How to place a question mark button in a NSAlert that opens an online web page?

EDIT: My question was not very clear now I edited it to make it clear that I need to open an online web page and not the help book.

I would like to include a question mark button in a NSAlert in a macOS project that points to an online web page with the help resource.

I saw here that there are two possibilities:

var showsHelp: Bool Specifies whether the alert has a help button.

var helpAnchor: String? The alert’s HTML help anchor.

But I can't figure out how to implement it.

I use this code:

@IBAction func buttonPressed(_ sender: Any) {
    let myAlert: NSAlert = NSAlert()

    myAlert.messageText = "Message"
    myAlert.informativeText = "Informative text."

    myAlert.showsSuppressionButton = true

    myAlert.addButton(withTitle: "Later")
    myAlert.addButton(withTitle: "Now")
    myAlert.addButton(withTitle: "OK")

    let choice = myAlert.runModal()

    switch choice {
    case NSAlertFirstButtonReturn:
        print ("OK")
    case NSAlertSecondButtonReturn:
        print ("Now")
    case NSAlertThirdButtonReturn:
        print ("Later")
    default: break

    }
    if myAlert.suppressionButton!.state == 1 {
        print ("Checked")
    } else {
        print ("Not checked")
    }

}

Upvotes: 6

Views: 712

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90671

You should make your controller class conform to NSAlertDelegate.
Then, set myAlert.delegate = self and myAlert.showsHelp = true.
In your controller class, implement func alertShowHelp(_ alert: NSAlert) -> Bool to do whatever you like.

In general, to open URLs in the user's default browser, use NSWorkspace and its open() method.

Upvotes: 4

Oskar
Oskar

Reputation: 3702

Use the helpAnchor property to set the help anchor, which can redirect to a section in the documentation or a website.

var helpAnchor The alert’s HTML help anchor. https://developer.apple.com/reference/appkit/nsalert/1534314-helpanchor

Example:

var alert = NSAlert()
alert.messageText = "Testing"
alert.helpAnchor = "https://google.com"
alert.showsHelp = true
alert.runModal()

That won't work because the URL is not set as a HelpBook, to set that see this:

https://developer.apple.com/library/content/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/using_ah_functions/using_ah_functions.html

For opening external websites:

https://developer.apple.com/library/content/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/authoring_help/authoring_help_book.html#//apple_ref/doc/uid/TP30000903-CH206-CIHEAADH

Please note that the built-in question mark button only supports opening the built in Help window, not internet browsers. If you want to open in a web browser, you have to create a whole custom NSAlert and provide your own accessoryView:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Dialog/Articles/CustomAlertDialogs.html

Upvotes: 0

Related Questions