Skip to content

Commit

Permalink
improved launch at login method for new macos version
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanSK committed Oct 17, 2024
1 parent beb8836 commit 64093ec
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
20 changes: 18 additions & 2 deletions Launcher/AppDelegateLauncher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ extension AppDelegateLauncher: NSApplicationDelegate {

let newPath = NSString.path(withComponents: components)

NSWorkspace.shared.launchApplication(newPath)
} else {
let url = URL(fileURLWithPath: newPath)

// Launch the main app
if #available(macOS 10.15, *) {
NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { (_, error) in
if let error = error {
print("Failed to launch app: \(error)")
} else {
print("Successfully launched app")
}
// Terminate the helper app
self.terminate()
}
} else {
NSWorkspace.shared.launchApplication(newPath)
self.terminate()
}
} else {
self.terminate()
}
}
Expand Down
5 changes: 3 additions & 2 deletions Launcher/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="23094" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23094"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down
8 changes: 4 additions & 4 deletions Menu Bar Dock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 25;
CURRENT_PROJECT_VERSION = 26;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = T34G959ZG8;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -789,7 +789,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 4.5;
MARKETING_VERSION = 4.6;
PRODUCT_BUNDLE_IDENTIFIER = com.ethansk.MenuBarDock;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -807,7 +807,7 @@
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 25;
CURRENT_PROJECT_VERSION = 26;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = T34G959ZG8;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -817,7 +817,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 4.5;
MARKETING_VERSION = 4.6;
PRODUCT_BUNDLE_IDENTIFIER = com.ethansk.MenuBarDock;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
33 changes: 30 additions & 3 deletions MenuBarDock/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let launcherAppId = Constants.App.launcherBundleId
let runningApps = NSWorkspace.shared.runningApplications

SMLoginItemSetEnabled(launcherAppId as CFString, false) // needs to be set to false to actually create the loginitems.501.plist file, then we can set it to the legit value...weird
SMLoginItemSetEnabled(launcherAppId as CFString, userPrefs.launchAtLogin)
if #available(macOS 13.0, *) {
do {
let appService = SMAppService.loginItem(identifier: launcherAppId)
if userPrefs.launchAtLogin {
try appService.register()
} else {
try appService.unregister()
}
} catch {
print("Failed to register/unregister login item: \(error)")
}
} else {
SMLoginItemSetEnabled(launcherAppId as CFString, false) // needs to be set to false to actually create the loginitems.501.plist file, then we can set it to the legit value...weird
SMLoginItemSetEnabled(launcherAppId as CFString, userPrefs.launchAtLogin)
}

let isLauncherRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty
if isLauncherRunning {
Expand Down Expand Up @@ -157,7 +170,21 @@ extension AppDelegate: PreferencesViewControllerDelegate {
func launchAtLoginDidChange(_ value: Bool) {
userPrefs.launchAtLogin = value
let launcherAppId = Constants.App.launcherBundleId
SMLoginItemSetEnabled(launcherAppId as CFString, value)
if #available(macOS 13.0, *) {
do {
let appService = SMAppService.loginItem(identifier: launcherAppId)
if value {
try appService.register()
} else {
try appService.unregister()
}
} catch {
print("Failed to register/unregister login item: \(error)")
}
} else {
SMLoginItemSetEnabled(launcherAppId as CFString, value)

}
userPrefsWasUpdated()
}

Expand Down
2 changes: 1 addition & 1 deletion MenuBarDock/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="tLf-Qi-dXJ">
<rect key="frame" x="1" y="246" width="336" height="15"/>
<rect key="frame" x="1" y="221" width="336" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="LBC-Ii-G6l">
Expand Down

0 comments on commit 64093ec

Please sign in to comment.