The Code
Perl is a great language for making HTTP requests and working with the responses. But it's not as good as AppleScript at performing Mac system tasks, such as setting a desktop image. Luckily, a Perl module called Mac::AppleScript lets you execute AppleScript code from within your Perl scripts. To install this module, open a Terminal window and type the following command:
sudo perl -MCPAN -e 'install Mac::AppleScript'
Before you begin, you'll need to install a couple of other Perl modules. As with most of the Perl examples in this book, you'll need LWP::Simple and XML::Simple to make Yahoo! requests and parse responses.
Once these prerequisites are installed, save the following code to a file named ybackground.pl:
#!/usr/bin/perl
# ybackground.pl
# Accepts a query term and sets a Mac desktop background
# with that theme.
# Usage: ybackground.pl <query>
#
# You can read the full documentation
# for Yahoo! Web Services at http://developer.yahoo.net/
use strict;
use LWP::Simple;
use XML::Simple;
use Mac::AppleScript qw(RunAppleScript);
# Please leave this Yahoo! Application ID
my $appID = "ybackground-mac";
# Grab the incoming search query
my $query = join(' ', @ARGV) or die "Usage: ybackground.pl <query>\n";
# Construct a Yahoo! Search Query with only required options
my $language = "en";
my $req_url = "http://api.search.yahoo.com/";
$req_url .= "ImageSearchService/V1/imageSearch?";
$req_url .= "appid=$appID";
$req_url .= "&query=$query";
$req_url .= "&results=50";
$req_url .= "&adult_ok=0";
# Make the request
my $yahoo_response = get($req_url);
# Parse the XML
my $xmlsimple = XML::Simple->new();
my $yahoo_xml = $xmlsimple->XMLin($yahoo_response);
# Grab a random image URL from the results
my $rnd = int(rand(@{$yahoo_xml->{Result}}));
my $url = $yahoo_xml->{Result}->[$rnd]->{Url};
# Save the image locally
my $time = time;
my $image = get($url);
open IMAGE,">$query-$time.jpg";
print IMAGE $image;
close IMAGE;
# Set the image as the current
# desktop background
RunAppleScript(qq(
tell application "Finder"
set desktop picture to document file "$query-$time.jpg"
end tell));
This code creates a unique image file each time the script is run, using the time the file was downloaded as part of the title. The last four lines of the script are where the AppleScript runs, via the RunAppleScript function, setting the desktop background to the newly downloaded file.
If you know the specific size of an image you'd like to download, you can modify the script to look for that size. For example, if your desktop size is set to 1024 x 768, a smaller image that's stretched might not look as good as an image that is 1024 x 768 without resizing.
To find only images that are a certain size you can use the height: and width: meta keywords when you construct the Yahoo! URL. If you only want images that are 1024 pixels wide, change the line in the script where you set the query, like so:
$req_url .= "&query=$query width:1024";
With the width set appropriately, you'll be sure to get only images that fit the width of your desktop.