Jul 3, 2007

Rainy Day R2

Upon seeing R2 in the rain during "The Empire Strikes Back" my wife asks: "Why doesn't R2 have an umbrella?"

Jun 25, 2007

Automatically update to latest WebKit each night.

You like webkit. You want the latest WebKit installed fresh each night while you sleep. You've looked at NightShift and found that it lacked a certain simplicity. You came here.

Ok. We need a script to do the dirty work. Bash is fine:


#!/bin/bash
# coding: utf-8

# Update Webkit
# Alex Ross

function handle {
 echo "An error occured!"
 exit $?
}

trap handle ERR

APPLICATIONS="/Applications"
ORG='http://nightly.webkit.org'

LOCAL_REVISION=`cat /Applications/WebKit.app/Contents/Resources/VERSION`
LATEST_REVISION_PATH=`curl --silent $ORG | grep -o "/files/trunk/mac/WebKit-SVN-r[0-9]*.dmg" | head -n 1`
LATEST_REVISION=`echo \"$ORG+$LATEST_REVISION_PATH\" | grep -E '[0-9]{4,}' -io`
if [ $LOCAL_REVISION -eq $LATEST_REVISION ]; then
  echo "Local WebKit r$LOCAL_REVISION is latest build, exiting." > /dev/console  
  exit
else
  echo "Updating WebKit.app to r$LATEST_REVISION..." > /dev/console  
fi

curl -sL $ORG$LATEST_REVISION_PATH > /tmp/latest-webkit-svn.dmg
hdiutil attach /tmp/latest-webkit-svn.dmg -mountpoint /tmp/latest-webkit-svn -quiet

osascript <<-APPLESCRIPT
tell application "Finder"
activate
set answer to display dialog "Would you like to update WebKit now? Your open Webkit and Drosera windows will close.

Updating automatically in 30 seconds…" with title "WebKit Updater" buttons {"No", "Yes"} default button {"Yes"} giving up after 30


if button returned of answer is "Yes" or button returned of answer is not "No" then
 tell application "System Events"
  if (name of processes) contains "WebKit" then quit application "WebKit" saving no
  if (name of processes) contains "DroseraLauncher" then quit application "Drosera" saving no
 end tell
end if

end tell

delay 0.25

tell application "System Events"
 if (name of processes) does not contain "WebKit" and (name of processes) does not contain "DroseraLauncher" then
  return 1
 else
  return 0
 end if
end tell
APPLESCRIPT

rm -rf $APPLICATIONS/Drosera.app $APPLICATIONS/WebKit.app

cp -R /tmp/latest-webkit-svn/Drosera.app $APPLICATIONS/Drosera.app
cp -R /tmp/latest-webkit-svn/WebKit.app $APPLICATIONS/WebKit.app

hdiutil detach /tmp/latest-webkit-svn -quiet
rm /tmp/latest-webkit-svn.dmg

echo "Webkit is up-to-date." > /dev/console

Paste all this junk into a text file and save it to /Library/Scripts/update_webkit.sh.

We create a launch agent to run this script each morning at 4am. Open a blank text file and paste in this xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Label</key>
 <string>UpdateWebKit</string>
 <key>UserName</key>
 <string>root</string>
 <key>GroupName</key>
 <string>wheel</string>
 <key>ProgramArguments</key>
 <array>
  <string>/bin/sh</string>
  <string>/Library/Scripts/update_webkit.sh</string>
 </array>
 <key>StartCalendarInterval</key>
 <dict>
  <key>Hour</key>
  <integer>4</integer>
 </dict>
</dict>
</plist>

That's it! You're done. Mmmm. I love the smell of fresh nightlies in the morning.

Jan 22, 2007

Generating HTML Image Maps

Have you ever pulled a handful of hair from your head? If you’ve ever tried to create an HTML Image Map with the GIMP, then I’m sure your sitting in clumps of it. No More! With gen_imgmap.py, you can auto-generate your image maps from a simple PNG image file and a CSV text file!

Here’s how it works:

Create an image that has a unique color for each area you would like to make clickable. Like this:

Now, all you need is a CSV file that maps the color values in your image to the href, name, title, and any other attributes that you would like on your area tags. The first line of the CSV contains labels for the following rows. The first label has to be “color”, it can be either a hexadecimal color code or an integer color code. The remaining columns can be whichever attributes you would like to give to your area tags. Like This (I’m using integer color codes here):

color, id, title, href
46621634, AKZ208, Lower Kobuk and Noatak Valleys, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ208
47610067, AKZ217, Upper Kobuk and Noatak Valleys, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ217
47549398, AKZ205, Northwestern Brooks Range, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ205
49656045, AKZ206, Northeastern Brooks Range, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ206
49847290, AKZ218, Southeastern Brooks Range, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ218
45763761, AKZ210, Northern and Interior Seward Peninsula, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ210
46099384, AKZ216, Lower Koyukuk and Middle Yukon Valleys, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ216
46608056, AKZ219, Upper Koyukuk Valley, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ219
45777072, AKZ220, Yukon Flats and Surrounding Uplands, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ220
45322154, AKZ221, Central Interior, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ221
47940043, AKZ222, Middle Tanana Valley, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ222
47594677, AKZ224, Upper Tanana Valley and the Fortymile Country, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ224
49981415, AKZ215, Lower Yukon Valley, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ215
45428630, AKZ151, Kuskokwim Valley, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ151
48126408, AKZ225, Denali, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ225
49956578, AKZ223, Deltana and Tanana Flats, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ223
47673817, AKZ226, Eastern Alaska Range, http://pafg.arh.noaa.gov/zonefcst.php?zone=AKZ226

If for some reason you need to use comma’s inside one of your attribute strings, you’ll have to double quote all of them.

Now, all that must be done is the running of the program (-i tells gen_imgmap.py to use integer color codes):

./gen_imgmap.py -i alaska.png attributes.csv map.html

This will take a couple of minutes. For simpler color maps, obviously the program will run far more quickly. When it’s finished, you’ll be left with something like this:

You may also notice that a folder called “masks” is created, and that this folder contains a bunch of PNG image masks with column two of the CSV file as their image name. gen_imgmap.py uses these files to figure out where to place the bounding polygons for your area tags. You may also use them to create overlays for each region of your image. Check out this page to see how I have applied this method to a production web page.

To use gen_imgmap.py you’ll need the following software:

  • ImageMagick ≥ 6.3.0
  • Python ≥ 2.4
  • numpy ≥ 1.0
  • PIL ≥ 1.1.6

You can get it here: gen_imgmap.py.

Update:

micheal fixed a couple of problems with the script with a patch below in the comments. I have updated the link to reflect his patch.

Jan 21, 2007

Hi

Hello there. I am Alexander John Ross. This is my blog.

Maybe you are wondering what I am. Well, I'll tell you. I am a mathematician. I've almost got a Bachelor's Degree from the University of Alaska Fairbanks. Math is fun! A lot of people ask me what I am going to do with a degree in mathematics. Well, I'll tell you. I don't know! I think I'll probably just use my lessons in logic to do something fun.

I like to write code. I write mostly in python, but ruby is fun too. I'm a bundle maintainer/developer for TextMate. I write code for the National Weather Service. I do a little bit of consulting on the side. If you want to hire me, you can! I am proficient in HTML, CSS, python, javascript, uhm… PHP, Ruby… You get the idea.

Stay tuned for various cool stuff which will be arriving here shortly!