From 48d5d6305ee370ee70079e008f34e27331dea1a8 Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Fri, 3 Apr 2020 01:10:29 +0900 Subject: [PATCH] fix: crashed if an invalid login item existed (closes #202) --- src/ui/App.swift | 1 - src/ui/preferences-window/tabs/GeneralTab.swift | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ui/App.swift b/src/ui/App.swift index 7b839a32..3ba069e1 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -10,7 +10,6 @@ class App: NSApplication, NSApplicationDelegate { static let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String static let licence = Bundle.main.object(forInfoDictionaryKey: "NSHumanReadableCopyright") as! String static let repository = "https://github.com/lwouis/alt-tab-macos" - static let url = URL(fileURLWithPath: Bundle.main.bundlePath) as CFURL static var app: App! static let shortcutMonitor = LocalShortcutMonitor() var statusItem: NSStatusItem? diff --git a/src/ui/preferences-window/tabs/GeneralTab.swift b/src/ui/preferences-window/tabs/GeneralTab.swift index a76937cb..292f4eba 100644 --- a/src/ui/preferences-window/tabs/GeneralTab.swift +++ b/src/ui/preferences-window/tabs/GeneralTab.swift @@ -106,13 +106,16 @@ class GeneralTab { @objc static func startAtLoginCallback(_ sender: NSControl) { let loginItems = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() let loginItemsSnapshot = LSSharedFileListCopySnapshot(loginItems, nil).takeRetainedValue() as! [LSSharedFileListItem] + let itemName = Bundle.main.bundleURL.lastPathComponent as CFString + let itemUrl = URL(fileURLWithPath: Bundle.main.bundlePath) as CFURL loginItemsSnapshot.forEach { - if LSSharedFileListItemCopyResolvedURL($0, 0, nil).takeRetainedValue() == App.url { + if (LSSharedFileListItemCopyDisplayName($0)?.takeRetainedValue() == itemName) || + (LSSharedFileListItemCopyResolvedURL($0, 0, nil)?.takeRetainedValue() == itemUrl) { LSSharedFileListItemRemove(loginItems, $0) } } if (sender as! NSButton).state == .on { - LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst.takeRetainedValue(), nil, nil, App.url, nil, nil) + LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst.takeRetainedValue(), nil, nil, itemUrl, nil, nil) } } }