This AppleScript fetches the Bing image of the day using any choice of a valid market code and sets it as your desktop wallpaper.
The AppleScript may then be added to the Shortcuts.app
in macOS and run from the Menu Bar.
Using the Japanese market code ja-JP
as an example, the direct download link for the Bing image of the day can be found from
http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP
where:
format
can take the valuesjs
(short forjson
),hp
(short forhtml
),xml
(the default) andrss
;uhd
can take the values0
and1
, with0
meaning fetch the standard resolution version of the image (normally a resolution of 1920x1080) and1
meaning fetch the ultra high definition resolution version of the image (which can vary from a minimum resolution of 1920x1080 -- for example, the image obtained in the example below is 5349×3009);idx
is the number days previous to the present day, with0
meaning the present day;n
is the number of images to fetch previous to the day given byidx
, with1
meaning fetch the one image for dayidx
; andmkt
is the market code. There are currently a total of 38 market codes as listed here, and at present (October 2024) only the market codesde-DE
,en-CA
,en-GB
,en-IN
,en-US
,es-ES
,fr-CA
,fr-FR
,it-IT
,ja-JP
,ko-KR
(sometimes),pt-BR
andzh-CN
have their own localised versions. Other market codes are set as being the “Rest of the World” (with the generic market codeROW
).
The AppleScript basically works by first downloading the Bing image of the day and saving it to ~/Downloads/bing_image_of_the_day.jpg
, then setting this image as the desktop wallpaper.
Again using the Japanese market code ja-JP
as an example, the Bing image of the day can be downloaded with the following one-liner CLI command
curl --silent "http://www.bing.com/$(curl --silent "http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP" | grep --only-matching "\"url\":\"\/.*\.jpg" | sed 's/"url":"\///g')" > ~/Downloads/bing_image_of_the_day.jpg
Let's unpack what this one-liner CLI command does ...
The command
curl --silent "http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP"
outputs the information for the current image of the day, like so:
{"images":[{"startdate":"20211120","fullstartdate":"202111201500","enddate":"20211121","url":"/th?id=OHR.ElephantGiving_JA-JP6387498046_UHD.jpg&rf=LaDigue_UHD.jpg&pid=hp&w=1920&h=1080&rs=1&c=4","urlbase":"/th?id=OHR.ElephantGiving_JA-JP6387498046","copyright":"アフリカゾウの家族, ケニア (© Yva Momatiuk and John Eastcott/Minden Pictures)","copyrightlink":"https://www.bing.com/search?q=%E3%82%A2%E3%83%95%E3%83%AA%E3%82%AB%E3%82%BE%E3%82%A6+%E5%AE%B6%E6%97%8F&form=hpcapt&filters=HpDate%3a%2220211120_1500%22","title":"今日は「家族の日」","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20211120_ElephantGiving%22&FORM=HPQUIZ","wp":true,"hsh":"1870702a47d30be6d7aee03f41b36604","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"読み込み中...","previous":"前の画像へ","next":"次の画像へ","walle":"この画像を壁紙としてダウンロードすることはできません。","walls":"この画像をダウンロードできます。画像の用途は壁紙に限定されています。"}}
The next part, namely
grep --only-matching "\"url\":\"\/.*\.jpg"
then pulls out relevant part of the url
location from the previous output, so that the command
curl --silent "http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP" | grep --only-matching "\"url\":\"\/.*\.jpg"
outputs
"url":"/th?id=OHR.ElephantGiving_JA-JP6387498046_UHD.jpg&rf=LaDigue_UHD.jpg
The next part, namely
sed 's/"url":"\///g'
then removes the "url":"/
part of the previous output, so that the command
curl --silent "http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP" | grep --only-matching "\"url\":\"\/.*\.jpg" | sed 's/"url":"\///g'
outputs
th?id=OHR.ElephantGiving_JA-JP6387498046_UHD.jpg&rf=LaDigue_UHD.jpg
We then have the required direct download link, namely
http://www.bing.com/th?id=OHR.ElephantGiving_JA-JP6387498046_UHD.jpg&rf=LaDigue_UHD.jpg
and the last step is to download the image and save it as ~/Downloads/bing_image_of_the_day.jpg
, which is accomplished with the final one-liner CLI command shown above, and the resultant image is illustrated below:
Note that the &rf=LaDigue_UHD.jpg
part of the direct download link is a "fallback" image if the actual image ElephantGiving_JA-JP6387498046_UHD.jpg
is not available. So if the downloaded image is the one illustrated below, something is wrong!
To use this one-liner CLI command in AppleScript, it is necessary to escape some characters (namely the "
's and \
's). The following is an AppleScript that sends this one-liner CLI command to Terminal.app
:
tell application "Terminal"
do script "curl --silent \"http://www.bing.com/$(curl --silent \"http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=ja-JP\" | grep --only-matching \"\\\"url\\\":\\\"\\/.*\\.jpg\" | sed 's/\"url\":\"\\///g')\" > ~/Downloads/bing_image_of_the_day.jpg" in front window
end tell
Let's unpack what this AppleScript does ...
The first part of the AppleScript, namely
set market_code_choices to {"da-DK, Denmark, Danish", "de-AT, Austria, German", "de-CH, Switzerland, German", "de-DE, Germany, German", "en-AU, Australia, English", "en-CA, Canada, English", "en-GB, United Kingdom, English", "en-ID, Indonesia, English", "en-IN, India, English", "en-MY, Malaysia, English", "en-NZ, New Zealand, English", "en-PH, Republic of the Philippines, English", "en-US, United States, English", "en-ZA, South Africa, English", "es-AR, Argentina, Spanish", "es-CL, Chile, Spanish", "es-ES, Spain, Spanish", "es-MX, Mexico, Spanish", "es-US, United States, Spanish", "fi-FI, Finland, Finnish", "fr-BE, Belgium, French", "fr-CA, Canada, French", "fr-CH, Switzerland, French", "fr-FR, France, French", "it-IT, Italy, Italian", "ja-JP, Japan, Japanese", "ko-KR, Korea, Korean", "nl-BE, Belgium, Dutch", "nl-NL, Netherlands, Dutch", "no-NO, Norway, Norwegian", "pl-PL, Poland, Polish", "pt-BR, Brazil, Portuguese", "ru-RU, Russia, Russian", "sv-SE, Sweden, Swedish", "tr-TR, Turkey, Turkish", "zh-CN, Peoples Republic of China, Chinese", "zh-HK, Hong Kong SAR, Traditional Chinese", "zh-TW, Taiwan, Traditional Chinese"}
set market_code to choose from list market_code_choices with prompt "Market code (Code, Country/Region, Language):" default items {"ja-JP, Japan, Japanese"}
if market_code is false then return
opens up a list of valid market codes for selection:
and selecting Cancel
exits the AppleScript.
For the above example, the market code ja-JP
is selected and the variable market_code
is then set to ja-JP, Japan, Japanese
.
The next part of the AppleScript
tell application "System Events"
tell every desktop
set picture to "~/Downloads/no_image.jpg"
end tell
end tell
just removes the current wallpaper image.
The next part of the AppleScript
tell application "Terminal"
do script "curl --silent \"http://www.bing.com/$(curl --silent \"http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=$(echo " & market_code & " | grep --extended-regexp --only-matching \"[a-z]{2}-[A-Z]{2}\")\" | grep --only-matching \"\\\"url\\\":\\\"\\/.*\\.jpg\" | sed 's/\"url\":\"\\///g')\" > ~/Downloads/bing_image_of_the_day.jpg" in front window
end tell
then sends the one-liner CLI command to Terminal.app
, where the (suitably escaped for AppleScript) command
echo " & market_code & " | grep --extended-regexp --only-matching \"[a-z]{2}-[A-Z]{2}\"
just pulls out the market code from the market_code
variable.
For example, the CLI command
echo ja-JP, Japan, Japanese | grep --extended-regexp --only-matching "[a-z]{2}-[A-Z]{2}"
simply outputs
ja-JP
The next part of the AppleScript
tell application "System Events"
tell every desktop
set picture to "~/Downloads/bing_image_of_the_day.jpg"
end tell
end tell
then sets the downloaded image ~/Downloads/bing_image_of_the_day.jpg
as the desktop wallpaper.
The final part of the AppleScript
tell application "Terminal"
do script "curl --silent \"http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=$(echo " & market_code & " | grep --extended-regexp --only-matching \"[a-z]{2}-[A-Z]{2}\")\" | grep --only-matching \"\\\"url\\\":\\\"\\/.*\\.jpg\" | sed 's/\"url\":\"\\/th?id=OHR\\.//g' | sed 's/\\.jpg.*//g'" in front window
end tell
provides a simple check of the file name, market code and image resolution obtained by the one-liner CLI command.
For example, the CLI command
curl --silent "http://www.bing.com/HPImageArchive.aspx?format=js&uhd=1&idx=0&n=1&mkt=$(echo ja-JP, Japan, Japanese | grep --extended-regexp --only-matching "[a-z]{2}-[A-Z]{2}")" | grep --only-matching "\"url\":\"\/.*\.jpg" | sed 's/"url":"\/th?id=OHR\.//g' | sed 's/\.jpg.*//g'
simply outputs
ElephantGiving_JA-JP6387498046_UHD
In the Shortcuts.app
, create a new Shortcut
that performs a Run AppleScript
action with the above AppleScript code
that may then be run from the Menu Bar
Do What The Fuck You Want To Public License.