Adding a menu item to open a URL

Hello. I'm new to Swift and am attempting to create a menu item on the menu bar that will open a URL in the default browser. When I run this code, I get the error message "NSMenu has no member URL" but I'm not sure what I'm doing wrong.

  func setupMenus() {
       
       let menu = NSMenu()
      _ = URL(string: "https://www.example.com")

       menu.addItem(NSMenuItem.separator())

      if #available(macOS 12.0, *) {
          menu.addItem(NSMenuItem(title: "Example", action: #selector(NSMenu.URL), keyEquivalent: ""))
      } else {
          // Fallback on earlier versions
      }

       statusItem.menu = menu
   }

Thank you TeamPuzel! That last bit of code you included worked perfectly! I'll also take a look at the additional blocks as well. Thanks again!

1 Like

It seems the solution to this is deleted? I'd be interested to see how this works.