Controlling the clipboard via Zenity and Xclip

Slashdot it! Delicious Share on Facebook Tweet! Digg!
© Aleksandr Lobanov, Fotolia.com

© Aleksandr Lobanov, Fotolia.com

Cut & Paste

Not every window manager provides a program for managing a clipboard. With Zenity and a script, you can comfortably control Xclip from anywhere.

Gnome, Unity, and KDE all have their own clipboard managing applications; they provide extensive features and are well integrated into the desktop. However, they are not necessarily easily integrated into other window managers. Those who do not use a top of the line desktop might be left without a clipboard manager. Another problem is that these programs usually create a history of the clipboard in which the older entries are continuously pushed back and eventually disappear from the list.

The Right Tools

Typically, to adapt an application as quickly as possible, all you need are the right tools, some creativity, and a little bit of imagination. This also holds true for the implementation of a tailor-made clipboard management app. I'll show how you can set up sleek dialogs for the script of the clipboard using Xclip [1] with Zenity [2].

Both Zenity and Xclip can be installed quickly using your package manager of choice. Both pieces of software can be installed via Apt or Yum using the command sudo apt-get install zenity xclip on DEB-based systems (e.g., Ubuntu) or yum install zenity xclip on a RPM-based computer.

The clipboard presented here comprises 10 memory slots. The content in these memory locations can be allocated, recalled, deleted, or manually revised retroactively with an editor.

A Few Lines of Script

Listing 1 begins by setting a fixed editor that allows you to manually and retroactively revise individual actions of the clipboard. This also defines a directory for the contents of the clipboard. The command test in line 4 checks whether the directory already exists. If the check determines that there is no directory in existence, then it creates one.

Listing 1

Personal Clipboard

01 #!/bin/bash
02 EDITOR=gedit
03 STORAGEPATH=~/.clipboard
04 test ! -d $STORAGEPATH && mkdir $STORAGEPATH
05
06 for x in $(seq 1 10); do
07   test ! -f ${STORAGEPATH}/slot${x} && > ${STORAGEPATH}/slot${x}
08 done
09
10 action=$1
11 length=80
12
13 sp=$(zenity --list --title="Select slot" \
14      --text "Slot ${1}!\nCurrently in the clipboard: \
15             $(xclip -o | cut -c 1-${length})" \
16      --column "Slots" \
17      "Slot 1: $(cat ${STORAGEPATH}/slot1 | cut -c 1-${length})" \
18      "Slot 2: $(cat ${STORAGEPATH}/slot2 | cut -c 1-${length})" \
19      "Slot 3: $(cat ${STORAGEPATH}/slot3 | cut -c 1-${length})" \
20      "Slot 4: $(cat ${STORAGEPATH}/slot4 | cut -c 1-${length})" \
21      "Slot 5: $(cat ${STORAGEPATH}/slot5 | cut -c 1-${length})" \
22      "Slot 6: $(cat ${STORAGEPATH}/slot6 | cut -c 1-${length})" \
23      "Slot 7: $(cat ${STORAGEPATH}/slot7 | cut -c 1-${length})" \
24      "Slot 8: $(cat ${STORAGEPATH}/slot8 | cut -c 1-${length})" \
25      "Slot 9: $(cat ${STORAGEPATH}/slot9 | cut -c 1-${length})" \
26      "Slot 10: $(cat ${STORAGEPATH}/slot10 | cut -c 1-${length})" \
27      | sed "1 s/Slot /slot/1" | grep -Eo "slot[0-9]{1,2}")
28
29 if [ $action = "save" ] && [ $(echo -n $sp | wc -m) -gt 0 ] 2>/dev/null; then
30   xclip -o > ${STORAGEPATH}/${sp}
31 elif [ $action = "retrieve" ] && [ $(echo -n $sp | wc -m) -gt 0 ] 2>/dev/null; then
32   cat ${STORAGEPATH}/${sp} | xclip -i -selection primary
33   cat ${STORAGEPATH}/${sp} | xclip -i -selection secondary
34   cat ${STORAGEPATH}/${sp} | xclip -i -selection clipboard
35 elif [ $action = "delete" ] && [ $(echo -n $sp | wc -m) -gt 0 ] 2>/dev/null; then
36   > ${STORAGEPATH}/${sp}
37 elif [ $action = "edit" ] && [ $(echo -n $sp | wc -m) -gt 0 ] 2>/dev/null; then
38   $EDITOR ${STORAGEPATH}/${sp}
39 fi

The code block at lines 6 through 8 creates the individual memory slots for the clipboard. A for loop operates from a list created by the command seq 1 10 . Within the block, between do and done , each digit appears instead of the line ${x} . Line 7 first tests whether the files slot1 to slot10 are in the directory. If this is true, then the redirection operator > gets executed at the end of line 7, creating the slot.

In line 10, the script grabs the first parameter passed on to the script and stores it in the variable action . Line 11 defines the maximum length of the character string, which should be displayed in the Zenity dialog. This prevents trying to store an excessively long string in one of the memory locations of the corresponding dialog. The first 80 characters should be plenty to indicate which text is saved to each slot.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content