Place the image on the device ahead of time, and use a configuration profile to set the wallpaper on macOS. This should solve all your problems.
Do you want the users to have the ability to change the desktop background? You can set one via config profiles.
In the past I've packaged the desktop background and the desktoppr command line tool with the following script:
this allows the user to change the desktop background or you can redeploy it as a reoccurring policy if needed (I only set it on enrollment).
#!/bin/sh
##
## sets the desktop using `desktoppr`
##
# set the path to the desktop image file here
picturepath="/path/to/desktop/background.jpg"
# only run when installing on System Volume
#
#if [ "$3" != "/" ]; then
#
# echo "Not installing on /, not setting desktop"
#
# exit 0
#
#fi
# verify the image exists
if [ ! -f "$picturepath" ]; then
echo "no file at $picturepath, exiting"
exit 1
fi
# path to current directory of the script
scriptdir=$(dirname "$0")
# try to locate desktoppr
if [ -x "$scriptdir/desktoppr" ]; then
desktoppr="$scriptdir/desktoppr"
elif [ -x "/usr/local/bin/desktoppr" ]; then
desktoppr="/usr/local/bin/desktoppr"
else
echo "cannot find desktoppr, exiting"
exit 1
fi
# get the current user
loggedInUser=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name
{ print $3 }')
uid=$(id -u "$loggedInUser")
if [ "$loggedInUser" != "loginwindow" ]; then
# set the desktop for the user
launchctl asuser "$uid" "$desktoppr" "$picturepath"
else
echo "no user logged in, no desktop set"
fi
exit 0
exit 1
The error message you're encountering could be caused by a number of different issues, but here are some possible steps to troubleshoot the issue:
Make sure the correct permissions are set: You need to ensure that the user account that's running the script has the correct permissions to modify the wallpaper. If you're using sudo, make sure that the user has the necessary permissions.
Check for any restrictions: If there are any restrictions in place that prevent the user from modifying the wallpaper, the script will fail. Check for any restrictions in the System Preferences, Parental Controls, or in a configuration profile that might be affecting the user.
Check for any compatibility issues: The script might not be compatible with the version of macOS you're running, or with the hardware architecture. Try running the script on a different Mac that has a similar configuration to see if it works.
Use a different method to set the wallpaper: Instead of using osascript, you can try using another method to set the wallpaper, such as sqlite3 or defaults. This might help you determine if the issue is with osascript specifically, or if there's a problem with the script itself.
Place the image on the device ahead of time, and use a configuration profile to set the wallpaper on macOS. This should solve all your problems.
I took this route. Might choose one of the others suggestions if I want to allow teachers ability to change. However for the time being your suggestion works. I just have it download wallpaper on enrollment and then used the profiles to set wallpaper.