Script to set desktop wallpaper for Mac
Device admins can set wallpapers remotely for endpoint devices by running a script. The following instructions will help you prepare the script that locates an image on the endpoint and sets it as the desktop wallpaper for the specified user. The script can be saved as a .sh file and executed on the managed device using Hexnode’s Execute Custom Script action.
Scripting language – Bash
File extension – .sh
Set desktop wallpaper
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "'"$path"'"'
if [ $ret == "0" ]; then
echo "Wallpaper set successfully "
echo "Operation failed."
The above code uses the
osascript command to run an AppleScript line.
path=”$1” – Reads the absolute location of the image file along with the file extension. The user folder specified on the path will be taken as the user for which the wallpaper is to be set and the user must be actively logged in while the script is executed. The argument $1 here must be entered while adding the script on the Execute Custom Script window.
A sample path can be like –
osascript –e – Runs the main AppleScript command from within the Shell environment.
tell application ‘Finder’ to set desktop picture to POSIX file “’”$path”’” – Use the ‘Finder’ app on the Mac to locate the image and set it as a desktop picture. The image location is given in the standard POSIX format.
The next lines set up an alert system to notify if the operation succeeded or failed. It follows the standard exit status of Bash.
When the script is initiated, a prompt appears at the endpoint device asking the user to allow the script to control Finder. Click OK button to allow the script to locate the image and set it as the wallpaper.