GeekTool, text encodings, and iconv

Update 12/16/09
Reader Martin Ström emailed me a link to this site, whose author has patched GeekTool 2.1.2 to display UTF-8 characters correctly. I haven’t tested it myself, but Martin says it works for him, and on matters of international character sets I tend to trust someone with an ö in his name.

One thing that’s always bothered me about my GeekTool setup for displaying the current iTunes track is that it doesn’t handle accented characters properly.

The AppleScript that generates the text is emitting UTF-8, and apparently GeekTool can’t handle it. (Note: this is GeekTool 2. GeekTool 3 may work fine with UTF-8, but I’m not happy with its memory use or lack of stability, so I’m still using version 2.1.2.) Hence the funky characters in the track name.

The solution to this, which I should have implemented a long time ago, is to pipe the output of the AppleScript through iconv to convert the encoding from UTF-8 to whatever it is GeekTool wants. A bit of trial and error led me to this GeekTool shell command:

osascript ~/bin/itunes-playing.scpt | iconv -s -f UTF-8 -t ISO_8859-1

The part before the pipe is what I’ve always used, the iconv switches tell it to convert from (-f) UTF-8 to (-t) ISO 8859-1, also known as Latin-1, and to keep silent (-s) and continue the conversion if any errors arise. The result:

I’ve done the same thing for displaying the current PandoraBoy track:

osascript ~/bin/pandora-playing.scpt | iconv -s -f UTF-8 -t ISO_8859-1

I’ve also added a new temperature display next to the time and date down in the lower left corner of the screen:

The text is generated by running the following Python script, which is named temperature and is stored in my ~/bin/ folder.

1:  #!/usr/bin/python
2:  # -*- coding: utf-8 -*-
3:  
4:  import pywapi
5:  
6:  # Get the current temperature for the given station.
7:  noaa = pywapi.get_weather_from_noaa('KARR')
8:  print u'%.0f°'.encode('utf8') % float(noaa['temp_f'])

This is a much-abbreviated version of my weathertext script. Line 2 allows me to include UTF-8 characters directly in the Python source code, and the encode call in Line 8 prepares them for display in the Terminal.1 To get the degree symbol (°) displayed in GeekTool, I follow the procedure above and pipe it through iconv:

~/bin/temperature | iconv -s -f UTF-8 -t ISO_8859-1

Until everything has moved to Unicode, iconv is a handy command to know.

Tags:


  1. This is for Python 2.x, which is ASCII-centric. Python 3 handles UTF-8 more directly and doesn’t need these encoding workarounds.