The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Scripting a character chooser with dzen2

By Bob Mesibov, published 13/02/2014 in Tutorials


I use 16 special characters fairly regularly. They sit on a launchable 'palette' on my desktop (first image). I select a character (second image), then paste the character with a middle-click into wherever it's needed: text editor, word processor, email, webpage form etc. The palette stays open and available for grabbing other characters until I close the palette with a right-click.

Figure 1: The dzen2 character chooser palette in desktop action

Figure 2: Closeup of the dzen2-powered character chooser

I built the palette using dzen2, a desktop notification utility written by Robert Manea and available in repositories. The script to launch my palette is:

#!/bin/bash -i
echo -e "—\n°\n±\n¼\n½\n¾\n£\nà\ná\nä\nè\né\nó\nö\nü\nμ" \
| dzen2 -x 15 -y 15 -w 400 -bg "#696969" -fg white -h 30 -sa c -p -l 16 -m h

Configuration options for dzen2 are explained in its README file. The palette is 15 pixels from the left desktop edge (-x 15), 15 pixels down from the top edge (-y 15) and 400 pixels wide (-w 400). It's a menu (-m) laid out horizontally (h) and it persists on the desktop (-p) until closed. It runs in dzen2's 'slave window', within which each menu item is centered (-sa c) and 30 pixels high (-h 30). There are 16 items in the menu (-l 16) and each one is white (-fg white) against a gray background (-bg "#696969"). When a menu item is selected (see second image), the foreground and background colors reverse.

The 16 menu items are my 16 special characters and are piped to dzen2 as a newline-separated list:

echo -e "—\n°\n±\n¼\n½\n¾\n£\nà\ná\nä\nè\né\nó\nö\nü\nμ"

I use dzen2's default choices of font and font size for the menu, but these are also adjustable (see the README).

The default mouse actions for a dzen2 menu are for a left-click to execute the selection and a right-click to close the menu. These default actions can be included in the dzen2 command as an explicit option:

-e 'button1=menuexec;button3=exit'

Alternatively, selecting an item can simply send it to STDOUT with the 'menuprint' action:

-e 'button1=menuprint;button3=exit'

I haven't worked out how to turn the STDOUT output into a command, so I've left the menu with its default action, namely executing the command specified by the selected item. However, there aren't any GNU/Linux commands named ± or ¼, and I haven't figured out how to get dzen2 to accept a selected item as a command alias. So again I scripted, writing 16 very short scripts which pipe a selected character to the xclip utility. For example, this script is named ± and it sends the character ± to xclip:

#!/bin/bash
echo -n ± | xclip

The xclip utility is also in repositories and is very handy. Its default action is to load text into the X or primary clipboard, from which the text can be pasted as many times as required with a middle-click. (For more options, see the xclip man page.)

Next, I added the directory where I put the 16 scripts (~/scripts/dzen) to the PATH variable, so that dzen2 could find them. I did this by editing ~/.bashrc to include the line:

PATH=/home/bob/scripts/dzen:$PATH

One final tweak was to add the -i option (for 'interactive') to the bash line in the dzen2 script. Without that option, the script launched an unresponsive palette. In other words, I could launch the palette and get it working by entering the echo... | dzen2... command in a terminal, but the same commands in a bash script wouldn't work — the palette appeared, but selecting an item did nothing.

My character palette isn't elegant but it works just fine on my Debian Xfce system with UTF-8 encoding. For tinkerers like me, the GNU/Linux toolbox is a backyard shed full of magical little gadgets and spare parts like dzen2 and xclip, and it's the first place I look when a job needs doing!


About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.



About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.

Tags: tutorial dzen2 special-characters scripting
blog comments powered by Disqus