Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

fix: wire up Installed Apps, Orphan Finder, and file removal

moamendf5714e6926e
diff --git a/PureMac.xcodeproj/project.pbxproj b/PureMac.xcodeproj/project.pbxproj
index 9a24225..125f29b 100644
--- a/PureMac.xcodeproj/project.pbxproj
+++ b/PureMac.xcodeproj/project.pbxproj
@@ -31,6 +31,7 @@
DDDA5879006CB97D5D7BDD87 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5785762276FB5E3209C6DE4D /* Logger.swift */; };
E60B0A2C5D0A6CAE35BF4DFB /* Locations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77D3D9A9BC52839E6D0A22BC /* Locations.swift */; };
EDEF28CAD23E936FBED1783B /* PureMacApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63581B70F9B10231964E3602 /* PureMacApp.swift */; };
+ F1F506C146E17D6CEC4A12F3 /* AppInfoFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 23486A54A82EE3865B784D1C /* AppInfoFetcher.swift */; };
FDA30FE6349CA01C8D859F04 /* OrphanListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B71E8F62DB76D85F41F9A2E9 /* OrphanListView.swift */; };
/* End PBXBuildFile section */
@@ -39,6 +40,7 @@
10B0AF194677EAC1D5568785 /* CategoryDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryDetailView.swift; sourceTree = "<group>"; };
11BA091943263913276F3CCF /* SmartScanView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmartScanView.swift; sourceTree = "<group>"; };
1D3AC5F17D7CA94FAB1E8D7A /* StringNormalization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringNormalization.swift; sourceTree = "<group>"; };
+ 23486A54A82EE3865B784D1C /* AppInfoFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppInfoFetcher.swift; sourceTree = "<group>"; };
2641C6376DD6F5889F35510E /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
28181F034530331A550C6A3D /* SchedulerService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchedulerService.swift; sourceTree = "<group>"; };
2A8DE5D45BA19E2670B57DC5 /* CleaningEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CleaningEngine.swift; sourceTree = "<group>"; };
@@ -205,6 +207,7 @@
EC11280F62B0574E3699EFA6 /* Scanning */ = {
isa = PBXGroup;
children = (
+ 23486A54A82EE3865B784D1C /* AppInfoFetcher.swift */,
AC0FEE7141871ED5F9E36121 /* AppPathFinder.swift */,
CB94E06E145558123BB5BFB3 /* Conditions.swift */,
77D3D9A9BC52839E6D0A22BC /* Locations.swift */,
@@ -298,6 +301,7 @@
files = (
4F754D89F4CE5142BE384062 /* AppConstants.swift in Sources */,
1B0D043102FDB245312A5147 /* AppFilesView.swift in Sources */,
+ F1F506C146E17D6CEC4A12F3 /* AppInfoFetcher.swift in Sources */,
75B5F0401D37F2872B1AD85A /* AppListView.swift in Sources */,
9BB5AAA574AFED6C27A3F8E2 /* AppPathFinder.swift in Sources */,
9AA80E035DF7B33F6EE118DF /* AppState.swift in Sources */,
diff --git a/PureMac/Logic/Scanning/AppInfoFetcher.swift b/PureMac/Logic/Scanning/AppInfoFetcher.swift
new file mode 100644
index 0000000..5ba956d
--- /dev/null
+++ b/PureMac/Logic/Scanning/AppInfoFetcher.swift
@@ -0,0 +1,114 @@
+import Foundation
+import AppKit
+
+struct InstalledApp: Identifiable, Hashable {
+ let id: UUID
+ let appName: String
+ let bundleIdentifier: String
+ let path: URL
+ let icon: NSImage
+ let size: Int64
+
+ var formattedSize: String {
+ ByteCountFormatter.string(fromByteCount: size, countStyle: .file)
+ }
+
+ func hash(into hasher: inout Hasher) {
+ hasher.combine(id)
+ }
+
+ static func == (lhs: InstalledApp, rhs: InstalledApp) -> Bool {
+ lhs.id == rhs.id
+ }
+}
+
+final class AppInfoFetcher {
+ static let shared = AppInfoFetcher()
+ private let fileManager = FileManager.default
+
+ private init() {}
+
+ func fetchInstalledApps() -> [InstalledApp] {
+ var apps: [InstalledApp] = []
+ var seenBundleIDs: Set<String> = []
+
+ let searchPaths = [
+ "/Applications",
+ "\(home)/Applications",
+ "/System/Applications",
+ ]
+
+ for searchPath in searchPaths {
+ guard let enumerator = fileManager.enumerator(
+ at: URL(fileURLWithPath: searchPath),
+ includingPropertiesForKeys: [.isDirectoryKey],
+ options: [.skipsHiddenFiles, .skipsPackageDescendants]
+ ) else { continue }
+
+ for case let url as URL in enumerator {
+ guard url.pathExtension == "app" else { continue }
+
+ // Skip subdirectories inside .app bundles
+ enumerator.skipDescendants()
+
+ guard let app = loadAppInfo(from: url),
+ !seenBundleIDs.contains(app.bundleIdentifier) else { continue }
+
+ seenBundleIDs.insert(app.bundleIdentifier)
+ apps.append(app)
+ }
+ }
+
+ return apps.sorted { $0.appName.localizedCaseInsensitiveCompare($1.appName) == .orderedAscending }
+ }
+
+ private func loadAppInfo(from url: URL) -> InstalledApp? {
+ guard let bundle = Bundle(url: url) else { return nil }
+
+ let bundleID = bundle.bundleIdentifier ?? url.deletingPathExtension().lastPathComponent
+ let appName = (bundle.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String)
+ ?? (bundle.object(forInfoDictionaryKey: "CFBundleName") as? String)
+ ?? url.deletingPathExtension().lastPathComponent
+
+ let icon = NSWorkspace.shared.icon(forFile: url.path)
+ icon.size = NSSize(width: 32, height: 32)
+
+ let size = appSize(at: url)
+
+ return InstalledApp(
+ id: UUID(),
+ appName: appName,
+ bundleIdentifier: bundleID,
+ path: url,
+ icon: icon,
+ size: size
+ )
+ }
+
+ private func appSize(at url: URL) -> Int64 {
+ // Use Spotlight metadata for fast size lookup
+ if let item = NSMetadataItem(url: url),
+ let size = item.value(forAttribute: kMDItemFSSize as String) as? Int64 {
+ return size
+ }
+
+ // Fallback: enumerate files
+ guard let enumerator = fileManager.enumerator(
+ at: url,
+ includingPropertiesForKeys: [.fileSizeKey, .isRegularFileKey],
+ options: [.skipsHiddenFiles]
+ ) else { return 0 }
+
+ var total: Int64 = 0
+ var count = 0
+ for case let fileURL as URL in enumerator {
+ count += 1
+ if count > 5000 { break }
+ guard let values = try? fileURL.resourceValues(forKeys: [.fileSizeKey, .isRegularFileKey]),
+ values.isRegularFile == true,
+ let size = values.fileSize else { continue }
+ total += Int64(size)
+ }
+ return total
+ }
+}
diff --git a/PureMac/ViewModels/AppState.swift b/PureMac/ViewModels/AppState.swift
index 85a431f..1a51abf 100644
--- a/PureMac/ViewModels/AppState.swift
+++ b/PureMac/ViewModels/AppState.swift
@@ -3,12 +3,7 @@ import Combine
import UserNotifications
import AppKit
-struct AppInfoPlaceholder: Identifiable, Hashable {
- let id = UUID()
- var appName: String = ""
- var bundleIdentifier: String = ""
- var icon: NSImage = NSImage()
-}
+// InstalledApp is defined in AppInfoFetcher.swift
enum AppSection: Hashable {
case apps
@@ -38,12 +33,13 @@ final class AppState: ObservableObject {
// MARK: - App Uninstaller State
- @Published var installedApps: [AppInfoPlaceholder] = []
- @Published var selectedApp: AppInfoPlaceholder?
+ @Published var installedApps: [InstalledApp] = []
+ @Published var selectedApp: InstalledApp?
@Published var discoveredFiles: [URL] = []
@Published var selectedFiles: Set<URL> = []
@Published var orphanedFiles: [URL] = []
@Published var isSearchingOrphans: Bool = false
+ @Published var isLoadingApps: Bool = false
// MARK: - Services
@@ -74,12 +70,102 @@ final class AppState: ObservableObject {
init() {
loadDiskInfo()
checkFullDiskAccess()
+ loadInstalledApps()
scheduler.setTrigger { [weak self] in
await self?.runScheduledScan()
}
scheduler.start()
}
+ // MARK: - App Loading
+
+ func loadInstalledApps() {
+ isLoadingApps = true
+ Task.detached(priority: .userInitiated) {
+ let apps = AppInfoFetcher.shared.fetchInstalledApps()
+ await MainActor.run { [weak self] in
+ self?.installedApps = apps
+ self?.isLoadingApps = false
+ }
+ }
+ }
+
+ func scanForAppFiles(_ app: InstalledApp) {
+ discoveredFiles = []
+ selectedFiles = []
+ let locations = Locations()
+ let appInfo = AppPathFinder.AppInfo(
+ appName: app.appName,
+ bundleIdentifier: app.bundleIdentifier,
+ path: app.path,
+ entitlements: nil,
+ teamIdentifier: nil
+ )
+ let finder = AppPathFinder(appInfo: appInfo, locations: locations)
+ finder.findPathsAsync { [weak self] urls in
+ guard let self else { return }
+ let sorted = urls.sorted { $0.path < $1.path }
+ self.discoveredFiles = sorted
+ self.selectedFiles = urls
+ }
+ }
+
+ func removeSelectedFiles() {
+ var errors: [String] = []
+ for url in selectedFiles {
+ do {
+ try FileManager.default.removeItem(at: url)
+ } catch {
+ errors.append("\(url.lastPathComponent): \(error.localizedDescription)")
+ Logger.shared.log("Failed to remove \(url.path): \(error.localizedDescription)", level: .error)
+ }
+ }
+ discoveredFiles.removeAll { selectedFiles.contains($0) }
+ selectedFiles.removeAll()
+ if errors.isEmpty {
+ Logger.shared.log("Successfully removed all selected files", level: .info)
+ }
+ }
+
+ func findOrphans() {
+ isSearchingOrphans = true
+ orphanedFiles = []
+ Task.detached(priority: .userInitiated) {
+ let locations = Locations()
+ let knownApps = await MainActor.run { self.installedApps }
+ let knownIDs = Set(knownApps.map { $0.bundleIdentifier.normalizedForMatching() })
+ let knownNames = Set(knownApps.map { $0.appName.normalizedForMatching() })
+
+ var orphans: [URL] = []
+ let fm = FileManager.default
+
+ for path in locations.reverseSearch.paths {
+ guard let contents = try? fm.contentsOfDirectory(atPath: path) else { continue }
+ for item in contents {
+ let normalized = item.normalizedForMatching()
+
+ // Skip known system items
+ if skipReverse.contains(where: { normalized.hasPrefix($0) }) { continue }
+
+ // Check if this item belongs to any known app
+ let belongsToApp = knownIDs.contains(where: { normalized.contains($0) }) ||
+ knownNames.contains(where: { normalized.contains($0) })
+
+ if !belongsToApp {
+ let fullPath = URL(fileURLWithPath: path).appendingPathComponent(item)
+ orphans.append(fullPath)
+ }
+ }
+ }
+
+ let sorted = orphans.sorted { $0.lastPathComponent < $1.lastPathComponent }
+ await MainActor.run { [weak self] in
+ self?.orphanedFiles = sorted
+ self?.isSearchingOrphans = false
+ }
+ }
+ }
+
// MARK: - Selection
func isItemSelected(_ item: CleanableItem) -> Bool {
diff --git a/PureMac/Views/Apps/AppFilesView.swift b/PureMac/Views/Apps/AppFilesView.swift
index 3d1177c..039d6e8 100644
--- a/PureMac/Views/Apps/AppFilesView.swift
+++ b/PureMac/Views/Apps/AppFilesView.swift
@@ -2,7 +2,7 @@ import SwiftUI
struct AppFilesView: View {
@EnvironmentObject var appState: AppState
- let app: AppInfoPlaceholder
+ let app: InstalledApp
var body: some View {
VStack(spacing: 0) {
@@ -85,8 +85,8 @@ struct AppFilesView: View {
}
if !appState.selectedFiles.isEmpty {
- Button("Remove Selected", role: .destructive) {
- // Will be connected to cleaning engine
+ Button("Remove Selected (\(appState.selectedFiles.count))", role: .destructive) {
+ appState.removeSelectedFiles()
}
.buttonStyle(.borderedProminent)
.tint(.red)
diff --git a/PureMac/Views/Apps/AppListView.swift b/PureMac/Views/Apps/AppListView.swift
index 1263c1b..b7fd204 100644
--- a/PureMac/Views/Apps/AppListView.swift
+++ b/PureMac/Views/Apps/AppListView.swift
@@ -3,9 +3,9 @@ import SwiftUI
struct AppListView: View {
@EnvironmentObject var appState: AppState
@State private var searchText = ""
- @State private var selection: AppInfoPlaceholder.ID?
+ @State private var selection: InstalledApp.ID?
- private var filteredApps: [AppInfoPlaceholder] {
+ private var filteredApps: [InstalledApp] {
if searchText.isEmpty {
return appState.installedApps
}
@@ -18,8 +18,19 @@ struct AppListView: View {
var body: some View {
Group {
- if appState.installedApps.isEmpty {
- EmptyStateView("No Apps Loaded", systemImage: "square.grid.2x2", description: "App scanning is being set up. This feature will discover all installed applications and their related files.")
+ if appState.isLoadingApps {
+ VStack(spacing: 12) {
+ ProgressView("Loading installed apps...")
+ }
+ .frame(maxWidth: .infinity, maxHeight: .infinity)
+ } else if appState.installedApps.isEmpty {
+ EmptyStateView(
+ "No Apps Found",
+ systemImage: "square.grid.2x2",
+ description: "Could not find any installed applications.",
+ action: { appState.loadInstalledApps() },
+ actionLabel: "Retry"
+ )
} else {
Table(filteredApps, selection: $selection) {
TableColumn("Application") { app in
@@ -32,9 +43,16 @@ struct AppListView: View {
}
.width(min: 200)
+ TableColumn("Size") { app in
+ Text(app.formattedSize)
+ .monospacedDigit()
+ .foregroundStyle(.secondary)
+ }
+ .width(ideal: 80)
+
TableColumn("Bundle Identifier") { app in
Text(app.bundleIdentifier)
- .foregroundStyle(.secondary)
+ .foregroundStyle(.tertiary)
.font(.caption)
}
.width(min: 180)
@@ -43,11 +61,21 @@ struct AppListView: View {
if let id = newValue,
let app = appState.installedApps.first(where: { $0.id == id }) {
appState.selectedApp = app
+ appState.scanForAppFiles(app)
}
}
}
}
.searchable(text: $searchText, prompt: "Search apps")
.navigationTitle("Installed Apps (\(appState.installedApps.count))")
+ .toolbar {
+ ToolbarItem {
+ Button {
+ appState.loadInstalledApps()
+ } label: {
+ Label("Refresh", systemImage: "arrow.clockwise")
+ }
+ }
+ }
}
}
diff --git a/PureMac/Views/MainWindow.swift b/PureMac/Views/MainWindow.swift
index 21e86ad..fd93672 100644
--- a/PureMac/Views/MainWindow.swift
+++ b/PureMac/Views/MainWindow.swift
@@ -11,7 +11,8 @@ struct MainWindow: View {
} detail: {
detailView
}
- .frame(minWidth: 800, minHeight: 500)
+ .navigationSplitViewColumnWidth(min: 200, ideal: 220, max: 280)
+ .frame(minWidth: 860, minHeight: 520)
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
appState.checkFullDiskAccess()
}
@@ -22,8 +23,10 @@ struct MainWindow: View {
Section("Applications") {
Label("Installed Apps", systemImage: "square.grid.2x2")
.tag(AppSection.apps)
+ .badge(appState.installedApps.count)
Label("Orphaned Files", systemImage: "doc.questionmark")
.tag(AppSection.orphans)
+ .badge(appState.orphanedFiles.count)
}
Section("Cleaning") {
@@ -49,15 +52,9 @@ struct MainWindow: View {
private var detailView: some View {
switch selectedSection {
case .apps:
- Text("Installed Apps")
- .font(.title2)
- .foregroundStyle(.secondary)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
+ AppListView()
case .orphans:
- Text("Orphaned Files")
- .font(.title2)
- .foregroundStyle(.secondary)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
+ OrphanListView()
case .cleaning(let category):
if category == .smartScan {
SmartScanView()
@@ -65,9 +62,7 @@ struct MainWindow: View {
CategoryDetailView(category: category)
}
case nil:
- Text("Select a category")
- .foregroundStyle(.secondary)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
+ EmptyStateView("PureMac", systemImage: "sparkles", description: "Select a category from the sidebar to get started.")
}
}
}
diff --git a/PureMac/Views/Orphans/OrphanListView.swift b/PureMac/Views/Orphans/OrphanListView.swift
index b0a6040..0c50f84 100644
--- a/PureMac/Views/Orphans/OrphanListView.swift
+++ b/PureMac/Views/Orphans/OrphanListView.swift
@@ -14,7 +14,7 @@ struct OrphanListView: View {
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if appState.orphanedFiles.isEmpty {
- EmptyStateView("No Orphaned Files", systemImage: "checkmark.circle", description: "No leftover files from uninstalled apps were found.", action: { /* Will be connected to OrphanFinder */ }, actionLabel: "Scan for Orphans")
+ EmptyStateView("No Orphaned Files", systemImage: "checkmark.circle", description: "No leftover files from uninstalled apps were found.", action: { appState.findOrphans() }, actionLabel: "Scan for Orphans")
} else {
List(appState.orphanedFiles, id: \.self) { fileURL in
Toggle(isOn: orphanBinding(for: fileURL)) {
@@ -55,12 +55,16 @@ struct OrphanListView: View {
.toolbar {
ToolbarItemGroup {
Button("Scan for Orphans") {
- // Will be connected to OrphanFinder
+ appState.findOrphans()
}
if !selectedOrphans.isEmpty {
- Button("Remove Selected", role: .destructive) {
- // Will be connected to cleaning engine
+ Button("Remove Selected (\(selectedOrphans.count))", role: .destructive) {
+ for url in selectedOrphans {
+ try? FileManager.default.removeItem(at: url)
+ }
+ appState.orphanedFiles.removeAll { selectedOrphans.contains($0) }
+ selectedOrphans.removeAll()
}
.buttonStyle(.borderedProminent)
.tint(.red)