Terminal – How to Automatically Fetch and Update Desktop Images

desktopterminal

I'm an astronomer, and I constantly (read: several times per day) check the same website to view the clear sky chart for my observatory. Specifically, the chart found on this webpage:

http://www.cleardarksky.com/c/YerkesObILkey.html

It would be incredibly handy if I were able to automatically set this image to be my laptop's desktop. I know I could set it manually each morning, but that is a hassle (and isn't as nerdy).

Unfortunately, I am not as versed in C and the command line as I perhaps should be. Is there any way to alter my bash profile to automatically download an image from a website and then set that image as my desktop, say, at midnight every 2 days? Barring obvious issues of image resolution and internet connection. A task like this is well out of my skill-set.

Even if this isn't possible/practical, I would be interested to know why not, just as a matter of curiosity.

If it matters, I'm running 10.13.1 High Sierra on a MacBook Pro.

Best Answer

You have a couple of options to tackle this: AppleScript and a Bash (Terminal) script. Since this needs to happen once per day (meaning scheduled) and you want to guarantee that the file gets downloaded the moment you're logged in so it can be there when you start and then once again every night at midnight, I would do this in Bash.

Create the Script

The script is going to do two things:

  • Download the image
  • Set as the background image

The script is fairly simple. It takes two arguments - URL and the location to save the file. It will then download it to that folder location:

#!/bin/bash
#Bash Script to Download Forecast and Set as desktop background

#User Defined VARs below:

#URL to fetch image 
url="http://www.cleardarksky.com/c/YerkesObILcsk.gif?c=2239204"

#Picture file Location
picFile="/Users/Thomas/Pictures/test.gif"


#Command to retrieve picture and save to defined location
curl -o "${picFile}" "$url"

exit

Save it in a convenient location; give it an obvious name like getSkyForecast.sh. Make sure to make the script executable - chmod +x getSkyForecast.sh If you don't do this, the script won't run.

Schedule the Script

The following is a bit more complex, because it will run upon login and every 24 hours.

Next, the script needs to be kicked off with launchd. To do this, you need a plist (XML file that describes the job)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.user.getSkyForecast</string>
  <key>ProgramArguments</key>
  <array>
      <string>/Users/USERNAME/Path_to_Scripts/getSkyForecast.sh</string>
  <key>RunAtLoad</key> 
  <true/>
  </array>
  <key>StartCalendarInterval</key>
  <array>
  <dict>
    <key>Hour</key>
    <integer>00</integer>
    <key>Minute</key>
    <integer>00</integer>
  </dict>
</dict>
</plist>

Save this file as com.user.SkyForecast and copy it to the ~/Library/LaunchAgents directory.

Load it with the command:

launchctl load com.user.SkyForecast

Set the background image

The easiest thing to do is just go into System Preferences and set the Background image to this file (run the script manually first so it creates the file). When the script runs, it will change the file, not the name so the desktop image will change automatically.