Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Name an app macOS does not know by its file, so reading it registers nothing

Țugui Dragoș6f9df27761a3
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index 54a6e35f..b840fd6e 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -84,7 +84,7 @@ public enum AppInspector {
return InstalledApp(
url: url,
bundleIdentifier: bundleIdentifier,
- name: displayName(of: url),
+ name: displayName(of: url, identifier: bundleIdentifier),
bundleName: info["CFBundleName"] as? String,
// Falls back to the build number when the short version is empty or is not a string, such as a number.
version: (info["CFBundleShortVersionString"] as? String).flatMap { $0.isEmpty ? nil : $0 } ?? info["CFBundleVersion"] as? String,
@@ -134,17 +134,31 @@ public enum AppInspector {
return !info.isMissing && BoundedRead.data(at: info) == nil
}
- /// The name Finder shows for a bundle, in the user's language when the app translates its name.
/// The name Finder shows for the app macOS knows by `identifier`, or the identifier itself when it knows none.
public static func knownName(forBundleIdentifier identifier: String) -> String {
applicationURL(forBundleIdentifier: identifier).map(displayName(of:)) ?? identifier
}
+ /// The name Finder shows for a bundle, in the user's language when the app translates its name. Asking Launch
+ /// Services for it registers the bundle, so one it does not know yet is named by its file.
public static func displayName(of url: URL) -> String {
- let name = FileManager.default.displayName(atPath: url.path(percentEncoded: false))
+ let identifier = infoDictionary(in: AppBundleLayout(of: url).infoFolder)?["CFBundleIdentifier"] as? String
+ return displayName(of: url, identifier: identifier)
+ }
+
+ private static func displayName(of url: URL, identifier: String?) -> String {
+ let name = identifier.map { isKnown(url, as: $0) } == true
+ ? FileManager.default.displayName(atPath: url.path(percentEncoded: false)) : url.lastPathComponent
return name.hasSuffix(".app") ? String(name.dropLast(4)) : name
}
+ private static func isKnown(_ url: URL, as identifier: String) -> Bool {
+ let path = PathPattern.comparablePath(of: PathPattern.canonical(url))
+ return NSWorkspace.shared.urlsForApplications(withBundleIdentifier: identifier).contains {
+ PathPattern.comparablePath(of: PathPattern.canonical($0)) == path
+ }
+ }
+
private static func signingInformation(
for url: URL
) -> (teamIdentifier: String?, developer: String?, applicationGroups: [String]) {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index d6de493a..745af068 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -181,6 +181,28 @@ struct AppInspectorTests {
#expect(scan.unreadable.contains(list))
}
+ /// Launch Services would answer Loud Name, and asking it registers the bundle.
+ @Test func anAppMacOSDoesNotKnowIsNamedByItsFile() throws {
+ let directory = try TemporaryDirectory()
+ let info = ["CFBundleIdentifier": "org.example.quiet", "CFBundleName": "Quiet", "LSHasLocalizedDisplayName": "1"]
+ try plist(info, at: "Quiet.app/Contents/Info.plist", in: directory)
+ try directory.file(
+ "Quiet.app/Contents/Resources/en.lproj/InfoPlist.strings",
+ contents: Data(#""CFBundleDisplayName" = "Loud Name";"#.utf8)
+ )
+
+ let app = try #require(AppInspector.inspect(directory.url.appending(path: "Quiet.app", directoryHint: .isDirectory)))
+
+ #expect(app.name == "Quiet")
+ }
+
+ @Test func anAppMacOSKnowsIsNamedAsFinderNamesIt() {
+ let calculator = URL(filePath: "/System/Applications/Calculator.app", directoryHint: .isDirectory)
+ let finder = FileManager.default.displayName(atPath: calculator.path(percentEncoded: false))
+
+ #expect(AppInspector.displayName(of: calculator) == finder)
+ }
+
/// A launchd plist too large to be a job is not read, and neither is a link to something that is not a file
/// (reading `/dev/zero` never ends). A link to a real job is followed.
@Test func readsThroughALinkedLaunchdJobAndRefusesWhatIsNoJob() throws {