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.

Draw an app macOS does not know from its own icon file, so showing it registers nothing

Țugui Dragoșb5d217c68703
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index a15b1026..c3b95943 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -1,9 +1,10 @@
import CoreServices
import Darwin
-import AppKit
+public import AppKit
public import Foundation
import Security
internal import PeelPrivileged
+import UniformTypeIdentifiers
public enum AppInspector {
/// Where macOS thinks an app is, including folders Peel doesn't scan, such as a build folder.
@@ -174,6 +175,32 @@ public enum AppInspector {
}
}
+ /// The icon of the app at `url`, as macOS draws it when Launch Services knows the app at that place. Asking
+ /// IconServices registers a bundle, as asking for its name does, so any other app's icon comes from its own
+ /// files, or is the generic one.
+ public static func icon(ofAppAt url: URL) -> NSImage {
+ if bundleIdentifier(ofAppAt: url).map({ isKnown(url, as: $0) }) == true {
+ return NSWorkspace.shared.icon(forFile: url.path(percentEncoded: false))
+ }
+ return ownIcon(ofAppAt: url) ?? NSWorkspace.shared.icon(for: .applicationBundle)
+ }
+
+ /// The icon in the app's own files: the `.icns` its Info.plist names, else the image its asset catalog holds.
+ static func ownIcon(ofAppAt url: URL) -> NSImage? {
+ guard let bundle = Bundle(url: AppBundleLayout(of: url).signedBundle), let info = bundle.infoDictionary else {
+ return nil
+ }
+ if let file = info["CFBundleIconFile"] as? String,
+ let icns = bundle.url(forResource: file, withExtension: file.hasSuffix(".icns") ? nil : "icns"),
+ let image = NSImage(contentsOf: icns) {
+ return image
+ }
+ if let name = info["CFBundleIconName"] as? String, let image = bundle.image(forResource: name) {
+ return image
+ }
+ return nil
+ }
+
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 de504660..7dc4933e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -1,7 +1,9 @@
+import AppKit
import Foundation
@testable import PeelCore
import PeelPrivileged
import Testing
+import UniformTypeIdentifiers
struct AppInspectorTests {
private static let thinX86_64: [UInt8] = [0xCF, 0xFA, 0xED, 0xFE, 0x07, 0x00, 0x00, 0x01]
@@ -255,6 +257,28 @@ struct AppInspectorTests {
#expect(app.name == "Quiet")
}
+ @Test func anAppMacOSDoesNotKnowGetsItsIconFromItsOwnFiles() throws {
+ let directory = try TemporaryDirectory()
+ let icns = URL(filePath: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns")
+ try plist(["CFBundleIdentifier": "org.example.drawn", "CFBundleIconFile": "Drawn"], at: "Drawn.app/Contents/Info.plist", in: directory)
+ try directory.directory("Drawn.app/Contents/Resources")
+ try FileManager.default.copyItem(at: icns, to: directory.url.appending(path: "Drawn.app/Contents/Resources/Drawn.icns"))
+ try plist(["CFBundleIdentifier": "org.example.plain"], at: "Plain.app/Contents/Info.plist", in: directory)
+
+ let drawn = AppInspector.icon(ofAppAt: directory.url.appending(path: "Drawn.app", directoryHint: .isDirectory))
+ let plain = AppInspector.icon(ofAppAt: directory.url.appending(path: "Plain.app", directoryHint: .isDirectory))
+
+ #expect(drawn.tiffRepresentation == NSImage(contentsOf: icns)?.tiffRepresentation)
+ #expect(plain.tiffRepresentation == NSWorkspace.shared.icon(for: .applicationBundle).tiffRepresentation)
+ }
+
+ @Test func anAppMacOSKnowsGetsItsIconAsFinderDrawsIt() {
+ let calculator = URL(filePath: "/System/Applications/Calculator.app", directoryHint: .isDirectory)
+ let finder = NSWorkspace.shared.icon(forFile: calculator.path(percentEncoded: false))
+
+ #expect(AppInspector.icon(ofAppAt: calculator).tiffRepresentation == finder.tiffRepresentation)
+ }
+
@Test func anAppMacOSKnowsIsNamedAsFinderNamesIt() {
let calculator = URL(filePath: "/System/Applications/Calculator.app", directoryHint: .isDirectory)
let shown = FileManager.default.displayName(atPath: calculator.path(percentEncoded: false))
diff --git a/Peel/Views/AppIcon.swift b/Peel/Views/AppIcon.swift
index 08c16549..6cb21133 100644
--- a/Peel/Views/AppIcon.swift
+++ b/Peel/Views/AppIcon.swift
@@ -1,4 +1,5 @@
import AppKit
+import PeelCore
import SwiftUI
/// An app's or a file's own icon. Smart Invert leaves it as it is, as it leaves photos, because it shows the
@@ -30,15 +31,24 @@ enum IconCache {
return cache
}
+ nonisolated private static func isApp(_ url: URL) -> Bool {
+ url.pathExtension.lowercased() == "app"
+ }
+
nonisolated private static func images(for url: URL) -> NSCache<NSString, NSImage> {
- url.pathExtension.lowercased() == "app" ? apps : files
+ isApp(url) ? apps : files
+ }
+
+ /// An app's icon is asked of `AppInspector`, which asks IconServices only about an app Launch Services knows.
+ nonisolated private static func image(for url: URL) -> NSImage {
+ isApp(url) ? AppInspector.icon(ofAppAt: url) : NSWorkspace.shared.icon(forFile: url.path(percentEncoded: false))
}
@MainActor
static func icon(for url: URL) -> NSImage {
let path = url.path(percentEncoded: false)
if let image = images(for: url).object(forKey: path as NSString) { return image }
- let image = NSWorkspace.shared.icon(forFile: path)
+ let image = image(for: url)
images(for: url).setObject(image, forKey: path as NSString)
return image
}
@@ -50,7 +60,7 @@ enum IconCache {
for url in urls {
let path = url.path(percentEncoded: false)
guard images(for: url).object(forKey: path as NSString) == nil else { continue }
- images(for: url).setObject(NSWorkspace.shared.icon(forFile: path), forKey: path as NSString)
+ images(for: url).setObject(image(for: url), forKey: path as NSString)
}
}