Building Local Calendar Sync Day 02: Add Status Bar Icon and remove Main Window

Day two of building a small software tool called Local Calendar Sync, which runs locally on my computer and will hopefully solve the Calendar Problem in my life. Today, we added a Status Bar icon and removed the default window.

Building Local Calendar Sync Day 02: Add Status Bar Icon and remove Main Window

Day two of building a small software tool called Local Calendar Sync, which runs locally on my computer and will hopefully solve the Calendar Problem in my life. Today, we added a Status Bar icon and removed the default window.

Add Menu Bar Icon

As the Local Calendar Sync Tool should run in the background and should be accessible from the Menu bar, the first task was to add a Menu Bar Icon. I was able to adapt a StackOverflow answer , to the Universal App Model, that I chose and created a simple Menu Bar Icon with Context Menu for the App.

This has been achieved by adding the following code to the CalSyncApp.swift file.

import SwiftUI
import EventKit

@main
struct CalSyncApp: App {
    @State private var command: String = "a"
    
    var body: some Scene {
        MenuBarExtra(command, systemImage: "\\(command).circle") {
            Button("Uno") { command = "a" }
                .keyboardShortcut("U")
                   
            Button("Dos") { command = "b" }
                .keyboardShortcut("D")
                   
            Divider()

            Button("Quit") { NSApplication.shared.terminate(nil) }
                .keyboardShortcut("Q")
        }
    }
}

Remove Main Window

When creating a new XCode project, a default window will be created, which shows up at launch. As the ClendarSync app should run in the Background, I wanted to avoid that. For this, I removed the following code from the CalSyncApp.swift file.

//  WindowGroup {
//   ContentView()
//  }

This does not remove the ContentView class, which is still present in the file explorer but doesn’t launch it by default. I might re-use it later when crafting the settings view.

Hide Dock Icon

As the Local CalendarS ync App should run in the background, I don’t need a Dock Icon to be present. I can think about bringing it back, when the Settings window is open, but for now, we can remove it. The same StackOverflow answer as before also included a hint to add Application is agent (UIElement) to the Info tab of Xcode.

As I had no such option for my Universal project, I added it to the Info.plist file, which also did the job.


☝️ Advertisement Block: I will buy myself a pizza every time I make enough money with these ads to do so. So please feed a hungry developer and consider disabling your Ad Blocker.