Simple scripts for operating the system clipboard

Linux systems have three kinds of text selection – primary, secondary and clipboard. The clipboard is the most famous one, Ctrl+c, Ctrl+v, you know the stuff. At work I need to note down all the important stuff I worked on recently, to have some kind of work report. So I end up with document with many bullets containing project names, bugzilla ticket summaries and URLs, etc. What I really hate is transferring rich text properties in the clipboard together with the content. That means if I select a title of some bug, it is copied (into my web application I use for creating reports) as a big thick line with custom font. One has to reset all the formatting after every such insertion. Awful!

So let’s do something about it. Wouldn’t it be nice to convert the contents of the system clipboard to plaintext only? Fortunately a little of bash scripting can help:

#!/bin/bash

# print usage
if [ "$1" = '--help' -o "$1" = '-h' -o $# -ne 0 ]; then
 echo "Usage: $0"
 echo "Takes text in system clipboard and transforms it into plaintext."
fi

# get the plaintext
TEXT=`xsel -b`

# print the text, it may come handy sometimes
echo "$TEXT"

# replace the original rich-text with plaintext
echo -n "$TEXT" | xsel -b -i

Well, and now just store this script in your PATH and call it whenever needed. I used Gnome’s Keyboard shortcuts to define a shortcut to call this script anytime I need it. Works perfect 🙂

I also needed to print same basic information about a particular bug in a specific format, so I can easily put it into my report. Manually copying all the information is not fun. Let’s create another script (python-bugzilla must be installed):

#!/bin/bash

# print usage
if [ "$1" = '--help' -o "$1" = '-h' -o $# -ne 1 ]; then
 echo "Usage: $0 bug_id|bug_url"
fi

# parse bug id
ID="$1"
if [[ "$ID" == http* ]]; then
 ID=`echo $ID | sed -r 's/.*id=([0-9]+).*/\1/'`
fi

# query bugzilla
INFO=`bugzilla query --bug_id "$ID" --outputformat='%{url} %{component} %{summary}'`

# parse info into parts
URL=`cut -d ' ' -f 1 <<< "$INFO"`
COMPONENT=`cut -d ' ' -f 2 <<< "$INFO"`
SUMMARY=`cut -d ' ' -f 3- <<< "$INFO"`

# output text in a suitable format
OUTPUT="$URL
 \"${COMPONENT}: $SUMMARY\""

# print the output
echo "$OUTPUT"

# copy the output to X clipboard
echo "$OUTPUT" | xsel -b -i

# play a notification sound
paplay /usr/share/sounds/freedesktop/stereo/message.oga

So now I just run “bug 595448” command and it prints all the necessary information out for me and puts it also into the clipboard. Hail to the improved work efficiency! 🙂

I hope this helps somebody in these kinds of repetitive tasks – creating work reports. Improvements welcome.

Advertisements
Simple scripts for operating the system clipboard

Leave a Reply (Markdown syntax supported)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s