Applescript, Desktop Wallpaper and Growl. Automagically.

I´ve been a Mac and OS X user now for about 7 months, but to be honest I have not been very experimental in my usage of my Macbook Air. Until now, my main focus has been basic usage of the hardware and operating systems, and making it work in my corporate environment. As I feel that I´ve reached mission accomplished status on that initial project, I decided to use some of my “free” vacation time to play around a bit and experiment a bit more.

For some reason, I decided to start to play around a bit with Applescript and try to create something that changes my desktop wallpaper based on a “Home” and a “Work” scenario. Initially I made a quick script, my first ever Applescript, that presented a dialog box asking me if “Home” or “Work” was the current status, and then changed the desktop wallpaper accordingly. I then assigned that to a hotkey, so I could quickly change between the two modes.

That solution left me with two things; One, it worked! and Two, it could be done better by adding some automation to it.

Here is the solution I ended up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Populate ipaddr variable with IP address
set ipaddr to do shell script "ifconfig | awk '/broadcast/ {print $2}' | tail -1"

# Check if connection is home connection, based on IP subnet

# If not connected, set ipaddr variable to 0.0.0.0 to avoid empty variable issues
# Dirty hack to add padding to zero values, but hey, it works for me.

if (ipaddr is "") then
set ipaddr to "000.000.0.0"
end if

set ipaddrtxt to (characters 1 thru 9 of ipaddr) as string
if ipaddrtxt is "192.168.5" then # Change this to your own subnet
set situation to "Home"
else
set situation to "Work"
end if

# Get current desktop image path
tell application "System Events"
tell current desktop
set currentDesktop to (get picture as text)
end tell
end tell

# Build the desktop image path
set desktopPath to "Macintosh HD:Users::Dropbox:switcher:" & situation & ".png" # Change this to your own paths

# Only tell Finder to change the desktop and notify Growl if a change is required (eg. desktop path not equal to current path)
if currentDesktop is not desktopPath then

# Tell Finder to change the desktop
tell application "Finder"
set desktop picture to {desktopPath} as alias
end tell

tell application "GrowlHelperApp"
set the allNotificationsList to {"Wallpaper Change"}
set the enabledNotificationsList to {"Wallpaper Change"}
register as application "Wallpaper Change" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
-- Send a Notification...
notify with name "Wallpaper Change" title "Wallpaper Change" description "Wallpaper changed to " & (situation) application name "Wallpaper Change"
end tell
end if

I know my theme isn´t very suited for code pastes, so the whole applescript source code is also available for download.

After the script was finished, I added it to my Geektools bash script, that refreshes at a fairly frequent interval:

1
2
# Run Background Switcher Applescript
osascript /Applescript/Switcher.scpt

Sure, I could have used a crontab instead, but for me the easiest way to get this working quickly was to add it to the existing setup I had in place.

Demo time!

The Applescript detects if the computers IP address is on a given subnet, and if so it sets the wallpaper specified for that subnet, dubbed “Home”. If it determines that it is not on that particular subnet, it´s officially at “Work” and sets the wallpaper accordingly. That´s all the logic there is too it, and I´m sure that can be improved either by me or more likely someone who has been hit by the Applescript clue bat.

My desktop Wallpaper images are stored in a given folder in my Dropbox, with one of them called Home and the other called Work. This limits the script to those two given file names, and switches between the two accordingly. It also assumes the the files are on the PNG format. An added bonus by storing the files in Dropbox is that I can remotely change out the image, and keep the wallpapers synchronized between several computes if I wish to do so.

Note that I am under no circumstances a developer and that this is my first Applescript. I am sure there are much better ways to accomplish the same things, with better error handling and overall better code quality, but the fact is that this works for me and I´m happy with it.

10 minute lightning Tech Talk at VMworld?

I love it when a plan comes together, especially when someone gets an idea and then someone fires on all cylinders and executes!

As mentioned before, there has been a lot of talk about various ways to create a “soapbox” for all those who submitted VMworld sessions and got rejected, or those who did not submit but would still like to present.

Thankfully the guys behind vBrownBags has teamed up with VMware communities and are now offering this opportunity at VMworld 2012. I am not sure if this also extends to VMworld Europe, but it is happing in the US event at least.

To read all the details, including sign up details check TechTalks powered by vBrownBags – Coming to VMworld

vSphere 5: Creating a Hardware Version 4 VM

I recently had a need to create a VM for usage on ESX 3.x, but the only thing available to me in my lab was vSphere 5, which naturally creates new VMs with the latest and greatest version 8 virtual hardware.

The following table lists the virtual hardware versions and the corresponding ESX and ESXi releases:

Version Virtual Hardware Version
ESXi 5.x 8
ESX/ESXi 4.x 7
ESX 3.x 4
ESX 2.x 3

Naturally, an ESXi 5.x host can run VMs created with older hardware versions but it is not possible (through the vSphere Client) to create a new VM with an old hardware version, and I needed to create a v4 VM for later export to an ESX 3.x host.

Of course, I could have created the VM on the ESX host that it was supposed to run on on the first place, but the host in question was unavailable at the time and the plam was to prepare the VM off site before deployment in the production environment.

Since I did not have a valid license for ESX 3.5 any more, the easiest way I found to create a hardware version 4 VM, and configure it on vSphere 5, was to use Easy VMX to create the basic VM (vmx file) instead of hacking away at creating it manually.

Easy VMX created the .VMX file that I needed to register a hardware version 4 VM to a ESXi 5.x host, and edit it through the vCenter client. Of course, it creates disk files that are incompatible with ESX but that´s of no concern as you can just remove the invalid devices and add new ones through the vCenter client.

All in all this little “trick” worked without a hitch, and the small Linux machine I had set up for the client worked perfectly when deployed on the old ESX 3.5 install.

Of course, the best option would be to reinstall the old ESX 3.5 host with ESXi 5, but as this was a system due to be decommissioned very soon that was not a real option for the client. The main purpose of the VM I created was to act as an alerting system for incoming email, warning that the email addresses had changed to a new domain, and emails sent to the old one would not be read or acted upon.