Setting Desktop background via script

andrew_nguyen
New Contributor

Hi All, Have been attempting to push out a set desktop background for all users in the org.
7956756326ab4091adadeccf10e678d2

Not sure if I've got the right way about it but i have setup a pkg to push to all devices on the network which contains the .jpg file i wish to set. thats works fine.

however when i the script thats meant to set the picture run it keeps failing.
if i set that particular rule to be available by self service it works fine.

please see attachment for error message.

8 REPLIES 8

davidacland
Honored Contributor II
Honored Contributor II

Hi @andrew.nguyen

I normally do this with an OS X configuration profile. The restrictions payload has an option to set the desktop background.

On a side note, I generally put the JPG in /Library/CompanyName along with any company specific stuff. Any tmp or system directories may get wiped out by Apple or the vendor.

Björck
New Contributor II

I do this in 2 steps. One policy that just install the image in /Library/Desktops. Then one policy run this script to set the picture:
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Custom.jpg"'
I might very well be a login script of you want to set it every time a user logs in.

andrew_nguyen
New Contributor

Hi All, Thanks for the responses, the script posted by @Joppmann seems to be doing the trick, however it only seems to set the Background Picture of the Primary Display. I'm trying to get this to set all Display's to the defined Desktop Background.

I can't seem to find a way to make the above script work for that, all i can find is...
tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Lion.jpg"

this works if entered in AppleScript / SelfService (Casper) but not if i set as a policy, when reading the logs i get what is seen in the above screenshot,

Thanks for you help on this.

andrew_nguyen
New Contributor

Hi All, Have been making some progress here,

Have created a policy which runs the "File and Processes" command

osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Lion.jpg"'

If i enable this in Self Service it works fine, with the below message. (Desktop Pictures on both desktops set)...

[STEP 1 of 4]
Executing Policy Set Desktop Background
[STEP 2 of 4]
[STEP 3 of 4]
Running command osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Lion.jpg"'...
Result of command:
[STEP 4 of 4]

However if i set it up to run as a recurring task and deploy automatically i get the below...

Executing Policy Set Desktop Background
Running command osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/Lion.jpg"'...
Result of command:
36:104: execution error: An error of type -10810 has occurred. (-10810)

Have tried a number of different variations but always seems to bring back the same result.

The Script offered by Björck appears to work fine either Auto Deployed or Self Service however only sets the image on 1x screen. :(

davidacland
Honored Contributor II
Honored Contributor II

Does the error occur when the user is logged in or is it when the Mac is at the login Window?

bentoms
Release Candidate Programs Tester

@andrew.nguyen Is this background to be always set?

andrew_nguyen
New Contributor

Hi Guys, @davidacland ,I have the script to run at Login & Recurring @bentoms , Yes we would like to set this as the default background premamently

Cheers

bentoms
Release Candidate Programs Tester

@andrew.nguyen under Configuration Profiles, there is a restrictions payload that can force the desktop.

You'll need to deploy the image, then specify it's path in the profile.