Zim clipping script on Ubuntu Linux
Final Edit: The author of Zim has implemented this functionality in a plugin called “Quick Note” that is distributed with Zim by default, so my scripts are now obsolete.
This is a little script that should work in any GTK environment on any distro.
EDIT March 30, 2010: I’ve just noticed that this has been linked from the Zim wiki “Tricks and Tips” page so I’ve updated the code just a little, to use the current Zim file header. Please use the “contact” link above to get in touch with me if you have questions or suggestions to improve this.
EDIT April 8, 2010: I got motivated (because of the above) and wrote a new script that’s (somewhat) better. Whoa. Check it out here.
Background:
One of the note-taking programs I’ve been using is the Zim Desktop Wiki. Zim is nice for keeping todo lists, taking notes from the internet or during a meeting, and even writing a multi-section document (such as a book with chapters). It supports inline images and linking to other files, and it feels like a simpler version of Evernote in many ways. It is very fast and responsive to use, even with large notes and notebooks.
Evernote has a nice “clipping” feature – select some text in any program (such as your web browser), hit the keyboard shortcut, and that text will be “clipped” into a new Evernote note. Next time you’re checking your notes, it will be there waiting for you. Unfortunately this feature of the Evernote Windows client doesn’t work in WINE on Linux, although otherwise the client performs decently.
When I switched to Zim, I wanted to maintain this clipping functionality. Fortunately, it’s easy to script for Zim because of Zim’s transparent file structure and easy-to-read text-based note format.
A couple pre-requisites:
- Install xsel, a utility that allows you to interact with the X selection (you may already use the X selection when you select text to copy, and middle-click to paste). Ubuntu doesn’t ship with xsel by default, so:
sudo apt-get install xsel - I actually use Zim with only one “Notebook” (as defined by Zim). I keep all my notes organized in a hierarchy within that notebook. Whether you use Zim this way or not, this script will be much easier to use if you have all your Notebooks in one directory on your computer.
The Script:
#!/bin/bash
#zim clipper: This will copy the x selection to a new Zim note.
#You can choose which notebook to insert it into.
#xsel does the heavy lifting here, it's the text
#that you selected. It's just plain text, no formatting, no pictures, etc.
#we do this first so you can select more text for the note title
note_text=`xsel`
#The script assumes all your notebooks/notes are in the same
#directory. I use ~/.zim-notes. Change the below line if you
#use a different directory.
cd ~/.zim-notes
#dialog 1: ask for the note title
note_title=`zenity --entry --text="Note Title:"`
#dialog 2: present a file selection dialog showing the
#folders in the directory above. With Zim, Folders == Notebooks
notebook=`zenity --file-selection --directory --title="Select a Notebook:"`
notebookname=`basename $notebook`
#prepare file name by removing underscores, replacing spaces
#with underscores, then making it alphanumeric, except those underscores.
note_file_name=${note_title//_/}
note_file_name=${note_file_name// /_}
note_file_name=${note_file_name//[^a-zA-Z0-9_]/}
#dialog 3: ask for additional text for the note
note_add_text=`zenity --entry --text="Additional Note Text:"`
#The next 5 lines set up the text file header for Zim format
the_date=`date +%FT%T.000000`
note_header_1='Content-Type: text/x-zim-wiki'
note_header_2='Wiki-Format: zim 0.4'
note_header_3='Creation-Date: '$the_date
#send a system notification:
notify-send "New Zim Note" "\"$note_title\" added to notebook \"$notebookname\"" --icon=zim
#OK, now write the text file with the rest of the file
cat > "$notebook"/"$note_file_name".txt << EOF
$note_header_1
$note_header_2
$note_header_3
====== $note_title ======
$note_text
$note_add_text
EOF
Feel free to use, modify, etc. this script. The commented lines in the script should be helpful in explaining exactly what it does.
Copy-paste the code into a new text file, give it a name (mine is “zim-clipper”) and make it executable. Make a keybinding that executes the script.
Select text in any program, and hit your keybinding. You’ll see a small dialog box that asks for the title of the new note. After entering the title, you’ll be presented with a file dialog asking you to “choose a notebook”. Really, it just displays a list of folders inside your “notes” directory (My notes directory is “~/.zim-notes” – if yours is different, then change it in the script). Select a folder (notebook), and you’ll finally get one more dialog box asking for any additional text in the note. At this point it’s fine to copy-paste again – you won’t lose the note text from the x selection (that’s already been stored in the script).
When you hit “OK”, a notification will pop up and the script will write the new note. To see the new note in Zim, either restart Zim or just choose “Tools -> Rebuild Index” and your new note should be available in the note tree on the left.
PS:
I’m currently moving to Tomboy because of the apparent Ubuntu One integration in Karmic. Gnome-Do has a plugin that allows you to make a new Tomboy note from selected text, so that’s what I’m using right now. This doesn’t mean I will never be back to Zim, of course.
And by now (March 30, 2010) I have indeed gone back to Zim. To stay. For now.



