Skip to content

Commit 64093ec

Browse files
committed
improved launch at login method for new macos version
1 parent beb8836 commit 64093ec

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

Launcher/AppDelegateLauncher.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ extension AppDelegateLauncher: NSApplicationDelegate {
4545

4646
let newPath = NSString.path(withComponents: components)
4747

48-
NSWorkspace.shared.launchApplication(newPath)
49-
} else {
48+
let url = URL(fileURLWithPath: newPath)
49+
50+
// Launch the main app
51+
if #available(macOS 10.15, *) {
52+
NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { (_, error) in
53+
if let error = error {
54+
print("Failed to launch app: \(error)")
55+
} else {
56+
print("Successfully launched app")
57+
}
58+
// Terminate the helper app
59+
self.terminate()
60+
}
61+
} else {
62+
NSWorkspace.shared.launchApplication(newPath)
63+
self.terminate()
64+
}
65+
} else {
5066
self.terminate()
5167
}
5268
}

Launcher/Base.lproj/Main.storyboard

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="23094" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
4+
<deployment identifier="macosx"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23094"/>
56
</dependencies>
67
<scenes>
78
<!--Application-->

Menu Bar Dock.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@
779779
CODE_SIGN_IDENTITY = "Mac Developer";
780780
CODE_SIGN_STYLE = Automatic;
781781
COMBINE_HIDPI_IMAGES = YES;
782-
CURRENT_PROJECT_VERSION = 25;
782+
CURRENT_PROJECT_VERSION = 26;
783783
DEVELOPMENT_ASSET_PATHS = "";
784784
DEVELOPMENT_TEAM = T34G959ZG8;
785785
ENABLE_HARDENED_RUNTIME = YES;
@@ -789,7 +789,7 @@
789789
"@executable_path/../Frameworks",
790790
);
791791
MACOSX_DEPLOYMENT_TARGET = 10.15;
792-
MARKETING_VERSION = 4.5;
792+
MARKETING_VERSION = 4.6;
793793
PRODUCT_BUNDLE_IDENTIFIER = com.ethansk.MenuBarDock;
794794
PRODUCT_NAME = "$(TARGET_NAME)";
795795
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -807,7 +807,7 @@
807807
CODE_SIGN_IDENTITY = "Mac Developer";
808808
CODE_SIGN_STYLE = Automatic;
809809
COMBINE_HIDPI_IMAGES = YES;
810-
CURRENT_PROJECT_VERSION = 25;
810+
CURRENT_PROJECT_VERSION = 26;
811811
DEVELOPMENT_ASSET_PATHS = "";
812812
DEVELOPMENT_TEAM = T34G959ZG8;
813813
ENABLE_HARDENED_RUNTIME = YES;
@@ -817,7 +817,7 @@
817817
"@executable_path/../Frameworks",
818818
);
819819
MACOSX_DEPLOYMENT_TARGET = 10.15;
820-
MARKETING_VERSION = 4.5;
820+
MARKETING_VERSION = 4.6;
821821
PRODUCT_BUNDLE_IDENTIFIER = com.ethansk.MenuBarDock;
822822
PRODUCT_NAME = "$(TARGET_NAME)";
823823
PROVISIONING_PROFILE_SPECIFIER = "";

MenuBarDock/AppDelegate.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5757
let launcherAppId = Constants.App.launcherBundleId
5858
let runningApps = NSWorkspace.shared.runningApplications
5959

60-
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
61-
SMLoginItemSetEnabled(launcherAppId as CFString, userPrefs.launchAtLogin)
60+
if #available(macOS 13.0, *) {
61+
do {
62+
let appService = SMAppService.loginItem(identifier: launcherAppId)
63+
if userPrefs.launchAtLogin {
64+
try appService.register()
65+
} else {
66+
try appService.unregister()
67+
}
68+
} catch {
69+
print("Failed to register/unregister login item: \(error)")
70+
}
71+
} else {
72+
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
73+
SMLoginItemSetEnabled(launcherAppId as CFString, userPrefs.launchAtLogin)
74+
}
6275

6376
let isLauncherRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty
6477
if isLauncherRunning {
@@ -157,7 +170,21 @@ extension AppDelegate: PreferencesViewControllerDelegate {
157170
func launchAtLoginDidChange(_ value: Bool) {
158171
userPrefs.launchAtLogin = value
159172
let launcherAppId = Constants.App.launcherBundleId
160-
SMLoginItemSetEnabled(launcherAppId as CFString, value)
173+
if #available(macOS 13.0, *) {
174+
do {
175+
let appService = SMAppService.loginItem(identifier: launcherAppId)
176+
if value {
177+
try appService.register()
178+
} else {
179+
try appService.unregister()
180+
}
181+
} catch {
182+
print("Failed to register/unregister login item: \(error)")
183+
}
184+
} else {
185+
SMLoginItemSetEnabled(launcherAppId as CFString, value)
186+
187+
}
161188
userPrefsWasUpdated()
162189
}
163190

MenuBarDock/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@
880880
</subviews>
881881
</clipView>
882882
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="tLf-Qi-dXJ">
883-
<rect key="frame" x="1" y="246" width="336" height="15"/>
883+
<rect key="frame" x="1" y="221" width="336" height="16"/>
884884
<autoresizingMask key="autoresizingMask"/>
885885
</scroller>
886886
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="LBC-Ii-G6l">

0 commit comments

Comments
 (0)