Follow these steps to create a simple macOS toolbar app that opens a specific website when clicked.
Target > Your App Name > Info and add a new key to create Info.plistApplication is agent (UIElement): YES
Main.storyboard, delete the **"Window Controller Scene"** and **"View Controller
Scene"** to avoid a
blank window on launch.AssetsAppDelegate.swift
import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {
//the website you want to open
var urlToOpen: String = "https://github.com"
//the website logo(need to add the image into Asset with the same name(iconName)!)
var iconName: String = "github"
/////////////////////////////
var statusItem: NSStatusItem?
var selectedBrowser: String? {
get {
return UserDefaults.standard.string(forKey: "selectedBrowser")
}
set {
UserDefaults.standard.set(newValue, forKey: "selectedBrowser")
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create menu bar icon
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
if let button = statusItem?.button {
if let image = NSImage(named: iconName) {
button.image = image
button.image?.size = NSSize(width: 18, height: 18) // Resize if needed
}
// Set action for both left and right mouse clicks
button.action = #selector(handleClick)
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
}
@objc func handleClick() {
if let event = NSApp.currentEvent {
if event.type == .rightMouseUp {
// Handle right-click: show context menu
showContextMenu()
} else {
// Handle left-click: open ChatGPT in selected browser or default browser
openDiscord()
}
}
}
func showContextMenu() {
let menu = NSMenu()
// Add browser options
let browsers = ["Safari", "Chrome", "Firefox"]
for browser in browsers {
let menuItem = NSMenuItem(title: browser, action: #selector(selectBrowser(_:)), keyEquivalent: "")
menuItem.target = self
menu.addItem(menuItem)
}
// Add a separator
menu.addItem(NSMenuItem.separator())
// Add the default option
let defaultItem = NSMenuItem(title: "Default Browser", action: #selector(selectDefaultBrowser), keyEquivalent: "")
defaultItem.target = self
menu.addItem(defaultItem)
// Add quit option
menu.addItem(NSMenuItem(title: "Quit", action: #selector(quitApp), keyEquivalent: "q"))
// Set the menu
statusItem?.menu = menu
statusItem?.button?.performClick(nil)
statusItem?.menu = nil // Clear the menu to prevent it from staying attached
}
@objc func selectBrowser(_ sender: NSMenuItem) {
// Save the selected browser
selectedBrowser = sender.title
openDiscord()
}
@objc func selectDefaultBrowser() {
// Remove the selected browser setting
selectedBrowser = nil
openDiscord()
}
func openDiscord() {
let url = URL(string: urlToOpen)!
if let browser = selectedBrowser {
openChatGPTInBrowser(named: browser)
} else {
// Fallback to default browser
NSWorkspace.shared.open(url)
}
}
func openChatGPTInBrowser(named browserName: String) {
// Define the URL
let url = URL(string: urlToOpen)!
// Define the paths to the browsers
let browserPaths = [
"Safari": "/Applications/Safari.app",
"Chrome": "/Applications/Google Chrome.app",
"Firefox": "/Applications/Firefox.app"
]
if let browserPath = browserPaths[browserName] {
let config = NSWorkspace.OpenConfiguration()
NSWorkspace.shared.open([url], withApplicationAt: URL(fileURLWithPath: browserPath), configuration: config, completionHandler: nil)
} else {
// Fallback to default browser
NSWorkspace.shared.open(url)
}
}
@objc func quitApp() {
NSApplication.shared.terminate(self)
}
}
Product > ArchiveCopy App to export(Method of Distribution)Your macOS toolbar app is now ready to use. Click the icon to open your selected website in the preferred browser.