Reputation: 21
macOS lets users automate desktop background changes by rotating through images in the folder as described in Apple Support.
I use the feature to rotate through a 'Wallpaper' folder in iCloud every 30 minutes. I've tried to set up a command to make this change from the command line but it's currently setting the background to the default image in macOS High Sierra.
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '/Library/Mobile Documents/com~apple~CloudDocs/Wallpaper'" && killall Dock
Any tips would be much appreciated!
Upvotes: 2
Views: 4999
Reputation: 31
As part of setting up my dotfiles, I wanted have it randomly select a wallpaper from my collection of images. The above solution seemed just what I needed, but I ran into some issues.
shuf was not an available command in my environment
manipulating the desktoppicture.db database failed for me in a couple important instances:
Initially the data table in desktoppicture.db holds no records. This is the state on a new machine or freshly installed OS, where it shows the default background. Calling the 'update data set value' command in this case will fail, since there is no previous value to update.
If the user goes into the Desktop tab of "Desktop & Screen Saver" in System Preferences and adds a folder to the list of places to look for wallpaper images, then the path to this folder gets added to the data table in desktoppicture.db. Calling the 'update data set value' command in this case works successfully, just not as intended. It causes the Dock to fail to start, probably because there is now file in a spot where the system expected a folder path. Fortunately, deleting desktoppicture.db file allows the system to create a new one.
If the user uses "Desktop & Screen Saver" to both add a folder and pick a wallpaper, the data table ends up with two rows, one for each value. In this situation, 'update data set value' changes both rows to have the same new value. Again, not so good for Dock's ability to run.
To address these issues, I went with the following:
#!/usr/bin/env bash
walls="$HOME/projects/dot/wall"
wall=$(ls -1 "$walls" | sort --random-sort | head -1)
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "'"$walls/$wall"'"'
Upvotes: 2
Reputation: 78
I arrived here via google. Your answer is almost there, but you're trying to set the wallpaper image to a directory instead of an image file. Here's what I use
#!/bin/zsh
dir='~/Wallpapers'
file=$(ls -1 $dir/*.jpg | shuf -n 1)
echo $file
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '${file}'" && killall Dock
Save this as a file called rand-wallpaper.sh
or something and have it run on a cron job every 30 minutes.
You could easily make this into a shell function that takes the directory as a command line argument and switch between different directories.
So, this will give you the effect you want, but I'm not sure how to have it show up in the actual system preferences,
Upvotes: 0