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.

Peel reads when an app was last opened only from Spotlight's index

Țugui Dragoșd78dc40272f5
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index 78349ba6..c32deec9 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -92,7 +92,8 @@ public enum AppCatalog {
let path = PathPattern.comparablePath(of: bundle)
let identity = identity(of: bundle)
if let remembered = known.withLock({ $0[path] }), remembered.identity == identity {
- return remembered.app.withLastUsedDate(AppInspector.lastUsedDate(of: bundle))
+ let use = AppInspector.use(of: bundle)
+ return remembered.app.withUse(lastUsedDate: use.lastUsedDate, isUseRecorded: use.isRecorded)
}
guard let app = AppInspector.inspect(bundle) else { return nil }
known.withLock { $0[path] = (identity, app) }
@@ -142,7 +143,10 @@ public enum AppCatalog {
/// installed. Both are in the order `sorted` keeps.
public static func listsTheSameApps(_ reading: [InstalledApp], as listed: [InstalledApp]) -> Bool {
reading.count == listed.count
- && zip(reading, listed).allSatisfy { $0.withLastUsedDate(nil) == $1.withLastUsedDate(nil) }
+ && zip(reading, listed).allSatisfy { read, known in
+ read.withUse(lastUsedDate: nil, isUseRecorded: true)
+ == known.withUse(lastUsedDate: nil, isUseRecorded: true)
+ }
}
static func appBundles(in directory: URL, maximumDepth: Int = 3) -> [URL] {
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
index 9e315969..2f247936 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppInspector.swift
@@ -70,6 +70,7 @@ public enum AppInspector {
atPath: contents.appending(path: "_MASReceipt/receipt").path(percentEncoded: false)
)
let isSystemProtected = isSystemProtected(url)
+ let use = use(of: url)
return InstalledApp(
url: url,
@@ -86,7 +87,8 @@ public enum AppInspector {
architectures: executable.map(MachOHeader.architectures(ofExecutableAt:)) ?? [],
isFromAppStore: isFromAppStore,
isSystemProtected: isSystemProtected,
- lastUsedDate: lastUsedDate(of: url),
+ lastUsedDate: use.lastUsedDate,
+ isUseRecorded: use.isRecorded,
dateAdded: dateAdded(of: url),
updateFeed: UpdateFeed.detect(info: info, contents: contents, isFromAppStore: isFromAppStore, isSystemProtected: isSystemProtected)
)
@@ -178,9 +180,19 @@ public enum AppInspector {
return info.st_flags & UInt32(SF_RESTRICTED) != 0
}
- static func lastUsedDate(of url: URL) -> Date? {
- guard let item = MDItemCreateWithURL(kCFAllocatorDefault, url as CFURL) else { return nil }
- return MDItemCopyAttribute(item, kMDItemLastUsedDate) as? Date
+ /// When the app was last opened, and whether Spotlight records it at all. An item outside Spotlight's index holds
+ /// only what the file system says (`kMDItemFS…`), yet asked for its last used date it answers with its
+ /// modification date. So the date is read only when the item holds it, and the index holds the item when it holds
+ /// a content type.
+ static func use(of url: URL) -> (lastUsedDate: Date?, isRecorded: Bool) {
+ guard
+ let item = MDItemCreateWithURL(kCFAllocatorDefault, url as CFURL),
+ let names = MDItemCopyAttributeNames(item) as? [String]
+ else { return (nil, false) }
+ let lastUsedDate = names.contains(kMDItemLastUsedDate as String)
+ ? MDItemCopyAttribute(item, kMDItemLastUsedDate) as? Date
+ : nil
+ return (lastUsedDate, names.contains(kMDItemContentType as String))
}
/// When the app appeared in its folder, which is the closest thing to an install date.
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
index cec04f2c..68d60d69 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppOrder.swift
@@ -19,10 +19,14 @@ public enum AppOrder {
}
/// Whether `app` has not been opened in the last `months` months. An app with no record of being opened
- /// counts from the day it was added, since it is exactly the kind of app this filter is meant to find. With
- /// neither date, the app does not count as unused.
+ /// counts from the day it was added, since it is exactly the kind of app this filter is meant to find. An app
+ /// whose openings nothing records, or one with neither date, does not count as unused.
public static func isUnused(_ app: InstalledApp, forMonths months: Int, now: Date = .now, calendar: Calendar = .current) -> Bool {
- guard let last = app.lastUsedDate ?? app.dateAdded, let limit = calendar.date(byAdding: .month, value: -months, to: now) else { return false }
+ guard
+ app.isUseRecorded,
+ let last = app.lastUsedDate ?? app.dateAdded,
+ let limit = calendar.date(byAdding: .month, value: -months, to: now)
+ else { return false }
return last < limit
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
index 843e8961..3f601521 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/InstalledApp.swift
@@ -17,7 +17,11 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
public let architectures: Set<Architecture>
public let isFromAppStore: Bool
public let isSystemProtected: Bool
+ /// When the app was last opened, as Spotlight records it. Nil when it never was, or when nothing records it.
public let lastUsedDate: Date?
+ /// Whether Spotlight records when the app is opened, so that no `lastUsedDate` means it never was. Spotlight
+ /// keeps no record for an app in a folder it does not index.
+ public let isUseRecorded: Bool
public let dateAdded: Date?
public let updateFeed: UpdateFeed?
@@ -36,6 +40,7 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
isFromAppStore: Bool = false,
isSystemProtected: Bool = false,
lastUsedDate: Date? = nil,
+ isUseRecorded: Bool = true,
dateAdded: Date? = nil,
updateFeed: UpdateFeed? = nil
) {
@@ -53,6 +58,7 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
self.isFromAppStore = isFromAppStore
self.isSystemProtected = isSystemProtected
self.lastUsedDate = lastUsedDate
+ self.isUseRecorded = isUseRecorded
self.dateAdded = dateAdded
self.updateFeed = updateFeed
}
@@ -69,9 +75,9 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
|| (folder.lastPathComponent == "Setapp" && folder.deletingLastPathComponent().lastPathComponent == "Applications")
}
- /// Returns a copy with `date` as the last used date, which can change while the bundle stays the same.
- func withLastUsedDate(_ date: Date?) -> InstalledApp {
- guard date != lastUsedDate else { return self }
+ /// Returns a copy with what Spotlight records of the app's use, which can change while the bundle stays the same.
+ func withUse(lastUsedDate date: Date?, isUseRecorded isRecorded: Bool) -> InstalledApp {
+ guard date != lastUsedDate || isRecorded != isUseRecorded else { return self }
return InstalledApp(
url: url,
bundleIdentifier: bundleIdentifier,
@@ -87,6 +93,7 @@ public struct InstalledApp: Sendable, Hashable, Identifiable {
isFromAppStore: isFromAppStore,
isSystemProtected: isSystemProtected,
lastUsedDate: date,
+ isUseRecorded: isRecorded,
dateAdded: dateAdded,
updateFeed: updateFeed
)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
index 5777d53b..d8fd124b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
@@ -6,10 +6,11 @@ struct AppCatalogTests {
private let yesterday = Date(timeIntervalSince1970: 1_800_000_000)
private let today = Date(timeIntervalSince1970: 1_800_086_400)
- private func app(_ name: String, version: String = "1.0", lastUsed: Date? = nil) -> InstalledApp {
+ private func app(_ name: String, version: String = "1.0", lastUsed: Date? = nil, isRecorded: Bool = true) -> InstalledApp {
InstalledApp(
url: URL(filePath: "/Applications/\(name).app", directoryHint: .isDirectory),
- bundleIdentifier: "org.example.\(name)", name: name, version: version, lastUsedDate: lastUsed
+ bundleIdentifier: "org.example.\(name)", name: name, version: version, lastUsedDate: lastUsed,
+ isUseRecorded: isRecorded
)
}
@@ -20,6 +21,7 @@ struct AppCatalogTests {
let reading = [app("Tunewell", lastUsed: today), app("Zed", lastUsed: today)]
#expect(AppCatalog.listsTheSameApps(reading, as: listed))
+ #expect(AppCatalog.listsTheSameApps([app("Tunewell", isRecorded: false), app("Zed")], as: listed))
}
@Test func anotherBuildAnAppAddedOrAnAppGoneIsAnotherList() {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
index 6e02df34..7e13abee 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppInspectorTests.swift
@@ -88,6 +88,21 @@ struct AppInspectorTests {
#expect(!app.isSystemProtected)
}
+ /// Spotlight does not index the temporary folder, and answers there with the bundle's modification date for when
+ /// it was last used.
+ @Test func anAppSpotlightDoesNotIndexHasNoRecordOfBeingOpened() throws {
+ let directory = try TemporaryDirectory()
+ try plist(["CFBundleIdentifier": "org.example.unindexed"], at: "Unindexed.app/Contents/Info.plist", in: directory)
+ let bundle = directory.url.appending(path: "Unindexed.app")
+ let written = Date(timeIntervalSince1970: 1_700_000_000)
+ try FileManager.default.setAttributes([.modificationDate: written], ofItemAtPath: bundle.path(percentEncoded: false))
+
+ let app = try #require(AppInspector.inspect(bundle))
+
+ #expect(app.lastUsedDate == nil)
+ #expect(!app.isUseRecorded)
+ }
+
/// Reading a bundle costs its `Info.plist`, its signature, its Mach-O header, and a Spotlight lookup, and the
/// list of apps is read again often. So a bundle is read again only when it has changed.
@Test func readsABundleAgainOnlyWhenItChanged() async throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
index b4693333..129ec562 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppOrderTests.swift
@@ -5,8 +5,11 @@ import Testing
struct AppOrderTests {
private let now = Date(timeIntervalSince1970: 1_800_000_000)
- private func app(_ name: String, lastUsed: Date? = nil, added: Date? = nil) -> InstalledApp {
- InstalledApp(url: URL(filePath: "/Applications/\(name).app"), bundleIdentifier: "com.example.\(name)", name: name, lastUsedDate: lastUsed, dateAdded: added)
+ private func app(_ name: String, lastUsed: Date? = nil, isRecorded: Bool = true, added: Date? = nil) -> InstalledApp {
+ InstalledApp(
+ url: URL(filePath: "/Applications/\(name).app"), bundleIdentifier: "com.example.\(name)", name: name,
+ lastUsedDate: lastUsed, isUseRecorded: isRecorded, dateAdded: added
+ )
}
@Test func breaksATieTheWayTheListByNameReads() {
@@ -36,4 +39,10 @@ struct AppOrderTests {
#expect(!AppOrder.isUnused(app("New", added: lastWeek), forMonths: 6, now: now))
#expect(!AppOrder.isUnused(app("Unknown"), forMonths: 6, now: now))
}
+
+ @Test func anAppWhoseOpeningsNothingRecordsIsNeverCalledUnused() {
+ let longAgo = now.addingTimeInterval(-400 * 86_400)
+
+ #expect(!AppOrder.isUnused(app("Elsewhere", isRecorded: false, added: longAgo), forMonths: 6, now: now))
+ }
}
diff --git a/Peel/Views/AppList.swift b/Peel/Views/AppList.swift
index 2e19969d..cd9532a4 100644
--- a/Peel/Views/AppList.swift
+++ b/Peel/Views/AppList.swift
@@ -308,7 +308,7 @@ private struct AppRow: View {
} else if let version = app.version {
Text(verbatim: version)
} else {
- Text("Never opened")
+ app.isUseRecorded ? Text("Never opened") : Text(verbatim: "")
}
case .dateAdded:
if let dateAdded = app.dateAdded {