Back when I used GNOME, I wrote this script to dynamically set a window's icon. By default, GNOME only used the icon statically set in an app's '.desktop' file.
The script takes at minimum two arguments: the path to an icon file and the window ID
$ set-icon.py /usr/share/pixmaps/terminal.xpm $WINDOWID
The icon-setting script:
set-icon.py.txt
The other portion of the script is a Perl extension for rxvt-unicode that handles setting an icon by name given a certain escape sequence.
icon-setter.txt
This code is MIT-licensed (see below). The icon-setting script also lives in my dotfiles.