Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Last active January 31, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptingosx/de6380d97ba2a2274843edd99f540b96 to your computer and use it in GitHub Desktop.
Save scriptingosx/de6380d97ba2a2274843edd99f540b96 to your computer and use it in GitHub Desktop.
Sample script to set the desktop with desktoppr
#!/bin/sh
##
## sets the desktop using `desktoppr`
##
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
# set the path to the desktop image file here
picturepath="/Library/Desktop Pictures/BoringBlueDesktop.png"
# verify the image exists
if [ ! -f "$picturepath" ]; then
echo "no file at $picturepath, exiting"
exit 1
fi
# verify that desktoppr is installed
desktoppr="/usr/local/bin/desktoppr"
if [ ! -x "$desktoppr" ]; then
echo "cannot find desktoppr at $desktoppr, exiting"
exit 1
fi
# get the current user
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
# test if a user is logged in
if [ -n "$loggedInUser" ]; then
# get the uid
uid=$(id -u "$loggedInUser")
# do what you need to do
sudo -u "$loggedInUser" "$desktoppr" "$picturepath"
else
echo "no user logged in, no desktop set"
fi
@built2order
Copy link

built2order commented Apr 19, 2020

Hi - Love the idea of the app and the ability to now set a background color and scale the image appropriately is exactly what I've been looking for for sometime. I'm just wondering thought, have you tried running your command on the latest Catalina version 10.15.4?

sudo -u "$loggedInUser" "$desktoppr" "$picturepath"

I'm not getting the desired outcome, the command simply runs without and output and the requested changes not implemented. I've verified that the commands work as run manually by the user ruling out permissions, variable paths, fat thumbs!

Executing the command without switches (ie. sudo -u "$loggedInUser" "$desktoppr") returns with configuration data, however it's not correct!

I'm stumped!

@scriptingosx
Copy link
Author

It works for me and I am on the latest 10.15.4 (19E287). At this point you should check: is desktoppr installed correctly in the expected path? is it the latest version? Is the image you reference in picturepath installed when the script runs? is the path correct?

you might want to place a set -x at the beginning the of the script to get a detailed trace while it runs.

@charliwest
Copy link

Thanks for this and the desktoppr tool!

In this script why is there this?
# get the uid
uid=$(id -u "$loggedInUser")
I don't see it used anywhere

@scriptingosx
Copy link
Author

because an earlier version of the script used

launchctl asuser "$uid" "$desktoppr" "$picturepath"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment