Also launching an app on system startup

import SwiftUI
import ServiceManagement
import Combine

@main
struct WatchControl_MacApp: App {
    @ObservedObject var menuState = LaunchOnStartupState.shared

    var body: some Scene {
        MenuBarExtra("WatchControl", image: "Image") {
            Toggle(isOn: $menuState.launchOnLogin) {
                Text("Launch on startup")
            }
            .toggleStyle(.checkbox)
            Divider()
            Button("Quit") {
                NSApplication.shared.terminate(nil)
            }.keyboardShortcut("q")
        }
    }
}


class LaunchOnStartupState : ObservableObject {
    static var shared = LaunchOnStartupState()
    @Published var launchOnLogin = SMAppService.mainApp.status == .enabled
    var cancelable: AnyCancellable?
    
    private init() {
        cancelable = $launchOnLogin.sink { newValue in
            do {
                if newValue {
                    print(Calling SMAppService.mainApp.register()")
                    try SMAppService.mainApp.register()
                } else {
                    print(“Calling SMAppService.mainApp.unregister()")
                    try SMAppService.mainApp.unregister()
                }
            } catch {
                print(Error \(newValue ? "" : "un")registering for startup: \(error)")
            }
        }
    }
}