Archive for the ‘Technical tips’ Category.

Firefox: creating a shortcut to ScrapBook highlight operations with the Keyconfig extension

Summary: ScrapBook is a Firefox extension that allows one to save snippets from web pages locally, and to add notes and highlights to them. I wanted shortcuts to access the highlighting features. Using the Keyconfig extension, which allows you to create new keyboard shortcuts, I associated 4 keyboard commands to the 4 available highlight styles.

The bare steps

(The following, of course, assumes having the ScrapBook extension installed.)

1. I installed the Keyconfig extension and restarted Firefox. It’s available here, just click on keyconfig.xpi.

2. In Tools > Keyconfig, I first selected the “Keyconfig” option in the dropdown list at the top of the window.

3. For each highlight operation (there are 4 possible styles), I created a new key with the titles you may see in the screenshot (“scrapbook highlight 1”). The code associated with each is:

    sbPageEditor.highlight(1)

and I replaced “1” by 2, 3 and 4 for the other operations. Also, I checked the “global” checkbox a the top of the “key editor” window.

4. After having defined that operation, I assigned it a keyboard shortcut , trying not to conflict with existing shortcuts for other operations. To assign the key, select the command you want, then click in the text box next to the “Apply” button. Now enter the keyboard combination you want, and click “Apply”.

5. I also configured two other operations which I find useful, “save” and “undo”, which are associated with the code

    sbPageEditor.saveOrCapture()

and

    sbPageEditor.undo()

respectively.

The underlying explanations

The “hard part” part (well, the non-obvious part) here was to find the code to activate the highlighting operations. That may be interesting to you if you want to repeat the process for other operations, perhaps in other extensions. There are many ways to go about doing this, one involving the DOM inspector in Firefox which is faster than the one I used. Mine involves exploring the extension source code, which I find instructive.

Firefox has a very extensible architecture. One key ingredient in this extensibility is XUL, the language used to describe components in the user interface. It’s real simple to grasp the basic principles, especially if you know HTML and JavaScript. Basically, extensions are programmed in XUL and JavaScript.

Firefox extensions may usually be found in your user directory (under Linux, by default, it’s in .mozilla/firefox/…/extensions, in your home directory). Their source code is right there for you to look at. Each extension is in its own directory, usually named with easy-to-remember strings like “{53A03D43-5363-4669-8190-99061B2DEBA5}” (here the Scrapbook extension directory name).

The code for ScrapBook is in there, in a JAR file (chrome/scrapbook.jar), which is just another extension for a Zip file, so you may open it and uncompress it with an unzipping utility. The file that interests us here is in this JAR file; it’s content/scrapbook/overlay.xul. This is the file that describes the way ScrapBook attaches itself to the main Firefox window, I believe.

Amongst other elements described in overlay.xul, you’ll find the toolbar which displays the highlight buttons (and “undo”/”save”). To find the line, search for “ScrapBookHighlighter”. The code for the operations is in the “oncommand” attribute.

To find the line and file of interest, which might seem non trivial given the number of files in there, I used a global search on files for the term “highlighter”.

Using xsel, xbindkeys and xmacro to insert common strings (date, name, etc.) in Linux

Goal: insert a date string (e.g. “2008/06/29”) in almost whatever text area/graphical program I’m using currently with a single keyboard shortcut.

Basic principle: copy the date string to the clipboard and emulate the Ctrl-V key combination, which activates the Paste operation in most programs.

Limitations:

  • The current program must support Ctrl-V as the Paste shortcut (won’t work on the command line, for example).
  • The current clipboard data is erased, replaced by the string inserted.

The bare steps I took

I’m using Ubuntu, but I guess this should work with most distributions, by adapting the installation instructions.

1. I installed the necessary utilities.

    sudo apt-get install xsel xbindkeys xbindkeys-config xmacro

2. I created a default xbindkeys configuration file.

    xbindkeys --defaults > ~/.xbindkeysrc

3. I wrote this shell script, which I saved on my local disk.

4. I configured xbindkeys using xbindkeys-config to launch the script when the Windows-D shortcut is pressed.

a. I loaded “xbindkeys-config” by typing that on the command line.

b. I created a new shortcut and associated it with the script.

c. I associated it with the Windows-D shortcut by pressing “Get Key” and then pressing Windows-D.

d. I saved and exited.

e. I arranged it so the “xbindkeys” command would run on every logon, which can be done by adding the line “xbindkeys” to a logon script (/home/…/.bash_profile, for example).

The underlying explanations

I often find it useful to insert the date in personal notes I take. So often, in fact, that it’s quite handy to automate the insertion. On Windows, there’s this handy app and scripting language called AutoIT which may be used to automate common tasks. But under Linux, quick googling doesn’t reveal any self-evident choice for an all-encompassing scripting language, so I went looking into more application-specific options.

The first key element to my solution, xsel, is a program that allows one to control the X selection and clipboard from the command line. The second utility, xbindkeys (and its configuration GUI, xbindkeys-config), as you guessed, allows you to associate keyboard shortcuts to commands. Finally, xmacro is a program that allows you to emulate specific keyboard key events, like key presses, and mouse events.

I therefore associated (using xbindkeys) a keyboard shortcut to a script that copies the date string to the clipboard (using xsel) and emulates the Paste keyboard shortcut (using xmacro). That’s a pretty complicated solution, but all in all it didn’t take so much time to set up. I commented the script so you may get a better understanding of the parameters used.

I could have used other shortcuts that Windows-D, but the Windows key isn’t used under Ubuntu, so this was a great occasion to capitalize on keyboard real estate.

As an alternative approach, which doesn’t replace the clipboard data, you can use xmacro to insert every single character in the date string, one at a time. I began by doing this, since it’s much simpler. The problem I ran into is instability: some programs need a delay between keypresses, otherwise they mix up the letters, and the insertion seems slow and sometimes misses letters. That limitation prompted me to try this solution.

If anyone has a suggestion as to how to improve this solution, feel free to post a comment about it.

Further reading and references