hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
| 1 | import AppKit |
| 2 | |
| 3 | /// How well typed text matches a name. Used for apps in the one box: a prefix beats a word start, which beats |
| 4 | /// initials, a plain "contains", one wrong letter, and letters in order with gaps. nil means no match. |
| 5 | /// The rules are not here: they are Omega code (`Omega/hopcore.om`, fn hopfuzz), reached through OmegaCore. |
| 6 | enum Fuzzy { |
| 7 | nonisolated static func score(_ query: String, in name: String) -> Int? { scores(query, in: [name])[0] } |
| 8 | |
| 9 | /// The same for many names in one round trip to the core. |
| 10 | nonisolated static func scores(_ query: String, in names: [String]) -> [Int?] { |
| 11 | OmegaCore.shared.scores(names.map { OmegaCore.encode(query: query, name: $0) }) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | /// The apps on this Mac, by name. A short list kept in memory and read again each time Hop opens, so a newly |
| 16 | /// installed app is there the next time. Separate from the file index: opening an app must never wait for it. |
| 17 | final class AppCatalog: @unchecked Sendable { |
| 18 | struct App: Hashable, Sendable { |
| 19 | let path: String |
| 20 | let name: String // as shown: "Safari" |
| 21 | let alt: String // the file's own name when it differs from the shown one, else "" |
| 22 | } |
| 23 | |
| 24 | static let standardRoots: [String] = ["/Applications", "/System/Applications", NSHomeDirectory() + "/Applications", |
| 25 | "/System/Library/CoreServices/Applications", "/System/Library/CoreServices/Finder.app"] |
| 26 | private let roots: [String] |
| 27 | private let lock = NSLock() |
| 28 | private var list: [App] = [] |
| 29 | private var scanning = false |
| 30 | |
| 31 | init(roots: [String] = AppCatalog.standardRoots) { self.roots = roots; list = Self.scan(roots) } |
| 32 | |
| 33 | var apps: [App] { lock.lock(); defer { lock.unlock() }; return list } |
| 34 | |
| 35 | func refresh(completion: (@Sendable () -> Void)? = nil) { |
| 36 | lock.lock(); if scanning { lock.unlock(); return }; scanning = true; lock.unlock() |
| 37 | let roots = self.roots |
| 38 | DispatchQueue.global(qos: .utility).async { [weak self] in |
| 39 | let found = Self.scan(roots) |
| 40 | guard let self else { return } |
| 41 | self.lock.lock(); let changed = found != self.list; self.list = found; self.scanning = false; self.lock.unlock() |
| 42 | if changed { completion?() } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Every .app in the given folders, three levels deep (Utilities, browser app folders). An app is one item: |
| 47 | /// nothing inside it is listed. Hidden folders (Hop's own backups among them) are skipped. |
| 48 | nonisolated static func scan(_ roots: [String]) -> [App] { |
| 49 | var out: [App] = [], seen = Set<String>() |
| 50 | func add(_ path: String) { |
| 51 | guard seen.insert(path).inserted else { return } |
| 52 | let file = ((path as NSString).lastPathComponent as NSString).deletingPathExtension |
| 53 | var shown = FileManager.default.displayName(atPath: path) |
| 54 | if shown.lowercased().hasSuffix(".app") { shown = (shown as NSString).deletingPathExtension } |
| 55 | if shown.isEmpty { shown = file } |
| 56 | out.append(App(path: path, name: shown, alt: shown.lowercased() == file.lowercased() ? "" : file)) |
| 57 | } |
| 58 | func walk(_ dir: String, depth: Int) { |
| 59 | guard let names = try? FileManager.default.contentsOfDirectory(atPath: dir) else { return } |
| 60 | for name in names.sorted() where !name.hasPrefix(".") { |
| 61 | let path = dir + "/" + name |
| 62 | if name.lowercased().hasSuffix(".app") { add(path); continue } |
| 63 | var isDir: ObjCBool = false |
| 64 | if depth < 3, FileManager.default.fileExists(atPath: path, isDirectory: &isDir), isDir.boolValue { walk(path, depth: depth + 1) } |
| 65 | } |
| 66 | } |
| 67 | for root in roots { |
| 68 | if root.lowercased().hasSuffix(".app") { if FileManager.default.fileExists(atPath: root) { add(root) } } |
| 69 | else { walk(root, depth: 1) } |
| 70 | } |
| 71 | return out |
| 72 | } |
| 73 | |
| 74 | /// Apps matching what was typed, best first. `running` are the paths of apps that are open now; an open app |
| 75 | /// that lives outside the usual folders is found too. |
| 76 | nonisolated static func match(_ query: String, apps: [App], running: [String: String] = [:]) -> [(app: App, score: Int, running: Bool)] { |
| 77 | var all = apps |
| 78 | let known = Set(apps.map(\.path)) |
| 79 | for (path, name) in running where !known.contains(path) { all.append(App(path: path, name: name, alt: "")) } |
| 80 | var out: [(app: App, score: Int, running: Bool)] = [] |
| 81 | let byName = Fuzzy.scores(query, in: all.map(\.name)) |
| 82 | let withAlt = all.indices.filter { !all[$0].alt.isEmpty } |
| 83 | var byAlt = [Int?](repeating: nil, count: all.count) |
| 84 | for (k, s) in zip(withAlt, Fuzzy.scores(query, in: withAlt.map { all[$0].alt })) { byAlt[k] = s } |
| 85 | for (i, a) in all.enumerated() { |
| 86 | let best = max(byName[i] ?? -1, byAlt[i] ?? -1) |
| 87 | guard best >= 0 else { continue } |
| 88 | let isRunning = running[a.path] != nil |
| 89 | out.append((a, best + (isRunning ? 5 : 0), isRunning)) |
| 90 | } |
| 91 | return out.sorted { $0.score != $1.score ? $0.score > $1.score : $0.app.name.count < $1.app.name.count } |
| 92 | } |
| 93 | |
| 94 | /// The apps open now that have a window or a Dock icon: path and name. |
| 95 | @MainActor static func runningNow() -> [String: String] { |
| 96 | var out: [String: String] = [:] |
| 97 | for r in NSWorkspace.shared.runningApplications where r.activationPolicy == .regular { |
| 98 | guard let u = r.bundleURL else { continue } |
| 99 | out[u.path] = r.localizedName ?? u.deletingPathExtension().lastPathComponent |
| 100 | } |
| 101 | return out |
| 102 | } |
| 103 | } |