More Nerdiness

As you may or may not know, in addition to taking fine photos, I enjoy designing and building software. For the last several years, I’ve been working in the cloud, mainly on the Force.Com platform, but before that, I was a Windows developer. Occasionally, I like to build a little software project on my own just to make sure I can still develop desktop applications or to build little tools to make my life easier. I did this much more often when I was a strictly-Windows guy, but since moving to an iMac as my desktop machine for day-to-day use and post-processing, I don’t get to do it as often as I’d like as I haven’t the time to learn Objective-C or Swift. Luckily, to sate my need for Windows-related development, I have a Windows 10 Virtual Machine on my iMac running under Parallels as well as a Dell XPS laptop.

For my latest “me project”, I thought it would be cool if I could have my Windows desktop wallpaper periodically change to one of the photos on 75CentralPhotography.com. To accomplish this, I knew I’d need a couple of pieces of software running in different places:

  1. A webservice or some other means of fetching a random photo from the site
  2. A local application running on Windows that would periodically make a call to the aforementioned webservice and get the random photo

So, for the first part, I build a RESTful API on 75CentralPhotography.Com that looks for photos on the site that I’ve specified as wallpaper candidates and returns one at random. Since our site runs on WordPress, a lot of the functionality was already in place. The process works like this:

  1. A new post custom metadata field called 75Wallpaper was created that can be set to TRUE if that photo should be used as wallpaper.
  2. A new webservice was written in PHP that uses WordPress’ WP_Query method to return a random photo with the above criteria
    $args = array( 'meta_key' => '75wallpaper', 'meta_value'=>'TRUE', 'posts_per_page' => '1', 'orderby' => 'rand' ); $your_query = new WP_Query( $args ); if($your_query->have_posts()){ while ($your_query->have_posts()) : $your_query->the_post(); $imageurl= catch_that_image(); $permlink = get_post_permalink(); endwhile; } 

    We use a handy function I found on the web some time back called “catch_that_image” that grabs the URL of the first image on a post, which ensures we get the link to the image. We also go ahead and grab the permalink to the image.

  3. We then serialize these results into some JSON so that making a call to this service results in a response that looks like this:
    {"randomImage":"http://www.75centralphotography.com/wp-content/uploads/2018/05/tandy-hills-nature-area-sunrise-grass-fort-worth.jpg","permalink":"http://www.75centralphotography.com/?post_type=post&p=36891"} 

Now to build the Windows portion. For this, I chose to build a .Net application in VB.Net, however, I could’ve just as easily used C#.

To start, I created a simple UI with a couple of buttons and a picture box. One button would let the user interactively set a new background by calling the above webservice’s randomImage element and the second would use the permalink element to open the current wallpaper’s page on 75CentralPhotography.com in the default browser. The picturebox would display the current wallpaper and was really just there for testing purposes (i.e. I needed to ensure I was retreiving the image properly before I wrote the code to set as the wallpaper).

I then started writing some code to do the magic.

  1. First, I needed to build a function to call the webservice and retrieve and store the results, including downloading the image to the Windows AppData folder:
Public Function ProcessWallpaper() As Boolean
        Dim wOPs As New WebOps
        Dim AppDataPath As String
        Dim u As New utilities
        Dim doesAppDataPathExists As Boolean
        Dim rP As Bitmap
        Dim setSuccess As Boolean
        Dim randomfilename As String = u.createRandomFilename()
        rP = tempPB(GetRandomImageURL()) ' fill form picturebox for testing
        AppDataPath = u.getAppDataPath()
        doesAppDataPathExists = u.checkfolderexists(AppDataPath)
        If doesAppDataPathExists Then
            rP.Save(AppDataPath & "\75Central\" & randomfilename, Imaging.ImageFormat.Jpeg)
        End If
        setSuccess = u.setWallPaper(AppDataPath & "\75Central\" & randomfilename)
        wait(5)
        ClearAppData(AppDataPath)
        Return True
    End Function

This code is called in three places:

  1. When the application loads, immediately setting a new wallpaper
  2. When the “Set New Wallpaper Now” button is clicked
  3. Every ten minutes after the application starts running, ensuring that a new wallpaper is set on a regular schedule

2. Secondly, I needed some code to actually set the wallpaper. Luckily, .Net already has a function to do this, so I just needed to call it and pass the path of the image downloaded:

Public Function setWallPaper(imagefile As String) As Boolean
        SystemParametersInfo(SETDESKWALLPAPER, 0, imagefile, UPDATEINIFILE)
        Return True
    End Function

3. Finally, I needed to cleanup the download. Once a Windows 10 wallpaper is set, it’s stored internally in the operating system and no longer needs to persist as a separate file, so there’s no reason to have a high-res version of my photo hanging around on my PC, taking up space:

Private Sub ClearAppData(apppath As String)
        Dim directoryName As String = apppath & "\75Central\"
        For Each deleteFile In Directory.GetFiles(directoryName, "*.jpg", SearchOption.TopDirectoryOnly)
            File.Delete(deleteFile)
        Next
    End Sub

All of this code is tied together to work, and upon launch, the app nicely minimizes into the system tray out of the way until the icon is double clicked to show the UI

So, there you have it, my latest nerdy project. If there’s sufficient interest, I might release this as a free download so that you can have automated new fun wallpapers. If you’re interested, shoot me an email at [email protected]!

  • February 8, 2019