Setting the Wallpaper - Script Error

rtarson
New Contributor II

Hello,

 

So I am trying to get the wallpaper login policy to set the wallpaper, it works on one of my test machines Mac Book Air that running a M1 chip but the iMac that I have thats running a intel i5 chip is not handling the osascript. It errors with this: 33:48: execution error: Finder got an error: AppleEvent handler failed. (-10000)

 

 

 

#!/bin/bash

#Create Directory for Custom Image
mkdir '/Library/Wallpaper'

# Change directories 
cd '/Library/Wallpaper/'

# Download the wallpapercurl -k -Os https://linktowallpaper.com -s

cd '../..'

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{print $3}')

sudo -u "$currentUser" -H osascript -e 'tell application "Finder" to set desktop picture to "/Library/Wallpaper/Background.jpg"'

 

 

1 ACCEPTED SOLUTION

AJPinto
Honored Contributor II

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.

View solution in original post

4 REPLIES 4

AJPinto
Honored Contributor II

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.

rtarson
New Contributor II

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.

Anonymous
Not applicable

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

 

Jaykrishna1
Contributor II

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.