
How to Change Your macOS Wallpaper from the Command Line
While macOS offers a straightforward way to change your desktop background through System Settings, power users and developers often seek more control and automation. Manually clicking through menus feels inefficient if you want to script your environment, schedule changes, or simply prefer the speed of the terminal. Fortunately, a powerful command-line utility exists to give you complete control over your desktop wallpaper.
This guide will walk you through how to set, automate, and customize your macOS wallpaper directly from the command line, unlocking a new level of personalization for your digital workspace.
The Ultimate Tool: Introducing chwp
The key to this process is a lightweight and effective utility called chwp (Change Wallpaper). It’s a simple tool designed to do one thing and do it well: change the desktop wallpaper on macOS. Its power lies in its simplicity and its ability to be integrated into shell scripts, cron jobs, and other automation workflows.
Getting Started: Installing chwp
Before you can use chwp, you need to install it. The easiest method is through Homebrew, the popular package manager for macOS. If you don’t have Homebrew installed, you can get it from the official website.
Once Homebrew is ready, open your terminal and run the following command:
brew install chwp
That’s it. Homebrew will handle the download and installation, and the chwp command will be available system-wide.
Core Commands: Changing Your Wallpaper
Using chwp is incredibly intuitive. Here are the fundamental commands you’ll use most often.
Setting a Specific Image
To change your wallpaper to a specific image file, simply provide the path to that file.
chwp /Users/yourusername/Pictures/Wallpapers/space.jpg
Your desktop background will instantly update to the specified image.
Setting a Random Wallpaper from a Folder
One of the most powerful features is the ability to randomly select an image from a directory. This is perfect for keeping your desktop fresh without manual intervention. Just point chwp to a folder containing your favorite wallpapers.
chwp /Users/yourusername/Pictures/Wallpapers/
Every time you run this command, a new, random image from that folder will be set as your wallpaper.
Advanced Control for Power Users
chwp goes beyond the basics, offering granular control for more complex setups, especially those involving multiple monitors.
Managing Multiple Displays
If you work with more than one screen, you can specify which display’s wallpaper you want to change using the --screen flag.
- Change all screens at once:
bash
chwp --screen all /path/to/your/image.jpg
- Change only the main screen:
bash
chwp --screen main /path/to/your/image.jpg
- Change a specific screen by its index (0, 1, 2, etc.):
bash
chwp --screen 1 /path/to/your/other_image.png
Adjusting How the Image Fits
You can also control how the wallpaper image is scaled to fit your screen with the --scale flag. This is equivalent to the “Fill Screen,” “Fit to Screen,” and other options in System Settings.
fill(default): Zooms the image to fill the screen, cropping if necessary.fit: Resizes the image to fit the screen without cropping, which may result in letterboxing.stretch: Stretches the image to fill the screen, which can distort the aspect ratio.center: Centers the image on the screen at its original size.
Example usage:
chwp --scale fit /path/to/your/tall_image.jpg
Actionable Tip: Automate Your Desktop with a Cron Job
The real power of a command-line tool is automation. You can create a cron job—a scheduled task—to automatically change your wallpaper at set intervals. For example, let’s set up a cron job to pick a new random wallpaper from your folder every hour.
Open your crontab file for editing by running:
crontab -eAdd the following line to the file, making sure to replace the path with the correct path to your wallpapers folder. Important: You must use the full path to the
chwpbinary, which you can find by runningwhich chwp. It’s typically/opt/homebrew/bin/chwpon Apple Silicon Macs.0 * * * * /opt/homebrew/bin/chwp /Users/yourusername/Pictures/Wallpapers/Save and exit the editor.
Now, at the beginning of every hour, your Mac will automatically set a new, random wallpaper. This simple script ensures your desktop environment remains dynamic and inspiring without any effort. By mastering a simple utility like chwp, you can take back control and craft a more personalized and efficient macOS experience.
Source: https://www.linuxlinks.com/chwp-change-background-wallpaper-command-line/


