|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# AUTHOR: Steve Ward [steve@tech-otaku.com] |
| 4 | +# URL: https://github.com/tech-otaku/macos-desktop.git |
| 5 | +# README: https://github.com/tech-otaku/macos-desktop/blob/master/README.md |
| 6 | + |
| 7 | +# USAGE: [bash] /path/to/set-desktop-mojave.sh <desktop image> |
| 8 | + |
| 9 | + # Where <desktop image> can be... |
| 10 | + # HEIF (.heic) images |
| 11 | + # mojave = Mojave Dynamic |
| 12 | + # light = Mojave Light (Still) |
| 13 | + # dark = Mojave Dark (Still) |
| 14 | + # solar = Solar Gradients |
| 15 | + # non-HEIF (.heic) images e.g. |
| 16 | + # "/Library/Desktop Pictures/High Sierra.jpg" |
| 17 | + # Other |
| 18 | + # default = set the database [$db] to the default |
| 19 | + |
| 20 | +# EXAMPLE: [bash] /path/to/set-desktop-mojave.sh light |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +# # # # # # # # # # # # # # # # |
| 25 | +# FUNCTION DECLARATIONS |
| 26 | +# |
| 27 | + |
| 28 | +# Restore the user's ~/.sqliterc configuration file renamed to ~/.sqliterc-HH:MM:SS. |
| 29 | + function restore_sqliterc { |
| 30 | + if [ -f ${HOME}/.sqliterc-$timestamp ]; then |
| 31 | + mv ${HOME}/.sqliterc-$timestamp ${HOME}/.sqliterc |
| 32 | + fi |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +# # # # # # # # # # # # # # # # |
| 38 | +# VARIABLES |
| 39 | +# |
| 40 | + |
| 41 | +# db = database name including path |
| 42 | +# value = value of the `value` column in the `data` table for new rows |
| 43 | +# key = value of the `key` column in the `preferences` table for new rows |
| 44 | +# image = the title or filename of the Desktop image |
| 45 | +# lastrow[0] = row id of the row in the `data` table with the highest row id |
| 46 | +# lastrow[1] = row id of the row in the `preferences` table with the highest row id |
| 47 | + |
| 48 | + db=${HOME}/Library/Application\ Support/Dock/desktoppicture.db |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +# # # # # # # # # # # # # # # # |
| 53 | +# DISABLE SQLITE3 CONFIG FILE |
| 54 | +# |
| 55 | + |
| 56 | +# To avoid unexcpected results the user's ~/.sqliterc configuration file may cause when `sqlite3` is executed, stop it being loaded by temporarily renaming it to ~/.sqliterc-HH:MM:SS |
| 57 | + if [ -f ${HOME}/.sqliterc ]; then |
| 58 | + timestamp=$(date '+%H-%M-%S') |
| 59 | + mv ${HOME}/.sqliterc ${HOME}/.sqliterc-$timestamp |
| 60 | + fi |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +# # # # # # # # # # # # # # # # |
| 65 | +# USAGE CHECKS |
| 66 | +# |
| 67 | + |
| 68 | +# Exit with error if OS version is not 10.14 |
| 69 | + if [ $(system_profiler SPSoftwareDataType | awk '/System Version/ {print $4}' | cut -d . -f 2) -ne 14 ]; then |
| 70 | + echo "ERROR: For use with macOS Mojave 10.14.x only." |
| 71 | + restore_sqliterc |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + |
| 75 | +# Exit with error if no Desktop image was passed on the command line. |
| 76 | + if [ -z "$1" ]; then |
| 77 | + echo "ERROR: No image was specified." |
| 78 | + restore_sqliterc |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + |
| 82 | +# Exit with error if the Desktop image is a file that doesn't exist or an invalid option. |
| 83 | + #if [[ "$1" != "default" && "$1" != "mojave" && "$1" != "light" && "$1" != "dark" && "$1" != "solar" ]]; then |
| 84 | + if [ "$1" != "default" ] && [ "$1" != "mojave" ] && [ "$1" != "light" ] && [ "$1" != "dark" ] && [ "$1" != "solar" ]; then |
| 85 | + re='\.' |
| 86 | + if [[ $1 =~ $re ]]; then # $1 contains a . denoting a filename.ext |
| 87 | + if ! [ -f "$1" ]; then |
| 88 | + echo "ERROR: '$1' doesn't exist." |
| 89 | + restore_sqliterc |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + else |
| 93 | + echo "ERROR: '$1' is not a valid option." |
| 94 | + restore_sqliterc |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + fi |
| 98 | + |
| 99 | +# Exit with error if this is a dual-monitor environment (the `displays` table will have 2 or more rows). |
| 100 | + if [ $(sqlite3 "$db" "SELECT COUNT() FROM displays;") -ge 2 ]; then |
| 101 | + echo "ERROR: This script should not be used in a dual-monitor environment." |
| 102 | + restore_sqliterc |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | + |
| 106 | +# Exit with error if multiple Desktops are configured (the `spaces` table will have 2 or more rows). |
| 107 | +# if [ $(sqlite3 "$db" "SELECT COUNT() FROM spaces;") -ge 2 ]; then |
| 108 | +# echo "ERROR: This script should not be used when multiple Desktops (Spaces) are configured." |
| 109 | +# restore_sqliterc |
| 110 | +# exit 1 |
| 111 | +# fi |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +# We've got this far, let's close System Preferences – if open – before continuing. |
| 116 | + killall System\ Preferences > /dev/null 2>&1 # Write STDOUT and STDERR to /dev/null to supress messages if process isn't running |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +# # # # # # # # # # # # # # # # |
| 121 | +# SET-UP |
| 122 | +# |
| 123 | + |
| 124 | + key=20 |
| 125 | + case "$1" in |
| 126 | + mojave) |
| 127 | + value=1 |
| 128 | + image="Mojave Dynamic" |
| 129 | + ;; |
| 130 | + light) |
| 131 | + value=2 |
| 132 | + image="Mojave Light (Still)" |
| 133 | + ;; |
| 134 | + dark) |
| 135 | + value=3 |
| 136 | + image="Mojave Dark (Still)" |
| 137 | + ;; |
| 138 | + solar) |
| 139 | + value="'/Library/Desktop Pictures/Solar Gradients.heic'" |
| 140 | + key=1 |
| 141 | + image="/Library/Desktop Pictures/Solar Gradients.heic" |
| 142 | + ;; |
| 143 | + default) |
| 144 | + # Set the database [$db] to the default and exit. Only works if $db is ~/Library/Application\ Support/Dock/desktoppicture.db |
| 145 | + if [[ "$db" == $HOME/Library/Application\ Support/Dock/desktoppicture.db ]]; then |
| 146 | + ! [ -d $HOME/Library/Application\ Support/Dock/backup ] && mkdir $HOME/Library/Application\ Support/Dock/backup |
| 147 | + mv "$db" $HOME/Library/Application\ Support/Dock/backup/desktoppicture-$(date '+%H-%M-%S').db # rename the current database [backup/desktoppicture-hh-mm-ss.db] first |
| 148 | + killall Dock # As desktoppicture.db no longer exists, this creates a new one with default values |
| 149 | + echo "The Desktop image has been set to the default." |
| 150 | + restore_sqliterc |
| 151 | + exit 0 |
| 152 | + else |
| 153 | + echo "ERROR: The 'default' option is only appropriate when the database is $HOME/Library/Application Support/Dock/desktoppicture.db" |
| 154 | + restore_sqliterc |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | + ;; |
| 158 | + *) |
| 159 | + value="'$1'" |
| 160 | + image="$1" |
| 161 | + key=1 |
| 162 | + ;; |
| 163 | + esac |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +# # # # # # # # # # # # # # # # |
| 168 | +# GET LAST ROW IDS |
| 169 | +# |
| 170 | + |
| 171 | +# Get the rowid of the last row to be inserted in the `data` table. This will be used to insert new rows later in the script. |
| 172 | + lastrow[0]=$(sqlite3 "$db" "SELECT ROWID FROM data ORDER BY ROWID DESC LIMIT 1;") |
| 173 | + |
| 174 | +# if `data` contains 0 rows, lastrow[0] is empty/null so explicitily set lastrow[0] to 0 |
| 175 | + [ -z "${lastrow[0]}" ] && lastrow[0]=0 |
| 176 | + |
| 177 | +# Get the rowid of the last row to be inserted in the `preferences` table. This will be used to insert new rows later in the script. |
| 178 | + lastrow[1]=$(sqlite3 "$db" "SELECT ROWID FROM preferences ORDER BY ROWID DESC LIMIT 1;") |
| 179 | + |
| 180 | +# if `preferences` contains 0 rows, lastrow[1] is empty/null so explicitily set lastrow[1] to 0 |
| 181 | + [ -z "${lastrow[1]}" ] && lastrow[1]=0 |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +# # # # # # # # # # # # # # # # |
| 186 | +# DELETE ALL ROWS |
| 187 | +# |
| 188 | + |
| 189 | +# Delete all rows in the `data` table. |
| 190 | + sqlite3 "$db" "DELETE FROM data;" |
| 191 | + |
| 192 | +# Delete all rows in the `preferences` table. Causes the `preferences_deleted` trigger to fire. |
| 193 | + sqlite3 "$db" "DELETE FROM preferences;" |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +# # # # # # # # # # # # # # # # |
| 198 | +# INSERT NEW ROWS |
| 199 | +# |
| 200 | + |
| 201 | +# Insert a new row into the `data` table. |
| 202 | + sqlite3 "$db" "INSERT INTO data(rowid,value) VALUES( $((lastrow[0] + 1)), $value );" |
| 203 | + |
| 204 | +# Insert four new rows into the `preferences` table. |
| 205 | + sqlite3 "$db" "INSERT INTO preferences(rowid,key,data_id,picture_id) VALUES( $((lastrow[1] + 1)),$key,$((lastrow[0] + 1)),3);" |
| 206 | + sqlite3 "$db" "INSERT INTO preferences(rowid,key,data_id,picture_id) VALUES( $((lastrow[1] + 2)),$key,$((lastrow[0] + 1)),4);" |
| 207 | + sqlite3 "$db" "INSERT INTO preferences(rowid,key,data_id,picture_id) VALUES( $((lastrow[1] + 3)),$key,$((lastrow[0] + 1)),2);" |
| 208 | + sqlite3 "$db" "INSERT INTO preferences(rowid,key,data_id,picture_id) VALUES( $((lastrow[1] + 4)),$key,$((lastrow[0] + 1)),1);" |
| 209 | + |
| 210 | + echo "The Desktop image has been set to $image." |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | +# # # # # # # # # # # # # # # # |
| 215 | +# FINISH UP |
| 216 | +# |
| 217 | + |
| 218 | +# Restart the Dock so changes to desktoppicture.db take effect |
| 219 | + killall Dock |
| 220 | + |
| 221 | +# Restore the user's ~/.sqliterc configuration file (if) renamed earlier in this script. |
| 222 | + restore_sqliterc |
0 commit comments