Setting Desktop Picture to be centered with a specific background color

JayDuff
Contributor II

So, I've seen a number of threads about how to set the desktop. We push the picture into /Users/Shared/ then use a Configuration Profile to set and lock the desktop to be that picture. The problem is that the picture is small enough to fit on our smallest screens, but our bigger screens stretch it out.

In the System Preferences > Desktop & Screen Saver pane, there is a menu set to Fill Screen. If I change that value to Center, there is a color sampler that comes up, and lets me set the color of the desktop that is not covered by the picture.

Given constants for the color, how do I use Casper (or a script) to set the way the background image is handled to be Center instead of Fill Screen, and the background color to be the constants (RGB, CMYK, or HSB)?

Thanks!

22 REPLIES 22

RobertBasil
Contributor

I'm looking for the exact same thing. I'll be bookmarking this one.

RobertBasil
Contributor

My script is still a work in progress but here is what I've done so far to add padding to the wallpaper image so it centers it on our iMacs using AppleScript.

tell application "Image Events" launch set Wallpaper to open "~/Downloads/wallpaper.jpg" pad Wallpaper to dimensions {5120, 2880} save Wallpaper close Wallpaper end tell

Have not figured out how to change the padded color yet.

bentoms
Release Candidate Programs Tester

@JayDuff I tend to make an image the same size of Apple's Desktop Images, then set that.. that seems to scale up & down nicely.

RobertBasil
Contributor

@bentoms

I wish I could do that, but the image we use for our backgrounds is created by our HR department and changed every month. It is stored on a network drive for our windows computers which are a much bigger part of our network than our iMacs.

I really don't want to have to resize the image they create by hand every month.

joshuasee
Contributor III

You can change the padding color in Image Events. Colors in AppleScript are specified with a three item list of 16-bit values. If you want to match one, run the AppleScript command "choose color" and then click the dropper to pick the color you want. I pulled the red value below from the Chandler USD home page.

With a little more work you should be able to get the Macs to pull the image off the network share and then resize them automatically.

tell application "Finder" to set dims to items 3 thru 4 of (get bounds of window of desktop)

tell application "Image Events"
    launch
    set Wallpaper to open "~/Downloads/wallpaper.jpg"
    pad Wallpaper to dimensions dims with pad color {13673, 1438, 4681}
    save Wallpaper
    close Wallpaper
end tell

RobertBasil
Contributor

@joshuasee

Thanks!

Here is the script (with your addition).

tell application "Finder" activate mount volume "smb://*@do-fs1/apps/wallpaper" end tell do shell script "cp /volumes/wallpaper/wallpaper.jpg ~/Downloads" do shell script "diskutil unmountDisk /Volumes/wallpaper" tell application "System Events" tell current desktop set picture to "~/Downloads/wallpaper.jpg" end tell end tell tell application "Finder" to set dims to items 3 thru 4 of (get bounds of window of desktop) tell application "Image Events" launch set Wallpaper to open "~/Downloads/wallpaper.jpg" pad Wallpaper to dimensions dims with pad color {13673, 1438, 4681} save Wallpaper close Wallpaper end tell

JayDuff
Contributor II

Great stuff here, @joshuasee & @RobertBasil !

Our desktop image is static, so I push it into /Users/Shared/ as part of a package. I'm trying to run Robert's script as a policy that includes the package. My script looks like this:

#!/bin/sh
osascript<<END
tell application "System Events"
    tell current desktop
        set picture to "/Users/Shared/83Logo.jpg"
    end tell
end tell
tell application "Finder" to set dims to items 3 thru 4 of (get bounds of window of desktop)
tell application "Image Events"
    launch
    set Wallpaper to open "/Users/Shared/83Logo.jpg"
    pad Wallpaper to dimensions dims with pad color {16705, 26985, 43690}
    save Wallpaper
    close Wallpaper
end tell
END

The desktop is now set to a properly padded image, but the color is gray instead of the color above. When I run a sudo jamf policy, the script clearly runs, gives an exit code 0 and no result.

I think Image Events crashed. And now the image file is changed to have the wrong padding color.

Once I get the script to download the package (it's using HTTP instead of AFP/SMB), I think I might be able to make it work.

RobertBasil
Contributor

Delete please

RobertBasil
Contributor

Delete please

JayDuff
Contributor II

Delete please.

RobertBasil
Contributor

When I test I just scope it to my test machine only and have the policy to to once per login.

RobertBasil
Contributor

When I test I just scope it to my test machine only and have the policy to to once per login.

JayDuff
Contributor II

@RobertBasil Any idea why Image Events would stop modifying the picture file? Events says it's working. Replies indicates the file is being accessed. However, the image file is not being changed! Tried rebooting. Tried just putting the script into the Script Editor and that's how I saw that Events and Replies look normal. It's almost as if the file is being shadowed. It's in /Users/Shared/ - that's not sandboxed in El Cap, is it?

RobertBasil
Contributor

Not sure about the sandboxing, I store ours in ~/Downloads. To be honest after the initial install I have not yet tried to update it a second time as we only change ours on a monthly basis.

I'll make a small change to the image file and let it run tonight to see if I have the same issue.

joshuasee
Contributor III

FWIW, the do shell script calls probably aren't necessary:

#!/usr/bin/osascript
tell application "Finder"
    activate -- Is this here for the credentials prompt?
    mount volume "smb://*@do-fs1/apps/wallpaper"

    repeat 3 times -- if copy fails, keep trying. You could put the mount and eject in here, too.
        try
            set Wallpaper to (duplicate (alias "wallpaper:wallpaper:wallpaper.jpg") to (path to downloads folder) with replacing)
            exit repeat
        end try
    end repeat

    eject disk "wallpaper"

    set dims to items 3 thru 4 of (get bounds of window of desktop)
end tell

tell application "System Events"
    tell every desktop
        set picture to Wallpaper
    end tell
end tell

tell application "Image Events"
    set Wallpaper to open Wallpaper
    pad Wallpaper to dimensions dims with pad color {13673, 1438, 4681}
    save Wallpaper
    close Wallpaper
end tell

JayDuff
Contributor II

So I figured out why the padding is gray. The COLOR SPACE of the original file is Gray! So even though I'm padding it with a color, the original file's color space is not getting changed, so it looks gray.

Next question - how do I change a file's color space with AppleScript? Image Events manual says Color Space is Read-only.

RobertBasil
Contributor

@JayDuff

That's strange that it's not working for you.

tell application "Image Events"
    set Wallpaper to open Wallpaper
    pad Wallpaper to dimensions dims with pad color {13673, 1438, 4681}
    save Wallpaper
    close Wallpaper
end tell

The above changes the pad color for me every time.

I should also state I had to declare the wallpaper variable in my script:

set Wallpaper to "~/Downloads/wallpaper.jpg"

JayDuff
Contributor II

@RobertBasil It works for you because your original image is using a color Color Space. My original image is a black and white logo. So the file (a JPEG) has been saved with a "Gray" color space. I could just re-save the file as a color picture, but there has GOT to be a way to simply modify the color space of the file.

I think the answer is sips, and the code is:

#Variable declarations omitted
sips 
    --matchTo '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' 
    "$SOURCE_FILE" 
    --out "$DESTINATION_FILE"

In fact, sips may be able to everything I need, all in bash!

RobertBasil
Contributor

Doh, didn't realize you were using a B/W logo.

JayDuff
Contributor II

This is getting ugly, and I just thought of a fatal flaw....

Our desktop computers have resolutions of 1920x1080 or more. But most of our projectors top out at 1024x768. When those desktops mirror to those projectors, it's not going to look right at all, is it?

There has to be a way to do this with the MDM.

I scoped a Configuration Profile, with a restriction, that sets the Desktop picture path, and locks the picture. It's doing that part perfectly. The only problem is that the picture is set to Fill Screen and I can't see any place to change Fill Screen to Center. Setting the padding color would be a nice bonus.

There has GOT to be a way to do this - maybe with a custom setting value in the Profile?

RobertBasil
Contributor

Delete

joshuasee
Contributor III

@JayDuff

SIPS and Image Events use the same engine. Given that it sound like you don't need user interactivity, I'd go ahead and do the conversion in bash.

As for the display resolution, if the computer can find a way to display on the projector at all it will scale and crop the desktop wallpaper to suit. How exactly depends on the display settings, such as if you are mirroring or extending the desktop.