From 482d38ec0b6a8581c11b34589d80df93d9079305 Mon Sep 17 00:00:00 2001 From: Igor Kulman Date: Fri, 30 Sep 2022 17:55:18 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Deleting=20old=20wallpaper=20files?= =?UTF-8?q?=20from=20previous=20runs=20so=20the=20temp=20folder=20does=20n?= =?UTF-8?q?ot=20get=20polluted?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/Abstract/Command.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift b/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift index b66e04b..c801e49 100644 --- a/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift +++ b/Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift @@ -69,7 +69,7 @@ class Command { } do { - let generatedWallpaperFile = workingDirectory.url.appendingPathComponent("/wallpaper-screen-adjusted-\(UUID().uuidString).jpg") + let generatedWallpaperFile = workingDirectory.url.appendingPathComponent("wallpaper-screen-adjusted-\(UUID().uuidString).jpg") try? FileManager.default.removeItem(at: generatedWallpaperFile) try wallpaper.write(to: generatedWallpaperFile) @@ -77,6 +77,16 @@ class Command { try NSWorkspace.shared.setDesktopImageURL(generatedWallpaperFile, for: screen, options: [:]) Log.info("Wallpaper set") + + let oldWallpaperFiles = workingDirectory.files.filter({ $0.url != generatedWallpaperFile }) + guard !oldWallpaperFiles.isEmpty else { + return + } + + Log.info("Deleting old wallpaper files from previous runs") + oldWallpaperFiles.forEach { + try? $0.delete() + } } catch { Log.error("Writing new wallpaper file failed with \(error.localizedDescription) for the main screen") }