Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import AppKit
/// One launchable application: an .app bundle, or a mollusc app directory run in dev mode.struct AppEntry { enum Kind { case bundle, mollusc }
let name: String /// The .app bundle, or the directory holding mollusc.json. let url: URL let kind: Kind /// Dimmed hint shown at the right of the row, e.g. "dev · ~/Code/robin.berjon.com/kavu". let detail: String? /// Explicit icon image; nil means the bundle's own icon. let iconFile: String? let lowerName: String /// Lower-cased words of the name, split on non-alphanumerics and camelCase ("QuickTime Player" → quick, time, player). let words: [String] /// First letter of each word ("System Settings" → "ss"). let initials: String
init(name: String, url: URL, kind: Kind = .bundle, detail: String? = nil, iconFile: String? = nil) { self.name = name self.url = url self.kind = kind self.detail = detail self.iconFile = iconFile lowerName = name.lowercased() words = AppEntry.split(name) initials = String(words.compactMap { $0.first }) }
static func split(_ name: String) -> [String] { var words: [String] = [] var current = "" var previousWasLower = false for ch in name { if !ch.isLetter && !ch.isNumber { if !current.isEmpty { words.append(current); current = "" } previousWasLower = false continue } if ch.isUppercase && previousWasLower && !current.isEmpty { words.append(current) current = "" } current.append(contentsOf: ch.lowercased()) previousWasLower = ch.isLowercase } if !current.isEmpty { words.append(current) } return words }}
/// Scans the usual application folders for .app bundles, plus mollusc apps under the dev roots.final class AppIndex { private(set) var entries: [AppEntry] = [] private var lastScan = Date.distantPast private var scanning = false /// Called on the main thread after a background rescan replaces `entries`. var onChange: (() -> Void)?
static let roots = [ "/Applications", "/System/Applications", "/System/Library/CoreServices/Applications", NSHomeDirectory() + "/Applications", ] /// Apps that live outside the roots but are still worth launching. static let extras = [ "/System/Library/CoreServices/Finder.app", ]
func scanNow() { entries = AppIndex.scan() lastScan = Date() }
func rescanIfStale(maxAge: TimeInterval = 60) { guard !scanning, Date().timeIntervalSince(lastScan) > maxAge else { return } scanning = true DispatchQueue.global(qos: .utility).async { let found = AppIndex.scan() DispatchQueue.main.async { self.entries = found self.lastScan = Date() self.scanning = false self.onChange?() } } }
static func scan() -> [AppEntry] { let fm = FileManager.default let selfPath = Bundle.main.bundleURL.standardizedFileURL.path var seen = Set<String>() var found: [AppEntry] = []
func add(_ url: URL) { let resolved = url.resolvingSymlinksInPath().standardizedFileURL guard resolved.path != selfPath, !seen.contains(resolved.path) else { return } seen.insert(resolved.path) var name = fm.displayName(atPath: url.path) if name.hasSuffix(".app") { name.removeLast(4) } found.append(AppEntry(name: name, url: url.standardizedFileURL)) }
// Recurse a couple of levels (Utilities, vendor folders) but never into a bundle. func walk(_ dir: URL, depth: Int) { guard let items = try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: []) else { return } // Not .skipsHiddenFiles: Safari.app in /Applications is a hidden-flagged symlink into the cryptex. for item in items where !item.lastPathComponent.hasPrefix(".") { if item.pathExtension == "app" { add(item); continue } var isDir: ObjCBool = false if depth < 2, fm.fileExists(atPath: item.path, isDirectory: &isDir), isDir.boolValue { walk(item, depth: depth + 1) } } }
for root in roots { walk(URL(fileURLWithPath: root), depth: 0) } for extra in extras where fm.fileExists(atPath: extra) { add(URL(fileURLWithPath: extra)) } found += MolluscApps.scan() return found.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending } }}