Tải bản đầy đủ - 0 (trang)
Hack 84. Randomize Your Mac Desktop Background

Hack 84. Randomize Your Mac Desktop Background

Tải bản đầy đủ - 0trang

Beforeyoubegin,you'llneedtoinstallacoupleofotherPerl

modules.AswithmostofthePerlexamplesinthisbook,you'll

needLWP::SimpleandXML::SimpletomakeYahoo!requestsand

parseresponses.

Oncetheseprerequisitesareinstalled,savethefollowingcode

toafilenamedybackground.pl:



















#!/usr/bin/perl

#ybackground.pl

#AcceptsaquerytermandsetsaMacdesktopbackgroun

#withthattheme.

#Usage:ybackground.pl

#

#Youcanreadthefulldocumentation

#forYahoo!WebServicesathttp://developer.yahoo.net













usestrict;

useLWP::Simple;

useXML::Simple;

useMac::AppleScriptqw(RunAppleScript);









#PleaseleavethisYahoo!ApplicationID

my$appID="ybackground-mac";









#Grabtheincomingsearchquery

my$query=join('',@ARGV)ordie"Usage:ybackground



















#ConstructaYahoo!SearchQuerywithonlyrequiredop

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";



#Maketherequest



my$yahoo_response=get($req_url);









#ParsetheXML

my$xmlsimple=XML::Simple->new();

my$yahoo_xml=$xmlsimple->XMLin($yahoo_response);











#GrabarandomimageURLfromtheresults

my$rnd=int(rand(@{$yahoo_xml->{Result}}));

my$url=$yahoo_xml->{Result}->[$rnd]->{Url};

















#Savetheimagelocally

my$time=time;

my$image=get($url);

openIMAGE,">$query-$time.jpg";

printIMAGE$image;

closeIMAGE;









#Settheimageasthecurrent

#desktopbackground





RunAppleScript(qq(



tellapplication"Finder"

setdesktoppicturetodocumentfile"$query-$time.jpg"

endtell));



Thiscodecreatesauniqueimagefileeachtimethescriptis

run,usingthetimethefilewasdownloadedaspartofthetitle.

ThelastfourlinesofthescriptarewheretheAppleScriptruns,

viatheRunAppleScriptfunction,settingthedesktopbackgroundto

thenewlydownloadedfile.

Ifyouknowthespecificsizeofanimageyou'dliketo



download,youcanmodifythescripttolookforthatsize.For

example,ifyourdesktopsizeissetto1024x768,asmaller

imagethat'sstretchedmightnotlookasgoodasanimagethat

is1024x768withoutresizing.

Tofindonlyimagesthatareacertainsizeyoucanusethe

height:andwidth:metakeywordswhenyouconstructtheYahoo!

URL.Ifyouonlywantimagesthatare1024pixelswide,change

thelineinthescriptwhereyousetthequery,likeso:





$req_url.="&query=$querywidth:1024";



Withthewidthsetappropriately,you'llbesuretogetonly

imagesthatfitthewidthofyourdesktop.



5.9.2.RunningtheHack

Youcanrunthescriptfromthecommandline,addingadesktop

theme,likethis:





perlybackground.plinserttheme



Runningthescriptwiththelightningsearchtermsetsyour

backgroundtosomethingthatlookslikeFigure5-17.



Figure5-17.ArandomMacdesktopbackground

viaYahoo!



Whiletheideaofleavingyourdesktopbackgroundtochance

mightseemfrightening,youmightbesurprisedatthequalityof

someoftheimagesYahoo!hasfoundacrosstheWeb.







Hack85.MashUpImagesfromAroundtheWeb



TheYahoo!ImageAPIprovidesaccesstothousandsof

imagesacrosstheWeb,anddevelopersarerecombining

theseimagesintonewapplicationsthatletyouplay

along.

Inthemusicworld,amash-upcombinestwoormoreexisting

songsintosomethingentirelynew.Amash-upmighthavea

songfromtheBeatlesinthebackgroundandvocalsfromSnoop

Doggintheforeground,whileblendinginaSesameStreetsong

atthesametime.TheYahoo!ImageSearchwebserviceoffers

programmaticaccesstothousandsofimages,andsome

developersareusingthisservicetocreatetheirownvisual

mash-upscombiningdisparateimagesintogames,interesting

visualizations,orrandomworksofart.Hereareafew

interactiveexamplesthatwillgiveyouatasteofwhat'sbeing

mashedtogetherwiththeYahoo!ImageSearchAPI.



5.10.1.Yahoo!BuzzDemo

TheYahoo!BuzzDemoisanalternativewaytoreadthenews.

Whenyoubrowsetohttp://buzz.progphp.com,you'llfindan

oval-shapedcollagemadeupofover100differentimages,each

representingacurrenttopnewsstory.Asyoumoveyour

pointerovereachimageinthecollage,theimagepopstothe

foregroundsothatyoucanseeitclearly,andasummaryofthe

newsstorytheimagerepresentsisdisplayedinthemiddleof

thepage,asshowninFigure5-18.

Clickinganimagetakesyoudirectlytothatimage'soriginal

locationontheWeb,andclickingthecenternewsstorytakes



youtothefullstoryatYahoo!News.Inadditiontotopstories,

youcanbrowseimagesbasedonothernewscategories,oruse

yourownterm.

RasmusLerdorf,aYahoo!employee,builttheYahoo!Buzz

Demotoshowwhat'spossiblewiththeYahoo!ImageSearch

webservice.ThedemousesPHPtopullinnewsstoriesfrom

Yahoo!NewsRSSandtheYahoo!NewsAPI,imagesfrom

aroundtheWebviatheYahoo!ImageAPI,andsomecraftyCSS

topulleverythingtogether.Whileitwon'treplacethefrontpage

ofthepaperanytimesoon,theBuzzDemoisafunwaytofind

stories.Ifyou'dliketotakealookbehindthescenes,youcan

viewthecompletesourcecodeat

http://buzz.progphp.com/buzz.phps.



5.10.2.SearchCollageGenerator

SiddharthUppalputtogetheraround100linesofPerlthat

grabsupto50imagesforanygivenqueryandassemblesthem

intoasinglecollage.HeusedtheexistingYahoo::Searchmodule

(availableathttp://search.cpan.org/~jfriedl/Yahoo-Search1.4.7/lib/Yahoo/Search.pm)toassembleYahoo!Image



Figure5-18.Avisuallookattopnewsstories



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 84. Randomize Your Mac Desktop Background

Tải bản đầy đủ ngay(0 tr)

×