Skip to content

Commit

Permalink
fix: crashed if an invalid login item existed (closes #202)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Apr 2, 2020
1 parent 19b742a commit 48d5d63
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/ui/App.swift
Expand Up @@ -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?
Expand Down
7 changes: 5 additions & 2 deletions src/ui/preferences-window/tabs/GeneralTab.swift
Expand Up @@ -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)
}
}
}

0 comments on commit 48d5d63

Please sign in to comment.