Download APOD and set it as the wallpaper batch script

May 14, 2009 14:25 by scibuff

Download my APOD Batch script.

If you never heard of APOD you’ve missed a lot in the last 14 years. The website, supported by NASA and Michigan Technological University, has published an image from the universe with a commentary by a professional astronomer every day since June 16, 1995.

I wanted an application which could download the APOD on daily basis and set it as my desktop wallpaper. There are a few tools out there that come close to the desired functionality. Most of them are free, but they all require admin/root rights on the machine and some don’t even seem to be 100% spyware-free. Thus, I’ve decided to create a simple batch script which would accomplish this task for me.

You may ask: Why bother creating an app if there already are similar ones out there? Well, usually I would say “don’t”. But, in this particular case, there actually wasn’t application which would work for me and/or had the functionality I was looking for. Besides, when it comes to programming, I like to do things myself. First, you always learn something new. Second, you can never be 100% sure that an executable does what it says it does. Sure, you can run tools such as TcpView and Process Explorer from SysInternals and see for yourself if anything wacky is happening behind the scenes, but by then you really are getting in too deep. I wanted just to download an image from the net and set it as a wallpaper for cryin’ out loud.

I like to have all my apps and code properly structured, so as the first step I created a new directory for my app (it doesn’t matter where). Inside, I created two directories: “bin” and “images” (I’ll come back to those a bit later), and my “apod.bat” batch file. I opened the batch in my favorite text editor (notepad) and typed @ECHO OFF at the first line and @ECHO ON at the end. Right after the first line I inserted the following:

REM create the image directory if it doesn't exist
IF NOT EXIST "images" mkdir "images"

REM is a “command” equivalent to the comment tag // in most modern languages. The line after that checks whether the directory called “images” exists and if not it creates it.

Now that I’m happy with the initial setup, I can proceed to the “real stuff”. Immediately from the beginning I knew there were four basic steps to the script:

1.) Read/download the most recent APOD HTML page
2.) Search through the APOD HTML page and find the image source path
3.) Download the APOD image
4.) Set the APOD image as the desktop wallpaper.

In linux, 1.), 2.) and 3.) are very easy – all you need is to pass the right arguments to the wget and grep commands.

wget is a simple yet powerful tool. I’m quite familiar with from the Linux environment so not even for a second did I look for a replacement on Windows, and went directly to GNU.org which provides most of the GNU core utilities for shell and text manipulation as Windows binary. The wget win32 binaries can be downloaded from the wget page. I will need the Binaries AND the Dependencies. After the packages were downloaded I unpacked them and place all binaries (*.exe and *.dll) into the “bin” folder created earlier. Then I repeat the same steps for the grep tool.

wget will take care of steps 1.) and 3.) and I’ll use grep for 2.). Now I just need a tool for 4.). This one takes a bit of looking around. It can be achieved directly with registry modification but then again it could run into the admin/root rights problems. I tried a few alternatives and the wallpaper changer from sg20.com had the best performance and 100% reliability. It does require .NET 2.0 framework but any up-to-date windows system has it already installed (as either a built-in or thru windows update) these days – in case you don’t, it can be downloaded from the msdn website. Once again, I downloaded the zip package and put the executable into the “bin” folder.

With all necessary tools ready in the “bin” folder I dove into the batch script itself. On the next line after the last edit, I added the following

REM download todays APOD html
bin\wget -N http://apod.nasa.gov/apod

The script above basically grabs the index.html from the APOD’s site (the -N flag ensures that the file is retrieved only if it is newer than the local copy). The index.html will contain the latest APOD image. The grep tool will search the html file using

REM get the big image url
bin\grep -o "image.*big.*jpg" index.html > images.txt

Most images on the APOD page are actually links to their largers sizes. The large images’ file names are usually of the format of <ImageName>_big.jpg. The line of code above searches the html source for image(s) names following this pattern. In general, grep outputs an entire line when a match was found. To restrict the output to the search pattern only I used the -o flag. Matches links are outputted into a temporary image.txt for later user. However, from time to time the big image name will not follow the above pattern (for example here). The few lines below will take care of that:

REM check if any images were found
COPY>nul images.txt nul |FIND "1 file(s) copied"

The code above investigates if any (big) images have been found in the APOD html by checking if the size of our images.txt is greater than 0.

REM if image(s) was/were found
IF NOT errorlevel=1 GOTO found

If the size is greater than 0, the script continues to the found label (oh boy, I’d flank my CS class if my teacher saw me using labels …)

REM images.txt has 0 size
IF errorlevel=1 GOTO not-found

If no images have been found, let’s just look for regular image names in the html files, i.e. not just the big ones.

REM get any type of image from the apod page
:not-found
bin\grep -o "image.*jpg" index.html > images.txt
goto found

After these few lines of code, we will have the APOD image filename(s) safely stored in the images.txt and we only need to download them.

I could just used the -i flag with wget to set images.txt as the input file. wget would simply run on every line of the file. Nevertheless, the line of code with grep looked only for image names, not full paths, so we need to add the path to the image directory. I used a simple FOR loop like so:

:found
REM download image(s)
FOR /F "tokens=*" %%G IN (images.txt) DO ( bin\wget -N "http://apod.nasa.gov/apod/%%G" )

Finally, the APOD image is downloaded, so I move it to the “images” folder as set it as the wallpaper

REM move image(s) to the image folder
FOR /F "tokens=*" %%G IN ('DIR /B *.jpg') DO (
MOVE "%%G" "images\%%G"
bin\WallpaperChanger.exe %~dp0images\%%G 1
)

DIR /B *.jpg lists all jpg’s in the current directory using the “bare” format (no heading information or summary).

The MOVE "%%G" "images\%%G" takes every match jpg’s and moves it into the “images” folder.

Finally bin\WallpaperChanger.exe sets the image as the desktop wallpaper. One important detail here is to use full path to the image. I can get the directory of the batch script using %~dp0 and adding images\%%G after that will do. I also specify 1 as the second parameter which stands for “Tiled” (1 is for Centered and 2 for Stretched) that sets the wallpaper settings.

As the very last step, I delete the temporary files the script used

DEL images.txt
DEL index.html

and the batch script is done. Here it is in its full:

@ECHO OFF

REM create the image directory if it doesn't exist
IF NOT EXIST "images" mkdir "images"

REM download todays APOD html
bin\wget -N http://apod.nasa.gov/apod

REM get the big image url
bin\grep -o "image.*big.*jpg" index.html > images.txt

REM check if any images were found
COPY>nul images.txt nul |FIND "1 file(s) copied"

REM download found images
IF not errorlevel=1 GOTO found

REM images.txt has 0 size
IF errorlevel=1 GOTO not-found

REM get any type of image from the apod page
:not-found
bin\grep -o "image.*jpg" index.html > images.txt
goto found

:found

REM download image(s)
FOR /F "tokens=*" %%G IN (images.txt) DO (
bin\wget -N "http://apod.nasa.gov/apod/%%G"
REM skip all other images
GOTO downloaded
)
:downloaded

REM move image(s) to the image folder
FOR /F "tokens=*" %%G IN ('DIR /B *.jpg') DO (
MOVE "%%G" "images\%%G"
bin\WallpaperChanger.exe %~dp0images\%%G 1
REM skip all other images
goto end
)
goto end

:end
DEL images.txt
DEL index.html

@ECHO ON

You can download the APOD batch (including the batch script, wget, grep and the WallpaperChanger tools) here.

Below is a list of some APOD applications for different platforms:

Windows

- Pic APOD

OS X

- Automatic APOD Desktop

Linux

- APOD fetch shell script

Be Sociable, Share!

Comments

4

Reader01

Great post! Just wanted to let you know you have a new subscriber- me!

Set APOD and AAPOD as your Desktop Pictures

[...] time ago I wrote a little batch script that downloads the most recent Astronomy Picture Of Day (APOD) and sets it as your desktop [...]

Madonius

I think i discovered a bug in the Shell script.
In line 24 it should be:
if [ "$test2" != "0" ]

Matt

I found your article through a Google search. Great stuff and very thorough! I had a problem at this step:

bin\WallpaperChanger.exe %~dp0images\%%G 1

kept getting an error:

Problem with command line arguments, please check the arguments”

so I hardcoded the images directory into the line instead and that seems to work fine.

Thanks very much for your article!

Leave your comment:
XHTML:You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . * required