Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change scaling for all desktop backgrounds on Mac via a script

I'm trying to set up a script that automatically changes all desktop (spaces) backgrounds on a Mac. I've gotten as far as this answer that lets you change all the desktop backgrounds to an image via the quick script of

#!/bin/sh
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '/Users/user/Documents/My Pictures/dlanham_Phobos.sitechange.jpg'";
killall Dock;

However, that defaults to the "Fill Screen" option, and I'd like to use "Fit to Screen" to show the whole image (and preferably set the background color too).

I've been playing around with the desktoppicture.db database, and I can see certain things change when I do change Fit to Screen, but I can't quite work out what needs to be added to the script to get all desktops to show the same image, but scaled appropriately.

like image 636
Ethan K. Avatar asked Nov 04 '15 22:11

Ethan K.


People also ask

How do I scale my desktop background?

Type in Paint in the search box near the Start button. Click on File, select Open and choose your image. From the Home tab, click Resize and navigate to Resize and Skew. Set the size for your future resized image.


1 Answers

I stumbled upon this problem myself today and did a little reverse engineering. So there are actually 4 essential steps that will accomplish the things you want:

  1. Clear the entire database. (This is not mandatory but makes everything much easier)

    DELETE FROM data
    DELETE FROM displays;
    DELETE FROM pictures;
    DELETE FROM preferences;
    DELETE FROM prefs;
    DELETE FROM spaces;
    
  2. Add an empty row to the pictures table. As far as I understood it, this somehow works as a fallback and macOS automatically creates additional rows with according display IDs and space IDs based on everything we define now.

    INSERT INTO pictures (space_id, display_id) VALUES (null, null);
    
  3. Add your actual preferences to the data table.

    INSERT INTO data (value) VALUES ('~/Pictures'); -- #10
    INSERT INTO data (value) VALUES (60); -- #11
    INSERT INTO data (value) VALUES (5); -- #2
    INSERT INTO data (value) VALUES (0.07); -- #3, #4, #5
    INSERT INTO data (value) VALUES ('current-wallpaper.jpg'); -- #16
    INSERT INTO data (value) VALUES (1); -- #11 / #12
    
  4. Now this is the most important part — the actual mapping of the data with the appropriate macOS internal preferences keys. This is what I found out so far:

    #1: Image path
    #2: Scaling method (Fill Screen (1), Center (3), Stretch to Fill Screen (4), Fit to Screen (5))
    #3: Fill color
    #4: Fill color
    #5: Fill color
    #9: Enable automatic changing
    #10: Directory path to images
    #11: Image changing interval
    #12: Random order
    #16: Current image (used when automatic changing is enabled)
    

    So all you have to do is to match the IDs of the data rows with the keys listed above. For example

    INSERT INTO preferences (key, data_id, picture_id) VALUES (11, 2, 1);
    

    The line above means: Use the 2nd row from the data table (→ 60) as the Image changing interval (#11) for the 1st picture (which in our case means all spaces/displays by default).


In my case I wanted to change the wallpaper every minute to a random image from a specific folder with Fit to Screen enabled and a 7% bright black background color. I achieved it with that line:

sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db " \
    DELETE FROM data; \
    DELETE FROM displays; \
    DELETE FROM pictures; \
    DELETE FROM preferences; \
    DELETE FROM prefs; \
    DELETE FROM spaces; \
    INSERT INTO pictures (space_id, display_id) VALUES (null, null); \
    INSERT INTO data (value) VALUES ('~/Pictures'); \
    INSERT INTO data (value) VALUES (60); \
    INSERT INTO data (value) VALUES (5); \
    INSERT INTO data (value) VALUES (0.07); \
    INSERT INTO data (value) VALUES ('198.png'); \
    INSERT INTO data (value) VALUES (1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (2, 3, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (3, 4, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (4, 4, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (5, 4, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (9, 6, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (10, 1, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (11, 2, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (12, 6, 1); \
    INSERT INTO preferences (key, data_id, picture_id) VALUES (16, 5, 1); \
" && killall Dock

Hope this helps anyone out there struggling with this.

like image 138
mzdr Avatar answered Sep 24 '22 14:09

mzdr