Skip to content

Commit

Permalink
✨ Deleting old wallpaper files from previous runs so the temp folder …
Browse files Browse the repository at this point in the history
…does not get polluted
  • Loading branch information
igorkulman committed Sep 30, 2022
1 parent 0a78695 commit 482d38e
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/ChangeMenuBarColor/Commands/Abstract/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@ 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)
Log.debug("Created new wallpaper for the main screen in \(generatedWallpaperFile.absoluteString)")

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")
}
Expand Down

3 comments on commit 482d38e

@Waitsnake
Copy link

@Waitsnake Waitsnake commented on 482d38e Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a script that uses ChangeMenuBarColor to switch between DarkMode/NormalMode for multiple desktops.
The switch between the desktop is done by a little AppleScript(and setting some Shortcuts to switch the desktops).
When I run ChangeMenuBarColor with this change 2 times in a row for two desktop (4 times in sum) I end up with all files deleted from folder:
~/Library/Application Support/ChangeMenuBarColor

  1. 1st run for 1st Desktop is fine.
  2. But already the 2nd run 2nd Desktop will delete the file that is used for the 1st desktop file as "old file".
  3. 3rd run for 1st Desktop can not any longer change the color since 1st desktop file is no longer there but it will now delete the 2nd desktop file as "old file".
  4. No color can be changed since now both desktop files are already deleted.

My script had its own cleanup method for deleting old wallpaper files but it was time based and only deleted files older than 50 minutes.
After analysing the issue caused by this commit is comment the cleanup line out and saw it was not causing the problem, but this commit here.

Here is the script I use the change the menu bar for multiple desktops:

#!/bin/bash

if $(defaults read -g AppleInterfaceStyle &>/dev/null) ; then
    # if ok it set dark
    COLOR="#333333"
else
    # set to light as default
    COLOR="#CCCCCC"
fi

SPACES=`/usr/libexec/PlistBuddy -c "Print :SpacesDisplayConfiguration:Space\ Properties:" ~/Library/Preferences/com.apple.spaces.plist | grep name -c `

echo "Go to first space"
# go to first space control+option+1 (key code 18) and change menu bar
osascript -e 'tell application "System Events" to key code 18 using {control down, command down, option down}'
mint run igorkulman/ChangeMenuBarColor SolidColor "${COLOR}"
sync

for (( c=2; c<=$SPACES; c++ ))
do
  echo "Go to next space $c"
  # go to next space control+option+left_arrow (key code 124)
  osascript -e 'tell application "System Events" to key code 124 using {control down, command down, option down}'
  sync
  sleep 1
  mint run igorkulman/ChangeMenuBarColor SolidColor "${COLOR}"
  sync
done

echo "Go to first space"
# go to first space control+option+1 (key code 18)
osascript -e 'tell application "System Events" to key code 18 using {control down, command down, option down}'

# clear old temp backgrounds from ChangeMenuBarColor (only those older then 10 minutes)
#find ~/Library/Application\ Support/ChangeMenuBarColor -name '*.jpg' -mmin +50 -delete > /dev/null

#osascript -e 'tell application "Terminal" to quit'

@igorkulman
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run the utility for a space, get the generated wallpaper file, move it to a different location and set it as wallpaper with osascript

@Waitsnake
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many roads leads to Rome.
I just forked this repo and removed this commit here and now run in the script my own repo without this commit and this works too.
It was just a suggestion that there is maybe another ways to cleanup and that using ChangeMenuBarColor with multiple spaces can cause trouble.

Please sign in to comment.