macOS: Change wallpaper by CLI

published on in category macOS , Tags: macos apple macbook applescript jax javascript

Table of contents

Current macOS versions are weird… Often when I connect my MacBook Pro to the two screens on my desk, the wallpaper on one of that guys is either gone or changed to a different one.

After many attempts to search them in my bunch of 2k wallpapers, I eventually gave up and wrote a script that sets the wallpaper on all my screens like this:

osascript -e 'tell application "System Events" to set picture of every desktop to ("/some/path/wallpaper.png" as POSIX file as alias)'

This is a simple call to osascript (Open Scripting Architecture… script) which evaluates the AppleScript provided by -e.

Who thought it would be that easy? Have fun!


ps. if you feel fancy, here’s the JAX (JavaScript for Automation) implementation:

var se = Application('System Events');

for (desktopIndex in se.desktops) {
    se.desktops[desktopIndex].picture = '/some/path/wallpaper.png';
}

This can be run e.g. using osascript -l JavaScript -e '...'.