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: app size calculation, file removal via Finder, and error alerts

zeck004a0ab30d44ed
diff --git a/PureMac/Logic/Scanning/AppInfoFetcher.swift b/PureMac/Logic/Scanning/AppInfoFetcher.swift
index b838363..a57b279 100644
--- a/PureMac/Logic/Scanning/AppInfoFetcher.swift
+++ b/PureMac/Logic/Scanning/AppInfoFetcher.swift
@@ -103,16 +103,16 @@ final class AppInfoFetcher {
}
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
+ // Try totalFileAllocatedSize on the bundle URL first (fast, accurate)
+ if let values = try? url.resourceValues(forKeys: [.totalFileAllocatedSizeKey]),
+ let size = values.totalFileAllocatedSize, size > 0 {
+ return Int64(size)
}
- // Fallback: enumerate files
+ // Enumerate files and sum their sizes
guard let enumerator = fileManager.enumerator(
at: url,
- includingPropertiesForKeys: [.fileSizeKey, .isRegularFileKey],
+ includingPropertiesForKeys: [.totalFileAllocatedSizeKey, .isRegularFileKey],
options: [.skipsHiddenFiles]
) else { return 0 }
@@ -120,11 +120,14 @@ final class AppInfoFetcher {
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)
+ if count > 10000 { break }
+ guard let values = try? fileURL.resourceValues(forKeys: [.totalFileAllocatedSizeKey, .fileSizeKey, .isRegularFileKey]),
+ values.isRegularFile == true else { continue }
+ if let allocated = values.totalFileAllocatedSize {
+ total += Int64(allocated)
+ } else if let size = values.fileSize {
+ total += Int64(size)
+ }
}
return total
}
diff --git a/PureMac/ViewModels/AppState.swift b/PureMac/ViewModels/AppState.swift
index 266615a..bead670 100644
--- a/PureMac/ViewModels/AppState.swift
+++ b/PureMac/ViewModels/AppState.swift
@@ -41,6 +41,7 @@ final class AppState: ObservableObject {
@Published var isSearchingOrphans: Bool = false
@Published var isLoadingApps: Bool = false
@Published var isScanningAppFiles: Bool = false
+ @Published var removalError: String?
// MARK: - Services
@@ -114,19 +115,49 @@ final class AppState: ObservableObject {
}
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)
+ let urls = Array(selectedFiles)
+ removalError = nil
+ trashViaFinder(urls: urls) { [weak self] success in
+ DispatchQueue.main.async {
+ guard let self else { return }
+ // Check which files were actually removed from disk
+ let removed = urls.filter { !FileManager.default.fileExists(atPath: $0.path) }
+ if !removed.isEmpty {
+ self.discoveredFiles.removeAll { removed.contains($0) }
+ self.selectedFiles.subtract(removed)
+ Logger.shared.log("Removed \(removed.count) files", level: .info)
+ }
+ let failed = urls.count - removed.count
+ if failed > 0 {
+ self.removalError = "\(failed) file\(failed == 1 ? "" : "s") could not be removed. Grant Full Disk Access in System Settings → Privacy & Security to allow PureMac to manage all files."
+ Logger.shared.log("Failed to remove \(failed) files — likely missing FDA", level: .error)
+ }
}
}
- discoveredFiles.removeAll { selectedFiles.contains($0) }
- selectedFiles.removeAll()
- if errors.isEmpty {
- Logger.shared.log("Successfully removed all selected files", level: .info)
+ }
+
+ /// Uses Finder via AppleScript to move files to Trash.
+ /// This triggers the standard macOS authorization prompt for protected files.
+ private func trashViaFinder(urls: [URL], completion: @escaping (Bool) -> Void) {
+ let posixPaths = urls.map { "\"\($0.path)\"" }.joined(separator: ", ")
+ let script = """
+ tell application "Finder"
+ set theFiles to {}
+ repeat with p in {\(posixPaths)}
+ set end of theFiles to (POSIX file p as alias)
+ end repeat
+ delete theFiles
+ end tell
+ """
+ DispatchQueue.global(qos: .userInitiated).async {
+ let appleScript = NSAppleScript(source: script)
+ var errorInfo: NSDictionary?
+ appleScript?.executeAndReturnError(&errorInfo)
+ let success = errorInfo == nil
+ if let errorInfo {
+ Logger.shared.log("Finder trash error: \(errorInfo)", level: .error)
+ }
+ completion(success)
}
}
diff --git a/PureMac/Views/Apps/AppFilesView.swift b/PureMac/Views/Apps/AppFilesView.swift
index d86cd57..b0baf9f 100644
--- a/PureMac/Views/Apps/AppFilesView.swift
+++ b/PureMac/Views/Apps/AppFilesView.swift
@@ -91,6 +91,20 @@ struct AppFilesView: View {
.padding()
}
}
+ .alert("Removal Failed", isPresented: Binding(
+ get: { appState.removalError != nil },
+ set: { if !$0 { appState.removalError = nil } }
+ )) {
+ Button("Open System Settings") {
+ FullDiskAccessManager.shared.openFullDiskAccessSettings()
+ appState.removalError = nil
+ }
+ Button("OK", role: .cancel) {
+ appState.removalError = nil
+ }
+ } message: {
+ Text(appState.removalError ?? "")
+ }
}
private func fileSelectionBinding(for url: URL) -> Binding<Bool> {
@@ -113,9 +127,8 @@ struct AppFilesView: View {
}
private func removeSingleFile(_ url: URL) {
- try? FileManager.default.removeItem(at: url)
- appState.discoveredFiles.removeAll { $0 == url }
- appState.selectedFiles.remove(url)
+ appState.selectedFiles = [url]
+ appState.removeSelectedFiles()
}
}