Diary SiteMap RecentChanges About Contact Calendar

Search:

Matching Pages:

Journal

2009-09-23 Astronomy Picture of the Day for Mac OSX Desktop Background

Based on Harold Bakker's APOD script, I offer the following solution. I don’t keep my computer running, so I just install the following Apple Script as a login item (→ System Preferences → Accounts → Login Items):

(* Place your with timeout statement within a try... on error statement to prevent the script from 
stopping when a timeout occurs. *)
try
	-- Give the script a three minute timeout to prevent problems when this is run as a login item
	with timeout of 180 seconds
		do shell script "~/bin/apod.pl >> /var/tmp/console.log" -- append the result to the console log
	end timeout
on error errMsg -- display a dialog only if an error occurs
	display dialog errMsg giving up after 10
end try

You should probably create a new script using the Script Editor on your system, paste the above, and save it as an application. Remember to untick the startup screen checkbox.

You’ll notice that it runs a Perl script called /bin/apod.pl – you should create that directory, put the following Perl script in it, and make it executable. I keep both apod.app and apod.pl in the same /bin directory.

#!/usr/bin/perl

# This script will download the astronomy picture of the day and set
# it as the current desktop background.

# originally by Harold Bakker, harold@haroldbakker.com
# http://www.haroldbakker.com/

# changes by Alex Schroeder <alex@gnu.org>
# http://emacswiki.org/alex/

use strict;
use LWP::UserAgent;
use File::Temp qw/tempfile/;

my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://antwrp.gsfc.nasa.gov/apod/astropix.html");
if ($response->is_success
   and $response->content =~ /href\="image\/([^\/]+)\/(.*?)"/) {
  my $url = "http://antwrp.gsfc.nasa.gov/apod/image/$1/$2";
  my $filename = $2;
  $response = $ua->get($url);
  if ($response->is_success) {
    my ($fh, $tempfile) = tempfile(UNLINK=>0);
    print $fh $response->content;
    close $fh;
    open(F, "|/usr/bin/osascript") or die "Cannot run Apple Script: $!";
    print F <<END;
tell application "Finder"
	set pFile to POSIX file "$tempfile" as string
	set desktop picture to file pFile
	end tell
END
  } else {
    die $response->status_line;
  }
} else {
  die $response->status_line;
}

This should work fine as long you restart your computer about once a day. I haven’t made sure that it will try to reuse images, saving the last one in a save place, etc.

Tags: RSS RSS RSS RSS

Add Comment