From 48f78a92a45a2ebec0f7470f794832474de0353a Mon Sep 17 00:00:00 2001 From: Guilherme Rambo Date: Tue, 4 Aug 2026 09:15:09 -0700 Subject: [PATCH] Register VirtualBuddyGuest itself as login item (#733) --- VirtualBuddy/Config/Signing.xcconfig | 6 +-- .../Support/GuestLaunchAtLoginManager.swift | 36 +++++++++++------ .../VirtualBuddyGuest-Bridging-Header.h | 4 +- .../GuestHelperAppDelegate.swift | 39 +------------------ 4 files changed, 33 insertions(+), 52 deletions(-) diff --git a/VirtualBuddy/Config/Signing.xcconfig b/VirtualBuddy/Config/Signing.xcconfig index c5bdbc3..895759c 100644 --- a/VirtualBuddy/Config/Signing.xcconfig +++ b/VirtualBuddy/Config/Signing.xcconfig @@ -2,10 +2,10 @@ CODE_SIGN_IDENTITY = Apple Development VB_BUNDLE_ID_PREFIX = VIRTUALBUDDY_BUNDLE_ID = $(VB_BUNDLE_ID_PREFIX)codes.rambo.VirtualBuddy -GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID = $(VB_BUNDLE_ID_PREFIX)codes.rambo.VirtualBuddyGuestHelper -GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID_STR=@"$(GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID)" +LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID = $(VB_BUNDLE_ID_PREFIX)codes.rambo.VirtualBuddyGuestHelper +LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID_STR=@"$(LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID)" -GCC_PREPROCESSOR_DEFINITIONS=$(inherited) GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID='$(GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID_STR)' +GCC_PREPROCESSOR_DEFINITIONS=$(inherited) LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID='$(LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID_STR)' OTHER_SWIFT_FLAGS[config=Release][sdk=*][arch=*] = -D BUILDING_NON_MANAGED_RELEASE OTHER_SWIFT_FLAGS[config=Dev_Release][sdk=*][arch=*] = -D BUILDING_DEV_RELEASE diff --git a/VirtualBuddyGuest/Dashboard/Support/GuestLaunchAtLoginManager.swift b/VirtualBuddyGuest/Dashboard/Support/GuestLaunchAtLoginManager.swift index 516ce78..6254f3e 100644 --- a/VirtualBuddyGuest/Dashboard/Support/GuestLaunchAtLoginManager.swift +++ b/VirtualBuddyGuest/Dashboard/Support/GuestLaunchAtLoginManager.swift @@ -6,21 +6,21 @@ final class GuestLaunchAtLoginManager: ObservableObject { private lazy var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: String(describing: Self.self)) - var isLaunchAtLoginEnabled: Bool { SMAppService.guestHelper.status == .enabled } + var isLaunchAtLoginEnabled: Bool { SMAppService.mainApp.status == .enabled } func setLaunchAtLoginEnabled(_ enabled: Bool) async throws { logger.debug("Set launch at login enabled: \(enabled, privacy: .public)") if enabled { - try SMAppService.guestHelper.register() + try SMAppService.mainApp.register() } else { - try await SMAppService.guestHelper.unregister() + try await SMAppService.mainApp.unregister() } await MainActor.run { objectWillChange.send() } } - private var hasAutoEnabled: Bool { + private var hasAutoEnabledMainAppService: Bool { get { UserDefaults.standard.bool(forKey: #function) } set { UserDefaults.standard.set(newValue, forKey: #function) @@ -29,16 +29,18 @@ final class GuestLaunchAtLoginManager: ObservableObject { } func autoEnableIfNeeded() { - guard !hasAutoEnabled else { return } - hasAutoEnabled = true + unregisterLegacyLoginItemIfNeeded() - logger.debug("Attempting to auto-enable launch at login") + guard !hasAutoEnabledMainAppService else { return } + hasAutoEnabledMainAppService = true + + logger.notice("Attempting to auto-enable launch at login") Task { do { try await setLaunchAtLoginEnabled(true) - logger.debug("Successfully auto-enabled launch at login") + logger.notice("Successfully auto-enabled launch at login") } catch { logger.error("Failed to auto-enable launch at login: \(error, privacy: .public)") } @@ -47,7 +49,19 @@ final class GuestLaunchAtLoginManager: ObservableObject { } -@available(macOS 13.0, *) -private extension SMAppService { - static let guestHelper = SMAppService.loginItem(identifier: kGuestAppLaunchAtLoginHelperBundleID) +private extension GuestLaunchAtLoginManager { + func unregisterLegacyLoginItemIfNeeded() { + /// Try to unregister regardless of the helper status since it's possible for it to not report enabled but still show up in System Settings. + do { + try SMAppService.legacyGuestHelper.unregister() + } catch { + guard SMAppService.legacyGuestHelper.status == .enabled else { return } + + logger.fault("Error unregistering legacy guest helper login item - \(error, privacy: .public)") + } + } +} + +private extension SMAppService { + static let legacyGuestHelper = SMAppService.loginItem(identifier: kLegacyGuestAppLaunchAtLoginHelperBundleID) } diff --git a/VirtualBuddyGuest/Dashboard/Support/VirtualBuddyGuest-Bridging-Header.h b/VirtualBuddyGuest/Dashboard/Support/VirtualBuddyGuest-Bridging-Header.h index 1c8afa7..d63b8ee 100644 --- a/VirtualBuddyGuest/Dashboard/Support/VirtualBuddyGuest-Bridging-Header.h +++ b/VirtualBuddyGuest/Dashboard/Support/VirtualBuddyGuest-Bridging-Header.h @@ -1 +1,3 @@ -#define kGuestAppLaunchAtLoginHelperBundleID GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID +/// VirtualBuddyGuest previously used a separate launch at login helper, but since VirtualBuddy 2.2 beta 3, it registers itself as a login item. +/// This identifier and the legacy helper target are kept so that VirtualBuddyGuest can unregister it. +#define kLegacyGuestAppLaunchAtLoginHelperBundleID LEGACY_GUEST_LAUNCH_AT_LOGIN_HELPER_BUNDLE_ID diff --git a/VirtualBuddyGuestHelper/GuestHelperAppDelegate.swift b/VirtualBuddyGuestHelper/GuestHelperAppDelegate.swift index 6b94fa1..14f9897 100644 --- a/VirtualBuddyGuestHelper/GuestHelperAppDelegate.swift +++ b/VirtualBuddyGuestHelper/GuestHelperAppDelegate.swift @@ -1,40 +1,5 @@ import Cocoa -import os.log +/// This launch at login helper is no longer used as of VirtualBuddy 2.2 beta 3, but is kept so that VirtualBuddyGuest can unregister it if needed. @main -final class GuestHelperAppDelegate: NSObject, NSApplicationDelegate { - - private let log = OSLog(subsystem: "codes.rambo.VirtualBuddyGuestHelper", category: String(describing: GuestHelperAppDelegate.self)) - - func applicationDidFinishLaunching(_ aNotification: Notification) { - let config = NSWorkspace.OpenConfiguration() - config.activates = false - config.addsToRecentItems = false - config.promptsUserIfNeeded = false - - NSWorkspace.shared.openApplication( - at: Bundle.main.mainAppBundleURL, - configuration: config) { _, error in - if let error = error { - os_log("Failed to launch main app: %{public}@", log: self.log, type: .fault, String(describing: error)) - } else { - os_log("Main app launched successfully", log: self.log, type: .info) - } - - DispatchQueue.main.async { NSApp?.terminate(nil) } - } - } - -} - -extension Bundle { - - var mainAppBundleURL: URL { - bundleURL - .deletingLastPathComponent() // VirtualBuddyGuestHelper.app - .deletingLastPathComponent() // LoginItems - .deletingLastPathComponent() // Library - .deletingLastPathComponent() // Contents - } - -} +final class GuestHelperAppDelegate: NSObject, NSApplicationDelegate { }