Toolbar One-Click App Guide

Follow these steps to create a simple macOS toolbar app that opens a specific website when clicked.

1. Create a New macOS Project in Xcode

2. Set Up Assets

3. Add the Following Code to AppDelegate.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)
    }
}

4. Build and Archive

5. Set the App to Start on Login

  1. Open System Preferences > Users & Groups.
  2. Select your user account.
  3. Go to the Login Items tab.
  4. Click + and add your app.
  5. Check the box to hide it on startup (optional).

That's It!

Your macOS toolbar app is now ready to use. Click the icon to open your selected website in the preferred browser.