diff --git a/HiddenTests/AppStoreMigrationTests.swift b/HiddenTests/AppStoreMigrationTests.swift index 7256f87b..987b726f 100644 --- a/HiddenTests/AppStoreMigrationTests.swift +++ b/HiddenTests/AppStoreMigrationTests.swift @@ -184,29 +184,98 @@ final class AppStoreMigrationTests: XCTestCase { // MARK: move into /Applications func testApplicationsFolderDetection() { - let home = URL(fileURLWithPath: "/Users/x") - XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Hidden Bar.app"), home: home)) - XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Utilities/Hidden Bar.app"), home: home)) - XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Applications/Hidden Bar.app"), home: home)) - XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app"), home: home)) - XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/ApplicationsOld/Hidden Bar.app"), home: home)) + XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Hidden Bar.app"))) + XCTAssertTrue(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Applications/Utilities/Hidden Bar.app"))) + XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Applications/Hidden Bar.app")), + "macOS 27 hides the arrow of a copy in ~/Applications") + XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app"))) + XCTAssertFalse(MoveToApplications.isInApplicationsFolder(URL(fileURLWithPath: "/ApplicationsOld/Hidden Bar.app"))) XCTAssertTrue(MoveToApplications.isTranslocated(URL(fileURLWithPath: "/private/var/folders/ab/T/AppTranslocation/1234/d/Hidden Bar.app"))) XCTAssertEqual(MoveToApplications.destination(for: URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app")).path, "/Applications/Hidden Bar.app") } - func testMoveOfferRules() { - let home = URL(fileURLWithPath: "/Users/x") - let downloads = URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app") - func offer(_ url: URL = downloads, answered: Bool = false, masAtDestination: Bool = false, writable: Bool = true) -> Bool { - MoveToApplications.shouldOffer(runningBundle: url, home: home, alreadyAnswered: answered, - destinationIsAppStoreCopy: masAtDestination, applicationsWritable: writable) + // Fake answers: the plan reads no disk, so writability and the App Store copy are parameters. + private func plan(_ running: String = "/Users/x/Downloads/Hidden Bar.app", answered: Bool = false, + writable: Bool = true, masInApplications: Bool = false) -> MoveToApplications.Plan { + MoveToApplications.plan(runningBundle: URL(fileURLWithPath: running), alreadyAnswered: answered, + applicationsWritable: writable, applicationsHoldsAppStoreCopy: masInApplications) + } + + func testMoveGoesToApplicationsWhenItCan() { + XCTAssertEqual(plan(), .moveToApplications(URL(fileURLWithPath: "/Applications/Hidden Bar.app"))) + XCTAssertEqual(plan("/Users/x/Applications/Hidden Bar.app"), .moveToApplications(URL(fileURLWithPath: "/Applications/Hidden Bar.app"))) + XCTAssertEqual(plan("/Applications/Hidden Bar.app"), MoveToApplications.Plan.none) + XCTAssertEqual(plan(answered: true), MoveToApplications.Plan.none, "offered once") + } + + /// Dead end: "Not now" to the Trash left the App Store copy in /Applications + /// and the move offer never showed, nor anything else. + func testKeptAppStoreCopyInApplicationsExplainsWhatToDo() { + XCTAssertEqual(plan(masInApplications: true), .explainAppStoreCopyKept) + XCTAssertEqual(plan(answered: true, masInApplications: true), MoveToApplications.Plan.none) + } + + /// Dead end: a standard account cannot write /Applications. + func testUnwritableApplicationsExplainsWhatToDo() { + XCTAssertEqual(plan(writable: false), .explainNotWritable) + XCTAssertEqual(plan(writable: false, masInApplications: true), .explainNotWritable) + } + + /// The move check reads the real folder: a fake /Applications on disk. + func testMovePlanReadsTheFolderAndTheKeptAppStoreCopy() throws { + let downloads = try makeBundle("Downloads/Hidden Bar.app", bundleID: bundleID, appStore: false) + let system = root.appendingPathComponent("SystemApplications") + try FileManager.default.createDirectory(at: system, withIntermediateDirectories: true) + func migration() -> AppStoreMigration { + AppStoreMigration(environment: .init(home: root, bundleURL: downloads, bundleIdentifier: bundleID, + defaults: defaults, registeredDefaults: registered, + systemApplications: system)) } - XCTAssertTrue(offer()) - XCTAssertFalse(offer(URL(fileURLWithPath: "/Applications/Hidden Bar.app"))) - XCTAssertFalse(offer(answered: true), "offered once") - XCTAssertFalse(offer(masAtDestination: true), "the Trash offer handles the App Store copy first") - XCTAssertFalse(offer(writable: false)) + XCTAssertEqual(migration().movePlan, .moveToApplications(system.appendingPathComponent("Hidden Bar.app"))) + + _ = try makeBundle("SystemApplications/Hidden Bar.app", bundleID: bundleID, appStore: true) + XCTAssertEqual(migration().movePlan, .explainAppStoreCopyKept) + + // The Trash answer does not matter. + MigrationOfferLedger(defaults: defaults).recordAnswer(MigrationOfferLedger.trashKey) + XCTAssertEqual(migration().movePlan, .explainAppStoreCopyKept) + } + + // MARK: another copy already running + + private let downloadsApp = URL(fileURLWithPath: "/Users/x/Downloads/Hidden Bar.app") + + private func instance(_ pid: pid_t, _ path: String, appStore: Bool) -> OtherRunningCopy.Instance { + .init(pid: pid, bundleURL: URL(fileURLWithPath: path), isAppStoreBuild: appStore) + } + + /// Dead end: the App Store copy runs at login and the direct build quit silently. + func testRunningAppStoreCopyIsOfferedAQuit() { + let others = [instance(41, "/Applications/Hidden Bar.app", appStore: true)] + XCTAssertEqual(OtherRunningCopy.decide(others: others, runningBundle: downloadsApp), .offerToReplaceAppStoreCopy(41)) + } + + func testRunningDirectCopyStillWinsSilently() { + XCTAssertEqual(OtherRunningCopy.decide(others: [instance(42, "/Applications/Hidden Bar.app", appStore: false)], + runningBundle: downloadsApp), .quitInFavorOf(42)) + XCTAssertEqual(OtherRunningCopy.decide(others: [instance(41, "/Applications/Hidden Bar.app", appStore: true), + instance(42, "/Users/x/Applications/Hidden Bar.app", appStore: false)], + runningBundle: downloadsApp), .quitInFavorOf(42), + "a direct copy is already running; nothing to switch") + XCTAssertEqual(OtherRunningCopy.decide(others: [], runningBundle: downloadsApp), .launch) + } + + func testDebugRulesOnlyCountTheSamePathUnlessForced() { + let installed = instance(42, "/Applications/Hidden Bar.app", appStore: false) + let appStore = instance(41, "/Applications/Hidden Bar.app", appStore: true) + let relaunch = instance(43, downloadsApp.path, appStore: false) + XCTAssertEqual(OtherRunningCopy.decide(others: [installed, appStore], runningBundle: downloadsApp, + countsOtherPaths: false, countsAppStoreCopies: false), .launch) + XCTAssertEqual(OtherRunningCopy.decide(others: [installed, appStore], runningBundle: downloadsApp, + countsOtherPaths: false, countsAppStoreCopies: true), .offerToReplaceAppStoreCopy(41)) + XCTAssertEqual(OtherRunningCopy.decide(others: [appStore, relaunch], runningBundle: downloadsApp, + countsOtherPaths: false, countsAppStoreCopies: true), .quitInFavorOf(43)) } } diff --git a/hidden/AppDelegate.swift b/hidden/AppDelegate.swift index 295c5c2d..0f82fe5d 100644 --- a/hidden/AppDelegate.swift +++ b/hidden/AppDelegate.swift @@ -63,6 +63,10 @@ class AppDelegate: NSObject, NSApplicationDelegate{ /// If another instance with the same bundle ID is already running, activate it /// and terminate this one instead. private func ensureSingleInstance() -> Bool { + #if HIDDENBAR_NATIVE_VISIBILITY + // The direct build asks before quitting an App Store copy that is running. + return AppStoreMigration.shared.resolveOtherRunningCopy() + #else let current = ProcessInfo.processInfo.processIdentifier let existing = NSWorkspace.shared.runningApplications.first { guard $0.bundleIdentifier == Bundle.main.bundleIdentifier, @@ -79,6 +83,7 @@ class AppDelegate: NSObject, NSApplicationDelegate{ existing.activate(options: [.activateAllWindows]) NSApp.terminate(nil) return false + #endif } func openPreferencesIfNeeded() { diff --git a/hidden/Features/AppStoreMigration/AppStoreMigration.swift b/hidden/Features/AppStoreMigration/AppStoreMigration.swift index 924d082f..f36d6080 100644 --- a/hidden/Features/AppStoreMigration/AppStoreMigration.swift +++ b/hidden/Features/AppStoreMigration/AppStoreMigration.swift @@ -12,7 +12,10 @@ import AppKit // reads ~/Library/Preferences. On launch the direct build offers, once each: // 1. import the App Store build's settings (known keys only), // 2. move a leftover App Store copy to the Trash, -// 3. move itself into /Applications. +// 3. move itself into /Applications, or say what to do when /Applications is +// not writable or holds the App Store copy the user kept. +// Before those, an App Store copy that is already running is offered a quit +// instead of this build quitting silently. // Nothing happens without the user's click. The App Store build compiles none of // this: App Review 2.4.5(iv) forbids it from fetching or installing other apps. #if HIDDENBAR_NATIVE_VISIBILITY @@ -137,14 +140,75 @@ enum AppStoreCopyFinder { && isAppStoreBuild(url, fileManager: fileManager) } } + + /// Signed by the Mac App Store (leaf certificate carries Apple's MAS + /// marker). Catches an App Store copy whose receipt is missing. + static func hasAppStoreSignature(_ bundleURL: URL) -> Bool { + var code: SecStaticCode? + var requirement: SecRequirement? + guard SecStaticCodeCreateWithPath(bundleURL as CFURL, [], &code) == errSecSuccess, let code, + SecRequirementCreateWithString("anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.9] exists" as CFString, + [], &requirement) == errSecSuccess, let requirement else { return false } + return SecStaticCodeCheckValidity(code, SecCSFlags(rawValue: kSecCSBasicValidateOnly), requirement) == errSecSuccess + } } -/// Whether to offer moving the running copy into /Applications. +/// What to do at launch when another copy with this bundle id is running. +/// The App Store copy often starts at login; quitting silently in front of it +/// made the downloaded build look broken. +enum OtherRunningCopy { + struct Instance { + let pid: pid_t + let bundleURL: URL? + let isAppStoreBuild: Bool + } + + enum Decision: Equatable { + case launch + /// Another direct copy: bring it forward and quit, as before. + case quitInFavorOf(pid_t) + /// The App Store copy: ask before quitting it and taking over. + case offerToReplaceAppStoreCopy(pid_t) + } + + /// `others` are running apps with this bundle id, this process excluded. + /// Debug builds pass `countsOtherPaths: false` so a dev build runs next to + /// the installed copy, and `countsAppStoreCopies` to test the offer. + static func decide(others: [Instance], runningBundle: URL, + countsOtherPaths: Bool = true, countsAppStoreCopies: Bool = true) -> Decision { + let running = runningBundle.resolvingSymlinksInPath().standardizedFileURL.path + func isSamePath(_ instance: Instance) -> Bool { + instance.bundleURL?.resolvingSymlinksInPath().standardizedFileURL.path == running + } + let counted = others.filter { instance in + if isSamePath(instance) { return true } + return instance.isAppStoreBuild ? countsAppStoreCopies : countsOtherPaths + } + if let direct = counted.first(where: { !$0.isAppStoreBuild || isSamePath($0) }) { + return .quitInFavorOf(direct.pid) + } + if let appStore = counted.first { + return .offerToReplaceAppStoreCopy(appStore.pid) + } + return .launch + } +} + +/// Where to move the running copy so macOS 27 knows it by bundle id. Only +/// /Applications counts: run from ~/Applications, macOS 27 still hides the +/// arrow on collapse, like any folder outside /Applications. enum MoveToApplications { - static func isInApplicationsFolder(_ bundleURL: URL, home: URL) -> Bool { - let path = bundleURL.resolvingSymlinksInPath().standardizedFileURL.path - let roots = ["/Applications/", home.appendingPathComponent("Applications").standardizedFileURL.path + "/"] - return roots.contains { path.hasPrefix($0) } + enum Plan: Equatable { + case none + case moveToApplications(URL) + /// The App Store copy the user kept holds /Applications/Hidden Bar.app. + case explainAppStoreCopyKept + /// This account cannot write to /Applications. + case explainNotWritable + } + + static func isInApplicationsFolder(_ bundleURL: URL) -> Bool { + bundleURL.resolvingSymlinksInPath().standardizedFileURL.path.hasPrefix("/Applications/") } /// Gatekeeper runs a quarantined app from a read-only random path. @@ -152,16 +216,19 @@ enum MoveToApplications { bundleURL.path.contains("/AppTranslocation/") } - static func destination(for bundleURL: URL) -> URL { - URL(fileURLWithPath: "/Applications").appendingPathComponent(bundleURL.lastPathComponent) + static func destination(for bundleURL: URL, in folder: URL = URL(fileURLWithPath: "/Applications")) -> URL { + folder.appendingPathComponent(bundleURL.lastPathComponent) } - static func shouldOffer(runningBundle: URL, home: URL, alreadyAnswered: Bool, - destinationIsAppStoreCopy: Bool, applicationsWritable: Bool) -> Bool { - guard !alreadyAnswered, applicationsWritable else { return false } - // The App Store copy sits where we would go; the Trash offer comes first. - guard !destinationIsAppStoreCopy else { return false } - return !isInApplicationsFolder(runningBundle, home: home) + /// Independent of the Trash answer: when /Applications cannot take this + /// copy, say what to do instead of skipping the offer in silence. + static func plan(runningBundle: URL, alreadyAnswered: Bool, + applicationsWritable: Bool, applicationsHoldsAppStoreCopy: Bool, + systemApplications: URL = URL(fileURLWithPath: "/Applications")) -> Plan { + guard !alreadyAnswered, !isInApplicationsFolder(runningBundle) else { return .none } + guard applicationsWritable else { return .explainNotWritable } + guard !applicationsHoldsAppStoreCopy else { return .explainAppStoreCopyKept } + return .moveToApplications(destination(for: runningBundle, in: systemApplications)) } } @@ -175,6 +242,7 @@ final class AppStoreMigration { var defaults: UserDefaults var registeredDefaults: [String: Any] var fileManager: FileManager = .default + var systemApplications = URL(fileURLWithPath: "/Applications") } static let shared = AppStoreMigration(environment: Environment( @@ -204,11 +272,91 @@ final class AppStoreMigration { guard Self.isEnabled else { return } DispatchQueue.main.async { [self] in if offerImportIfNeeded() { afterImport() } - offerTrashIfNeeded() - offerMoveIfNeeded() + // The move check waits for the Trash, so a copy just trashed from + // /Applications frees that slot instead of pushing us elsewhere. + offerTrashIfNeeded { [self] in offerMoveIfNeeded() } + } + } + + // MARK: Another copy already running + + /// Called before anything else at launch. Returns false when this process + /// is quitting in favor of the copy already running. + func resolveOtherRunningCopy() -> Bool { + let current = ProcessInfo.processInfo.processIdentifier + let apps = NSWorkspace.shared.runningApplications.filter { + $0.bundleIdentifier == environment.bundleIdentifier && $0.processIdentifier != current + } + let others = apps.map { app in + // Two bundles with one id can confuse Launch Services, so bundleURL + // may name the other copy; the executable path is the process's own. + let bundle = app.executableURL.map { + $0.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() + } ?? app.bundleURL + return OtherRunningCopy.Instance( + pid: app.processIdentifier, bundleURL: bundle, + isAppStoreBuild: bundle.map { + AppStoreCopyFinder.isAppStoreBuild($0, fileManager: environment.fileManager) + || AppStoreCopyFinder.hasAppStoreSignature($0) + } ?? false) + } + #if DEBUG + let decision = OtherRunningCopy.decide(others: others, runningBundle: environment.bundleURL, + countsOtherPaths: false, countsAppStoreCopies: Self.isEnabled) + #else + let decision = OtherRunningCopy.decide(others: others, runningBundle: environment.bundleURL) + #endif + NSLog("AppStoreMigration: other running copy decision \(decision)") + + switch decision { + case .launch: + return true + case .quitInFavorOf(let pid): + quit(inFavorOf: apps.first { $0.processIdentifier == pid }) + return false + case .offerToReplaceAppStoreCopy(let pid): + let appStoreCopy = apps.first { $0.processIdentifier == pid } + let alert = NSAlert() + alert.alertStyle = .informational + alert.messageText = "Hidden Bar from the App Store is running. Quit it and switch to this version?".localized + alert.informativeText = "Only one Hidden Bar can run at a time. The App Store version stays installed until you choose otherwise.".localized + alert.addButton(withTitle: "Quit and Switch".localized) + alert.addButton(withTitle: "Cancel".localized) + guard run(alert, id: "switch") == .alertFirstButtonReturn, let appStoreCopy else { + quit(inFavorOf: appStoreCopy) + return false + } + guard Self.terminate(appStoreCopy) else { + NSLog("AppStoreMigration: the App Store copy did not quit") + quit(inFavorOf: appStoreCopy) + return false + } + return true } } + private func quit(inFavorOf app: NSRunningApplication?) { + app?.activate(options: [.activateAllWindows]) + NSApp.terminate(nil) + } + + /// Asks the app to quit, then forces it; true once it has exited. + private static func terminate(_ app: NSRunningApplication) -> Bool { + func wait(_ seconds: TimeInterval) { + let deadline = Date().addingTimeInterval(seconds) + while !app.isTerminated && Date() < deadline { + RunLoop.current.run(until: Date().addingTimeInterval(0.1)) + } + } + app.terminate() + wait(5) + if !app.isTerminated { + app.forceTerminate() + wait(2) + } + return app.isTerminated + } + // MARK: Import private var containerValues: [String: Any]? { @@ -237,7 +385,7 @@ final class AppStoreMigration { alert.addButton(withTitle: "Import".localized) alert.addButton(withTitle: "Not now".localized) - switch run(alert) { + switch run(alert, id: "import") { case .alertFirstButtonReturn: ledger.recordAnswer(MigrationOfferLedger.importKey) AppStoreSettingsImport.apply(container, to: environment.defaults) @@ -262,10 +410,11 @@ final class AppStoreMigration { fileManager: environment.fileManager) } - func offerTrashIfNeeded() { - guard !ledger.isAnswered(MigrationOfferLedger.trashKey) else { return } + /// `then` runs once the offer is answered and any Trash move has finished. + func offerTrashIfNeeded(then next: @escaping () -> Void = {}) { + guard !ledger.isAnswered(MigrationOfferLedger.trashKey) else { return next() } let copies = appStoreCopies - guard !copies.isEmpty else { return } + guard !copies.isEmpty else { return next() } let alert = NSAlert() alert.alertStyle = .informational @@ -275,45 +424,66 @@ final class AppStoreMigration { alert.addButton(withTitle: "Move to Trash".localized) alert.addButton(withTitle: "Not now".localized) - switch run(alert) { + switch run(alert, id: "trash") { case .alertFirstButtonReturn: ledger.recordAnswer(MigrationOfferLedger.trashKey) NSWorkspace.shared.recycle(copies) { _, error in if let error { NSLog("AppStoreMigration: recycle failed: \(error.localizedDescription)") } + DispatchQueue.main.async(execute: next) } case .alertSecondButtonReturn: ledger.recordAnswer(MigrationOfferLedger.trashKey) + next() default: - break + next() } } // MARK: Move into /Applications - func offerMoveIfNeeded() { - let running = environment.bundleURL - let destination = MoveToApplications.destination(for: running) + var movePlan: MoveToApplications.Plan { let fm = environment.fileManager - guard MoveToApplications.shouldOffer( + let running = environment.bundleURL + return MoveToApplications.plan( runningBundle: running, - home: environment.home, alreadyAnswered: ledger.isAnswered(MigrationOfferLedger.moveKey), - destinationIsAppStoreCopy: AppStoreCopyFinder.isAppStoreBuild(destination, fileManager: fm), - applicationsWritable: fm.isWritableFile(atPath: "/Applications")) else { return } + applicationsWritable: fm.isWritableFile(atPath: environment.systemApplications.path), + applicationsHoldsAppStoreCopy: AppStoreCopyFinder.isAppStoreBuild( + MoveToApplications.destination(for: running, in: environment.systemApplications), fileManager: fm), + systemApplications: environment.systemApplications) + } + + func offerMoveIfNeeded() { + let fm = environment.fileManager + let destination: URL + let info: String + switch movePlan { + case .none: + return + case .explainAppStoreCopyKept: + explainMove("The App Store copy is in /Applications. Quit Hidden Bar, move /Applications/Hidden Bar.app to the Trash, drag this copy into Applications, then open it from there.".localized) + return + case .explainNotWritable: + explainMove("This account can't add apps to /Applications. Ask an administrator to drag Hidden Bar into /Applications, then open it from there.".localized) + return + case .moveToApplications(let url): + destination = url + info = fm.fileExists(atPath: url.path) + ? "Hidden Bar is running from outside Applications. The copy already in Applications goes to the Trash, then Hidden Bar relaunches from there.".localized + : "Hidden Bar is running from outside Applications. Move it there so it keeps working after you empty Downloads, then relaunch.".localized + } let alert = NSAlert() alert.alertStyle = .informational alert.messageText = "Move Hidden Bar to the Applications folder?".localized - alert.informativeText = fm.fileExists(atPath: destination.path) - ? "Hidden Bar is running from outside Applications. The copy already in Applications goes to the Trash, then Hidden Bar relaunches from there.".localized - : "Hidden Bar is running from outside Applications. Move it there so it keeps working after you empty Downloads, then relaunch.".localized + alert.informativeText = info alert.addButton(withTitle: "Move to Applications".localized) alert.addButton(withTitle: "Not now".localized) - switch run(alert) { + switch run(alert, id: "move") { case .alertFirstButtonReturn: ledger.recordAnswer(MigrationOfferLedger.moveKey) - moveAndRelaunch(from: running, to: destination) + moveAndRelaunch(from: environment.bundleURL, to: destination) case .alertSecondButtonReturn: ledger.recordAnswer(MigrationOfferLedger.moveKey) default: @@ -321,6 +491,17 @@ final class AppStoreMigration { } } + /// Shown once: macOS 27 hides nothing for a copy outside /Applications. + private func explainMove(_ steps: String) { + let alert = NSAlert() + alert.alertStyle = .warning + alert.messageText = "Hidden Bar can hide icons only from /Applications".localized + alert.informativeText = steps + alert.addButton(withTitle: "OK".localized) + run(alert, id: "explain") + ledger.recordAnswer(MigrationOfferLedger.moveKey) + } + private func moveAndRelaunch(from source: URL, to destination: URL) { let fm = environment.fileManager do { @@ -330,7 +511,7 @@ final class AppStoreMigration { try fm.copyItem(at: source, to: destination) } catch { let failure = NSAlert(error: error) - run(failure) + run(failure, id: "error") return } // A translocated copy is a read-only view of the original; leave the @@ -357,10 +538,40 @@ final class AppStoreMigration { } @discardableResult - private func run(_ alert: NSAlert) -> NSApplication.ModalResponse { + private func run(_ alert: NSAlert, id: String) -> NSApplication.ModalResponse { NSApp.activate(ignoringOtherApps: true) + #if DEBUG + Self.scheduleDebugAnswer(for: id) + #endif return alert.runModal() } + + #if DEBUG + /// Test hook: launch with `-debugAnswerAlerts "switch=first,move=second"`. + /// Each named alert (switch, import, trash, move, explain, error) gets that + /// answer after 3 seconds, time for a screenshot; any alert not named is + /// aborted, which records nothing and moves nothing. + private static let debugAnswers: [String: String]? = UserDefaults.standard.string(forKey: "debugAnswerAlerts").map { + Dictionary($0.split(separator: ",").compactMap { pair -> (String, String)? in + let parts = pair.split(separator: "=", maxSplits: 1).map(String.init) + return parts.count == 2 ? (parts[0], parts[1]) : nil + }, uniquingKeysWith: { _, last in last }) + } + + private static func scheduleDebugAnswer(for id: String) { + guard let debugAnswers else { return } + let answer = debugAnswers[id] ?? "abort" + let timer = Timer(timeInterval: 3, repeats: false) { _ in + NSLog("AppStoreMigration: debug answer \(id)=\(answer)") + switch answer { + case "first": NSApp.stopModal(withCode: .alertFirstButtonReturn) + case "second": NSApp.stopModal(withCode: .alertSecondButtonReturn) + default: NSApp.abortModal() + } + } + RunLoop.main.add(timer, forMode: .modalPanel) + } + #endif } #endif diff --git a/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift b/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift index 3a721381..92368d33 100644 --- a/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift +++ b/hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift @@ -246,10 +246,11 @@ final class NativeVisibilityEngine: MenuBarEngine { } // Where macOS 27 knows an app by its bundle id: /Applications (and folders - // in it, such as Setapp's) or ~/Applications. Symlinks are resolved first. + // in it, such as Setapp's). Not ~/Applications: run from there, the arrow + // still disappears on collapse. Symlinks are resolved first. static func isInApplicationsFolder(_ bundleURL: URL, applicationFolders: [URL] = FileManager.default.urls(for: .applicationDirectory, - in: [.localDomainMask, .userDomainMask])) -> Bool { + in: .localDomainMask)) -> Bool { let path = bundleURL.resolvingSymlinksInPath().standardizedFileURL.path return applicationFolders.contains { folder in path.hasPrefix(folder.resolvingSymlinksInPath().standardizedFileURL.path + "/") diff --git a/hidden/en.lproj/Localizable.strings b/hidden/en.lproj/Localizable.strings index 1b30e45e..4416dbc2 100644 --- a/hidden/en.lproj/Localizable.strings +++ b/hidden/en.lproj/Localizable.strings @@ -30,6 +30,14 @@ "Hidden Bar is running from outside Applications. The copy already in Applications goes to the Trash, then Hidden Bar relaunches from there." = "Hidden Bar is running from outside Applications. The copy already in Applications goes to the Trash, then Hidden Bar relaunches from there."; "Hidden Bar is running from outside Applications. Move it there so it keeps working after you empty Downloads, then relaunch." = "Hidden Bar is running from outside Applications. Move it there so it keeps working after you empty Downloads, then relaunch."; "Move to Applications" = "Move to Applications"; +"Hidden Bar from the App Store is running. Quit it and switch to this version?" = "Hidden Bar from the App Store is running. Quit it and switch to this version?"; +"Only one Hidden Bar can run at a time. The App Store version stays installed until you choose otherwise." = "Only one Hidden Bar can run at a time. The App Store version stays installed until you choose otherwise."; +"Quit and Switch" = "Quit and Switch"; +"Cancel" = "Cancel"; +"OK" = "OK"; +"Hidden Bar can hide icons only from /Applications" = "Hidden Bar can hide icons only from /Applications"; +"The App Store copy is in /Applications. Quit Hidden Bar, move /Applications/Hidden Bar.app to the Trash, drag this copy into Applications, then open it from there." = "The App Store copy is in /Applications. Quit Hidden Bar, move /Applications/Hidden Bar.app to the Trash, drag this copy into Applications, then open it from there."; +"This account can't add apps to /Applications. Ask an administrator to drag Hidden Bar into /Applications, then open it from there." = "This account can't add apps to /Applications. Ask an administrator to drag Hidden Bar into /Applications, then open it from there."; "Set Shortcut" = "Set Shortcut"; "Show Notch Items" = "Show Notch Items"; "Notch Overflow" = "Notch Overflow";