Controlling the clipboard via Zenity and Xclip

Slashdot it! Delicious Share on Facebook Tweet! Digg!

In the Dialog

The lines 13 to 27 generate the actual Zenity dialog [3]. The dialog is enclosed in brackets preceded by a dollar sign. This is how the script stores the result in a variable, which allows you to change it later. The variable sp will store the output from the dialog.

Zenity generates a selection dialog for --title and --text by using the parameter --list . Zenith also generates a set of meaningful displays for both items.

The command cut 1-${length} restricts the output text to a maximum of 80 characters; note, however, that the content that is actually stored on the clipboard can be much longer. Eighty is just the number of characters shown.

Line 16 sets the column header, which is followed by the entries, each of which is enclosed within quotation marks. The content begins with Slot<lfdNr> and is followed by the output of the content of the corresponding text file using cat command. The cut command then shears the output down to the maximum length of 80 characters.

Because a relatively long character string would now be in the variable sp , it is necessary to remove unnecessary content and to modify partitions. At the beginning of each selection is the word "Slot" followed by a space and the number of the memory location.

The sed command at the beginning of line 27 alters the slot name taken from the "Slot" string. In the process, I want to explicitly show only the first line of the address, because the clipboard slot may well contain multiple lines. Because the word "Slot" is followed by a space, I can also get rid of that.

The number one (1 ) at the end of sed's s (substitution) command causes the program to only make changes in the first occurrence of the expression.

The grep command in the second half of line 27 extracts the content from the content of the clipboard itself, which places it into the sp variable. The values will be from slot1 to slot10 .

Wild Activism

Next, the block of commands from line 29 to 39 checks the contents of the variable action in order to undertake one of four possible actions with the clipboard content.

The first if statement is used if you make the first parameter of the script a save command. This command saves the value in the clipboard into the slot determined by sp . The script uses the shortened form of the test command (i.e., square brackets to enclose the test to be evaluated).

At this point, the script returns the contents of variable sp and uses the echo -n command (-n disables the line break \n ) to count the number of words in the string via wc -m in the next step of the pipe. The length is 0 for unused variables. This lets you test whether the user has made a selection on the previous screen and whether you can proceed with the script. This step then assigns the current content of the clipboard to the selected slot.

The second condition, the elif on line 31, implements the retrieve action after having made a selection in the dialog. In this case, the script will activate the Xclip command and fill the clipboard with the contents of a selected slot. Xclip comes with three areas (primary (Standard), secondary or simply clipboard ) that can contain data.

To tell Xclip which part of the clipboard you want to fill, Xclip provides the -selection option. You can enter primary (standard), secondary , or simply clipboard . Because not all window managers and applications use the clipboard assign (or retrieve) in the same way, the script fills all three areas of the clipboard with the same data (lines 32 to 34), so that the selected text is available for all programs.

Line 35 checks whether the delete action delete has been passed to the script and if you have actually selected an entry. If you have done so, it erases the contents of the corresponding memory location.

Last but not least, line 37 takes care of the edit action, edit . Line 38 delivers the content of the selected memory slot to the text editor set in line 2.

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