import Foundation @MainActor func main() async { let timeStr = Date().formatted(date: .omitted, time: .standard) print("[\(timeStr)] 🎨 Finder Transparency Stamper starting...") var targetFolderPath: String? = nil // 1. Check CLI arguments if let arg = CommandLine.arguments.dropFirst().first, !arg.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { targetFolderPath = arg.trimmingCharacters(in: .whitespacesAndNewlines) } else { // 2. Query Finder for the frontmost window target let getFolderScript = """ tell application "Finder" try if (count of windows) > 0 then return POSIX path of (target of window 1 as alias) else return "" end if on error return "" end try end tell """ let process = Process() process.executableURL = URL(fileURLWithPath: "/usr/bin/osascript") process.arguments = ["-e", getFolderScript] let pipe = Pipe() process.standardOutput = pipe try? process.run() process.waitUntilExit() let data = pipe.fileHandleForReading.readDataToEndOfFile() let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" if !output.isEmpty { targetFolderPath = output } } // 3. Fallback to pandas asset vault if no Finder window active if targetFolderPath == nil || targetFolderPath!.isEmpty { let defaultPath = ("~/warezfactory/web/data/thegroovypanda.com/assets/images/pandas" as NSString).expandingTildeInPath if FileManager.default.fileExists(atPath: defaultPath) { targetFolderPath = defaultPath print("â„šī¸ No active Finder window resolved. Defaulting to pandas asset vault.") } else { print("âš ī¸ No folder argument provided and no active Finder window found.") exit(0) } } let folderURL = URL(fileURLWithPath: (targetFolderPath! as NSString).expandingTildeInPath) print("📁 Target Folder: \(folderURL.path)") let fileManager = FileManager.default guard let files = try? fileManager.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]) else { print("❌ Could not read directory at \(folderURL.path)") exit(1) } var stampedCount = 0 for fileURL in files { let ext = fileURL.pathExtension.lowercased() guard ["png", "webp"].contains(ext) else { continue } // Check if image has native alpha let sipsProc = Process() sipsProc.executableURL = URL(fileURLWithPath: "/usr/bin/sips") sipsProc.arguments = ["-g", "hasAlpha", fileURL.path] let sipsPipe = Pipe() sipsProc.standardOutput = sipsPipe try? sipsProc.run() sipsProc.waitUntilExit() let sipsData = sipsPipe.fileHandleForReading.readDataToEndOfFile() let sipsOutput = String(data: sipsData, encoding: .utf8) ?? "" var hasAlpha = sipsOutput.contains("hasAlpha: yes") let name = fileURL.lastPathComponent.lowercased() if name.contains("transparent") || name.contains("_tb.") || name.contains("building_2") { hasAlpha = true } if hasAlpha { // Execute ASOC setIcon via osascript let asocScript = """ use framework "Foundation" use framework "AppKit" set targetPath to "\(fileURL.path)" set targetURL to current application's NSURL's fileURLWithPath:targetPath set img to current application's NSImage's alloc()'s initWithContentsOfURL:targetURL if img is not missing value then set ws to current application's NSWorkspace's sharedWorkspace() ws's setIcon:img forFile:targetPath options:0 end if """ let asocProc = Process() asocProc.executableURL = URL(fileURLWithPath: "/usr/bin/osascript") asocProc.arguments = ["-e", asocScript] try? asocProc.run() asocProc.waitUntilExit() print(" ✨ Stamped borderless icon: \(fileURL.lastPathComponent)") stampedCount += 1 } } // Refresh Finder window let refreshScript = """ tell application "Finder" try if (count of windows) > 0 then update (items of window 1) end if end try end tell """ let refreshProc = Process() refreshProc.executableURL = URL(fileURLWithPath: "/usr/bin/osascript") refreshProc.arguments = ["-e", refreshScript] try? refreshProc.run() refreshProc.waitUntilExit() print("✨ Successfully stamped custom transparent icons for \(stampedCount) cutout(s) in '\(folderURL.lastPathComponent)'!") } await main()