30

I trying to add to my OS X configuration script a command which sets the background to the plain dark grey, however, when applied, it only sets it to the main monitor and any additional monitors currently connected keep their current background and any added thereafter still receive the default space background. What I have so far is as follows:

# Change Desktop default BackgroundColor to grey
defaults write com.apple.desktop '{ Background = { default = {BackgroundColor = ( "0.2549019753932953", "0.4117647111415863", "0.6666666865348816" ); Change = Never; ChangePath = "/Library/Desktop Pictures/Solid Colors"; ChangeTime = 1800; DrawBackgroundColor = 1; ImageFileAlias = <00000000 00ce0003 00000000 c73804cd 0000482b 00000000 000c2624 000c2633 0000ca1c 0a310000 00000920 fffe0000 00000000 0000ffff ffff0001 000c000c 2624000c 25fc000a 0789000e 00280013 0053006f 006c0069 00640020 00470072 00610079 00200044 00610072 006b002e 0070006e 0067000f 000c0005 006f0073 00780038 00360012 00394c69 62726172 792f4465 736b746f 70205069 63747572 65732f53 6f6c6964 20436f6c 6f72732f 536f6c69 64204772 61792044 61726b2e 706e6700 00130001 2f00ffff 0000>; ImageFilePath = "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png"; NewChangePath = "/Library/Desktop Pictures/Solid Colors"; NewImageFilePath = "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png"; NoImage = 0; Placement = Crop; Random = 0; }; }; }'

How do I set the default the the dark grey background via a terminal command? How do I change all monitors already configured to grey via a terminal command?

3
  • That's a pretty useful set of commands! I also like your .aliases one too. Thanks!
    – binarybob
    Feb 16, 2012 at 23:28
  • I can't take all the credit, I forked it from mathiasbynens, I did add the keyboard, mouse, and non-working background settings though.
    – rudolph9
    Feb 17, 2012 at 3:15
  • Ohh and my mouse tracking speed is set hight than system preferences will allow :() thats me screaming because it tracks fast.
    – rudolph9
    Feb 17, 2012 at 3:17

12 Answers 12

27
+100

None of these other solutions work on Mavericks anymore because Apple moved the settings to a sqlite DB. But that's ok because now it's easier, the png can be anywhere in the filesystem, and all desktops (even virtual) are updated.

 #!/usr/bin/env bash
 sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '/path/to/any/picture.png'";
 killall Dock;

Or, add it as a function to your ~/.bash_profile and call it as a terminal command with any non-relative path.

#   Update all Wallpapers
function wallpaper() {
    sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$1'" && killall Dock 
}

wallpaper ~/path/to/any/picture.png

9
  • 2
    @jeremyjjbrown: thanks for you answer! Unfortunately this is not working in macOS Sierra anymore.. Do have an idea where Apple might now be saving which picture to use as wallpaper? Oct 2, 2016 at 10:27
  • 1
    @BenjaminHerzog The answer by Sanghyun Lee still works (on Mojave), just build from the GitHub source, not brew: apple.stackexchange.com/a/273628/1195
    – Mint
    Aug 26, 2019 at 7:21
  • 1
    killall Dock breaks window placement on multi-desktop, so this can't be used with frequency
    – phazei
    Apr 6, 2020 at 17:55
  • 2
    This did not work for me, and made Dock not start at all. I had to remove the database file to start Dock again.
    – HappyFace
    Oct 13, 2020 at 16:45
  • 10
    DO NOT DO THIS on macOS 11 (Big Sur). It prevents the Dock from running and even survives an OS re-install. Once I did rm -rf ~/Library/Application\ Support/Dock, the Dock instantly started.
    – Denis Howe
    Aug 3, 2021 at 20:20
21

This should work for you:

osascript
tell application "Finder"
set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png"
end tell

There are several other ways on this Mac Rumors thread, but his way is the easiest.

7
  • Did you get this to work your self? It didn't work for me.
    – rudolph9
    Feb 17, 2012 at 1:50
  • 1
    gave me ./a.scpt:31:46: execution error: Finder got an error: Can’t set file "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png" to file "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png". (-10006)
    – rudolph9
    Feb 17, 2012 at 2:09
  • Tried it again with a different color to see if it had something to do with the desktop already being set to dark grey and got ./a.scpt:31:46: execution error: Finder got an error: Can’t set file "/Library/Desktop Pictures/Solid Colors/Solid Gray Medium.png" to file "/Library/Desktop Pictures/Solid Colors/Solid Gray Medium.png". (-10006)
    – rudolph9
    Feb 17, 2012 at 2:10
  • @KurtRudolph Try running it within AppleScript Editor. Does it work there? I did not try it, but I believe I have in the past.
    – daviesgeek
    Feb 17, 2012 at 4:22
  • Based upon @faraz's answer, it would appear that you need to tell Apple Script that you're supplying a POSIX path by including the POSIX keyword before the file keyword, e.g. tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png" Jul 31, 2014 at 14:53
15

The simplest way to do it in one line:

osascript -e 'tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Earth Horizon.jpg"'
1
  • The method of using osascript works for me only if I run killall Dock afterward. Does anybody else notice that? Nov 11, 2015 at 22:22
11

None of the solution mentioned worked for me on macOS 10.14.2 Sierra and I ended up with this AppleScript:

tell application "System Events"
    tell every desktop
        set picture to "path_to_wallpaper.jpg"
    end tell
end tell

It can be run from a terminal like that:

osascript path_to_applescript_file.scpt

Or as a oneliner:

osascript -e 'tell application "System Events" to tell every desktop to set picture to "path_to_wallpaper.jpg"'
3
  • Note: IIRC this only works if "Change Picture" is unchecked, so I might add to the "tell every desktop" block: if get picture rotation is 1 then set picture rotation to 0 Aug 10, 2020 at 16:22
  • 1
    This is a little better than the "tell application Finder" method. It changes the backgrounds of all visible desktops. So, it works on multiple monitors, unlike the Finder one. However, it doesn't change the backgrounds of desktops that aren't currently visible.
    – Dan Jones
    Aug 9, 2021 at 18:42
  • Interestingly, all of these osascript based solutions seem to pick a random picture for me on Ventura 13.1. Even when I pick a file that doesn't exist, it sets a picture. (M1 MBP) My Wallpaper settings are set to randomly cycle through a folder of pics I have, and the osascript seems to be doing that, regardless of what picture I specify. Jan 18 at 20:53
2

If you prefer installing a new application to making a new function, using m-cli is the way to go. It's easy to use, and it has lots of other useful features if you need.

brew install m-cli 
m wallpaper ./wallpapers/tree.jpg

m-cli auto detects the version of OS, internally do the same things as other correct answers using bash script.

2
  • Thanks!! This one worked for me on macOS Mojave (though you need to get m-cli from the Github source, as the brew version is outdated from 2017)
    – Mint
    Aug 26, 2019 at 7:23
  • 2
    On Catalina, brew version broke the OS, Dock gone, alt-tab gone, expose gone. Needed to create a new user to fix
    – phazei
    Apr 6, 2020 at 17:54
1

The paths need to be properly referenced using POSIX file for /posix/paths or as alias with Old:Mac:Style:Paths.

Pick your poison...

POSIX path:

set desktopImage to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid Gray Dark.png"
tell application "Finder"
    set desktop picture to desktopImage
end tell

Mac path:

set localLibrary to path to library folder from local domain as string
set desktopImage to localLibrary & "Desktop Pictures:Solid Colors:Solid Gray Dark.png" as alias

tell application "Finder"
    set desktop picture to desktopImage
end tell

You can easily turn any of the above Applescripts into a terminal command using osascript [-e statement]:

osascript -e 'set desktopImage to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid Gray Light.png"
tell application "Finder"
    set desktop picture to desktopImage
end tell'

To set multiple desktops, you can try something like tell application "System Events" to set picture of every desktop to "<path>". Unfortunately, this seems to be broken in 10.7, so you'll end up getting the following error: Expected class name but found application constant or consideration.

1

NB: As of Mojave, the below solution does not work.


Combining jeremyjjbrown's answer and this Gist by willurd, the most convenient way for El Capitan and up is as follows:

  1. Open Automator (in Applications) and create a new Service
  2. Set Service receives selected to image files at the top of the workflow. You can optionally limit it to only work in Finder.

Sample image 1

  1. Add a Run Shell Script action to the workflow

  2. Empty the default script and insert a slightly modified version of jeremyjjbrown's answer into the editor:

    read -e IMAGE;
    sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$IMAGE'";
    killall Dock;
    
  3. Save the service with a name of your choosing, for example Set Desktop Picture on all Spaces.

You can now right-click any image directly from Finder and choose the service from the context menu, easily setting the background for all desktops.

1

This might not be the answer you are looking for, but it is by far my favorite (requires Python3):

I have always used pywal on all of my Linux machines, and I just wanted to see if it would also work on MacOS, and indeed it does. Not only will it change the desktop image for all desktops with a single command, but it also has the ability to set your Terminal colors to a theme to match your desktop background (as long as you are using iTerm2 and not Terminal.app).

https://github.com/dylanaraps/pywal/

I would suggest cloning it from the git repo and building it from source, as there is an issue with the current version on PyPI that does not include a fix to show the cursor location in iTerm2.

If you think it would be easier to install it from PyPI and patch the file, that is also an option -- just run pip3 install pywal, and then find the location of sequences.py in your site-packages/pywal directory (mine happened to be at ~/Library/Python/3.9/lib/python/site-packages/pywal/sequences.py), and then just copy sequences.py from the master branch to your site-packages, and you are good to go.

My full installation went something like this:

$ pip3 install --user pywal
$ find /usr/local -type f -name "sequences.py"
(or)
$ find $HOME -type f -name "sequences.py"
> ./Library/Python/3.9/lib/python/site-packages/pywal/sequences.py
$ git clone https://github.com/dylanaraps/pywal
$ cp pywal/sequences.py ~/Library/Python/3.9/lib/python/site-packages/pywal/sequences.py

Then all you have to do to set the wallpaper for all desktops is run the following:

wal -i $HOME/Pictures/Wallpapers/your_image.jpg

...and bingo-bango it sets the wallpaper for ALL desktops AND gives you nice pretty matchy colors in iTerm2.

There are instructions in the pywal wiki on how to make the terminal colors persist across reboots by adding a line to ~/.bash_profile.

So all that being said, while this method may take a bit more time to set up, in the long run, I find it by far to be the easiest and most satisfying.

Also, just a heads-up: wal -i /path/to/image does not work for some images, and I'm not sure why, but it worked for most of the images I tested.

3
  • 1
    As of August 2021, this should not be used on Big Sur. It will kill Dock. Your background will become black, your Dock will disappear, other elements of the UI will not work correctly. There is a fix (github.com/dylanaraps/pywal/issues/581), but it hasn't been released yet.
    – Dan Jones
    Aug 9, 2021 at 19:12
  • That is not true. I use the latest version of MacOS, and this works perfectly for me. Aug 20, 2021 at 14:41
  • Perhaps it works for systems upgraded from a previous version. On my completely new computer, it kills Dock, and a bunch of UI stuff stops working.
    – Dan Jones
    Aug 23, 2021 at 14:46
1

On Monterey the command has slightly changed:

osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"/path/to/file/goes/here.jpg\" as POSIX file"

Also for post-Monterey: I suspect you can look at this file to see the "latest" way to do it, so long as m-cli stays a living project.

1

You can use the command osascript on macOS Monterey 12.4 to set the picture on every desktop such as:

osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"/System/Library/Desktop Pictures/Solid Colors/Stone.png\" as POSIX file"

To find preinstalled desktop pictures for macOS Monterey:

find '/System/Library/Desktop Pictures' -not -path '*/.*'

To find preinstalled desktop pictures for macOS Big Sur:

find '/Library/Desktop Pictures' -not -path '*/.*'

If you prefer to make a script that's easier to read:

file="/System/Library/Desktop Pictures/Solid Colors/Stone.png"
osascript -e "tell application \"System Events\" to tell every desktop to set picture to \"${file}\" as POSIX file"
1
  • Great, up-to-date answer +1 Sep 6, 2022 at 8:30
0

A perl script to accomplish the same:

#!/usr/bin/perl
use Cwd;

if ($#ARGV != 1) {
    die "\nUsage: ./changedesktop <filename> <startup disk> \nwhere <filename> may be    any JPEG, PNG, GIF, TIFF or PSD and <startup disk> is the name of your boot volume.\n\n";
}

my $filename = $ARGV[0];
my $bootdisk = $ARGV[1];
my $directory = getcwd;
my $file = $bootdisk.$directory."/".$filename;
$file =~ tr/\//:/;
system("osascript -e \'tell application \"Finder\"\' -e \'set theFile to \"$file\"\' -   e     \'set desktop picture to \(theFile as alias\)\' -e \'end tell'");
1
  • 3
    No that is a perl script which executes running an osascript performing the same operation as the first post with a bunch of unnecessary perl code inputing the the image file which is to be set as the background.
    – rudolph9
    Feb 17, 2012 at 1:58
0

I use m-cli together with a little shell/apple script to change the wallpaper on all spaces.

#!/bin/bash
for i in {1..16}; do
    osascript -e 'tell application "System Events" to key code 124 using control down'
    sleep 1
    m wallpaper $1
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .