Autoupdate the desktop background from an online photo webcam

desktopwebcam

I would like to set my desktop background to a webcam picture that updates several times an hour, for instance to this view of Tegernsee. I am looking for a way to automatically get that picture every couple of minutes from the internet and update the desktop background. How can I do that (I would prefer a bash or applescript solution)?

Best Answer

I have found a way to make this work with launchd. This requires two steps: First creating a script that downloads the webcam picture, and second creating a LaunchAgent plist file that will call the script every so often.

This method will update the background picture of a specific Space, but only while you are on that space (also, when logging in and maybe when waking from sleep).

First step: Creating a script that downloads the picture and changes the desktop background

  1. Create a folder, e.g. ~/Library/Desktop Pictures/Webcam.
  2. Select that folder to be your desktop backgrounds folder in System Preferences → Destkop & Screen Saver.
  3. Write a text file with the following content:

    #!/bin/bash
    shopt -s extglob # required for fancy rm
    # This script updates the desktop background of a specific Space to a webcam picture
    # while you are currently on that space.
    #
    # Choose a webcam:
    
    remotepic=http://www.foto-webcam.org/webcam/wallberg/current/full.jpg
    
    # Choose a desktop backgrounds pictures folder of the Space where you want to see the webcam
    # (you have to set this manually in System Preferences → Destkop & Screen Saver):
    
    webcampicturefolder=~/Library/Desktop\ Pictures/Webcam
    
    # check the desktop background pictures folder of the current Space
    currentpicturefolder=`osascript -e "tell application \"System Events\" to get pictures folder of current desktop"`
    # only proceed if you are currently on the space where you want to see the webcam
    if [ "$currentpicturefolder" == "$webcampicturefolder" ] ; then
      localpic="$webcampicturefolder"/$(date +%Y%m%dT%H%M%S).jpg
      backup="$webcampicturefolder"/backup.jpg
      #remove all but the previous backup file and txt files:
      rm "$webcampicturefolder"/!(*txt|`basename "$backup"`) 2>>/dev/null
      # get the new picture unless there is a connection failure (-f flag):
      curl -fs -o "$localpic" "$remotepic"
      # make a backup of the new picture:
      cp "$localpic" "$backup" 2>>/dev/null
      # if no new picture has been downloaded, copy from backup:
      cp "$backup" "$localpic"
      # see http://www.macosxautomation.com/applescript/features/system-prefs.html
      osascript -e "
      tell application \"System Events\"
        tell current desktop
          set picture rotation to 0
          set picture to POSIX file \"$localpic\"
        end tell
      end tell
      "
    fi
    
  4. Save the file, e.g. as ~/Library/Desktop Pictures/Webcam/getwebcam.sh

  5. Make it executable by issuing the following command in the Terminal:

    chmod u+x ~/Library/Desktop Pictures/Webcam/getwebcam.sh
    

Second step: Creating a LaunchAgent plist that calls the script

  1. Write a text file with the following content. You need to adapt the string /Users/myusername/Library/Desktop Pictures/Webcam/getwebcam.sh so it points to the script created in the first step. You cannot use a relative path with ~. Change the dict elements with the "Minute" key as you see fit – you can add more of them. The integer determines at which minute of every hour the script will be called (see also man launchd.plist on your Terminal).

    <?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>me.myname.update-desktop-from-webcam</string>
      <key>ProgramArguments</key>
      <array>
        <string>/Users/myusername/Library/Desktop Pictures/Webcam/getwebcam.sh</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>StartCalendarInterval</key>
      <array>
        <dict>
          <key>Minute</key>
          <integer>01</integer>
        </dict>
        <dict>
          <key>Minute</key>
          <integer>16</integer>
        </dict>
        <dict>
          <key>Minute</key>
          <integer>31</integer>
        </dict>
        <dict>
          <key>Minute</key>
          <integer>46</integer>
        </dict>
      </array>
    </dict>
    </plist>
    
  2. Save the text file to ~/Library/LaunchAgents/me.myname.update-desktop-from-webcam.plist – the name should match the "Label" key in the file.

  3. Load it by issuing the following command:

    launchctl load ~/Library/LaunchAgents/me.myname.update-desktop-from-webcam.plist