-
Notifications
You must be signed in to change notification settings - Fork 17
/
bing-wallpaper.sh
executable file
·352 lines (316 loc) · 11 KB
/
bing-wallpaper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/sh
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
readonly SCRIPT=$(basename "$0")
readonly VERSION='1.1.1'
RESOLUTIONS=(1920x1200 1920x1080 1024x768 1280x720 1366x768 UHD)
MONITOR="0" # 0 means all monitors
PLIST_FILE="$HOME/Library/LaunchAgents/com.bing-wallpaper-daily-mac-multimonitor"
AUTO_UPDATE_NAME="default"
ARGS=$@
usage() {
cat <<EOF
Usage:
$SCRIPT [options]
$SCRIPT -h | --help
$SCRIPT --version
Options:
enable-auto-update Enable automatic update of wallpapers every day
the picture if the filename already exists.
disable-auto-update Disable automatic update of wallpapers every day
the picture if the filename already exists.
info Show description of current wallpaper.
--auto-update-name <name> Name of your auto update when enabling/disabling
Using custom name enables setting multiple automatic update configurations.
Eg. Set on monitor 1 todays wallpaper and on monitor 2 wallpaper from yesterday
-f --force Force download of picture. This will overwrite
the picture if the filename already exists.
-s --ssl Communicate with bing.com over SSL.
-q --quiet Do not display log messages.
-c --country <coutry-tag> Specify market country/region eg. en-US, cs-CZ
Pictures may be different for markets on some days.
See full list of countries on https://learn.microsoft.com/en-us/previous-versions/bing/search/dd251064(v=msdn.10)
-d --day <number> Day for which you want to get the picture.
0 is current day, 1 is yesterday etc.
Default is 0.
-n --filename <file name> The name of the downloaded picture. Defaults to
the upstream name.
-p --picturedir <picture dir> The full path to the picture download dir.
Will be created if it does not exist.
[default: $HOME/Pictures/bing-wallpapers/]
-r --resolution <resolution> The resolution of the image to retrieve.
Supported resolutions: ${RESOLUTIONS[*]}
--resolutions <resolutions> The resolutions of the image try to retrieve.
eg.: --resolutions "1920x1200 1920x1080 UHD"
-m --monitor <num> Set wallpaper only on certain monitor (1,2,3...)
--all-desktops-experimental Set wallpaper on all desktops
Fixing osascript bug when wallpaper is not set for Desktop 2.
Known issue: Minimized apps are removed from Dock.
If something goes wrong delete Library/Application Support/Dock/desktoppicture.db
and restart your Mac.
-h --help Show this screen.
--version Show version.
EOF
}
create_plist_in_users_agents_folder() {
local SCRIPT_PATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/$(basename "${BASH_SOURCE:-$0}")
local REST_ARGS=$(echo "$ARGS" | sed -e "s/enable-auto-update//")
if [ $RUN_USING_NPX ]; then
local COMMANDS="<string>source ~/.bashrc && npx --yes bing-wallpaper-daily-mac-multimonitor@latest $REST_ARGS</string>"
else
local COMMANDS="<string>$SCRIPT_PATH $REST_ARGS</string>"
fi
cat > $PLIST_FILE <<- EOM
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bing-wallpaper-daily-mac-multimonitor.plist</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>-c</string>
$COMMANDS
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>StandardErrorPath</key>
<string>/tmp/bing-wallpaper-daily-mac-multimonitor-plist.err</string>
<key>StandardOutPath</key>
<string>/tmp/bing-wallpaper-daily-mac-multimonitor-plist.out</string>
<key>StartInterval</key>
<integer>1800</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOM
launchctl unload -w $PLIST_FILE 2>/dev/null
launchctl load -w $PLIST_FILE
}
remove_plist_in_users_agents_folder() {
launchctl unload -w "$PLIST_FILE" 2>/dev/null
rm "$PLIST_FILE"
}
show_info_text() {
# Parse HPImageArchive API and acquire picture BASE URL
COPYRIGHT=$(cat "$PICTURE_DIR/info.xml" | \
grep -Eo "<copyright>.*<\/copyright>")
COPYRIGHT=$(echo "$COPYRIGHT" | sed -e "s/<copyright>//")
COPYRIGHT=$(echo "$COPYRIGHT" | sed -e "s/<\/copyright>//")
echo $COPYRIGHT
}
print_message() {
if [ ! "$QUIET" ]; then
printf "%s\n" "$(date): ${1}"
fi
}
download_image_curl () {
local RES=$1
FILEURLWITHRES="${FILEURL}_${RES}.jpg"
echo $FILEURLWITHRES
FILENAME=${FILEURLWITHRES/\/th\?id=/}
FILENAME_LOCAL="${AUTO_UPDATE_NAME}-${FILENAME}"
FILEWHOLEURL="$PROTO://bing.com/$FILEURLWITHRES"
if [ $FORCE ] || [ ! -f "$PICTURE_DIR/$FILENAME_LOCAL" ]; then
find $PICTURE_DIR -type f -iname $AUTO_UPDATE_NAME-\*.jpg -delete
print_message "Downloading: $FILENAME..."
curl --fail -Lo "$PICTURE_DIR/$FILENAME_LOCAL" "$FILEWHOLEURL"
curl --fail -Lo "$PICTURE_DIR/info.xml" "$BING_HP_IMAGE_ARCHIVE_URL"
if [ "$?" == "0" ]; then
FILEPATH="$PICTURE_DIR/$FILENAME_LOCAL"
return
fi
FILEPATH=""
return
else
print_message "Skipping download: $FILENAME_LOCAL..."
FILEPATH="$PICTURE_DIR/$FILENAME_LOCAL"
DOWNLOAD_SKIPPED=true
return
fi
}
set_wallpaper () {
local FILEPATH=$1
local MONITOR=$2
if [ "$MONITOR" -ge 1 ] 2>/dev/null; then
print_message "Setting wallpaper for monitor: $MONITOR"
osascript - << EOF
set tlst to {}
tell application "System Events"
set tlst to a reference to every desktop
set picture of item $MONITOR of tlst to "$FILEPATH"
end tell
EOF
else
osascript -e 'tell application "System Events" to tell every desktop to set picture to "'$FILEPATH'"'
fi
}
set_wallpaper_experimental () {
local db_file="Library/Application Support/Dock/desktoppicture.db"
local db_path="$HOME/$db_file"
# Put the image path in the database
local sql="insert into data values(\"$FILEPATH\"); "
sqlite3 "$db_path" "$sql"
# Get the index of the new entry
local sql="select max(rowid) from data;"
local new_entry=$(sqlite3 "$db_path" "$sql")
local new_entry=$(echo $new_entry|tr -d '\n')
# Get all picture ids (monitor/space pairs)
local sql="select rowid from pictures;"
local pictures_string=$(sqlite3 "$db_path" "$sql")
local IFS=$'\n'
local pictures=($pictures_string)
# Clear all existing preferences
local sql="select max(rowid) from data; delete from preferences; "
for pic in "${pictures[@]}"
do
if [ "$pic" ]; then
local sql+="insert into preferences (key, data_id, picture_id) "
local sql+="values(1, $new_entry, $pic); "
fi
done
sqlite3 "$db_path" "$sql"
killall "Dock"
}
# Defaults
PICTURE_DIR="$HOME/Pictures/bing-wallpapers/"
# Option parsing
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
enable-auto-update)
ENABLE_AUTOMATIC_UPDATE=true
;;
disable-auto-update)
DISABLE_AUTOMATIC_UPDATE=true
;;
info)
INFO=true
;;
--auto-update-name)
AUTO_UPDATE_NAME="$2"
shift
;;
-r|--resolution)
RESOLUTION="$2"
shift
;;
-p|--picturedir)
PICTURE_DIR="$2"
shift
;;
-c|--country)
COUNTRY="$2"
shift
;;
-d|--day)
DAY="$2"
shift
;;
-n|--filename)
FILENAME="$2"
shift
;;
-m|--monitor)
MONITOR="$2"
shift
;;
-f|--force)
FORCE=true
;;
-s|--ssl)
SSL=true
;;
-q|--quiet)
QUIET=true
;;
-h|--help)
usage
exit 0
;;
--resolutions)
RESOLUTIONS="$2"
shift
;;
--all-desktops-experimental)
EXPERIMENTAL=true
;;
--version)
printf "%s\n" $VERSION
exit 0
;;
*)
(>&2 printf "Unknown parameter: %s\n" "$1")
usage
exit 1
;;
esac
shift
done
# Set options
[ $QUIET ] && CURL_QUIET='-s'
[ $SSL ] && PROTO='https' || PROTO='http'
[ $DAY ] && IDX=$DAY || IDX='0'
BING_HP_IMAGE_ARCHIVE_URL="https://www.bing.com/HPImageArchive.aspx?format=xml&idx=${IDX}&n=1"
[ $COUNTRY ] && BING_HP_IMAGE_ARCHIVE_URL="${BING_HP_IMAGE_ARCHIVE_URL}&mkt=${COUNTRY}"
PLIST_FILE="${PLIST_FILE}-${AUTO_UPDATE_NAME}.plist"
PARENT_COMMAND=$(ps -o comm= $PPID)
if [[ "$PARENT_COMMAND" == *"npm exec"* ]]; then
RUN_USING_NPX=true
fi
if [ "$ENABLE_AUTOMATIC_UPDATE" ]; then
# enable update
create_plist_in_users_agents_folder
echo "Automatic wallpaper update enabled"
exit 1
fi
if [ "$DISABLE_AUTOMATIC_UPDATE" ]; then
# disable update
remove_plist_in_users_agents_folder
echo "Automatic wallpaper update disabled"
exit 1
fi
if [ "$INFO" ]; then
show_info_text
exit 1
fi
# Create picture directory if it doesn't already exist
mkdir -p "${PICTURE_DIR}"
# Parse HPImageArchive API and acquire picture BASE URL
FILEURL=( $(curl -sL $BING_HP_IMAGE_ARCHIVE_URL | \
grep -Eo "<urlBase>.*?</urlBase>") )
FILEURL=$(echo "$FILEURL" | sed -e "s/<urlBase>//")
FILEURL=$(echo "$FILEURL" | sed -e "s/<\/urlBase>//")
if [ $RESOLUTION ]; then
download_image_curl $RESOLUTION
if [ "$FILEPATH" ]; then
if [ "$EXPERIMENTAL" ]; then
if [ ! "$DOWNLOAD_SKIPPED" ]; then
set_wallpaper_experimental $FILEPATH
fi
else
set_wallpaper $FILEPATH $MONITOR
fi
fi
exit 1
fi
for RESOLUTION in "${RESOLUTIONS[@]}"
do
download_image_curl $RESOLUTION
if [ "$FILEPATH" ]; then
if [ "$EXPERIMENTAL" ]; then
if [ ! "$DOWNLOAD_SKIPPED" ]; then
set_wallpaper_experimental $
fi
else
set_wallpaper $FILEPATH $MONITOR
fi
exit 1
fi
done