Register VirtualBuddyGuest itself as login item (#733)

This commit is contained in:
Guilherme Rambo
2026-08-04 09:15:09 -07:00
committed by GitHub
parent 57de400576
commit 48f78a92a4
4 changed files with 33 additions and 52 deletions

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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 { }