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
@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