• Tip Jar

Sharing the Clipboard with the Console

Sharing the clipboard between your GUI desktop and the console can be a bit of a challenge. I was doing it by pasting into a temperary file then opening that file in the console. Then, however, I got to thinking there has to be a better way. So, I wrote a bit of code in python to automatically check the clipboard for text and write it to /tmp/clipboard. It works quite nicely, but I have noticed that when it is running, switching between applications with alt+tab is slower than normal. It may be something to do with the loop slowing things down, but I am not sure. For those who are interested, here is the code:
#!/usr/bin/python
#Clipboard to File Written by Storm Dragon
#Copyright May 2011 by Storm Dragon
#License GPL
#This program places any text in the clipboard into the file /tmp/clipboard
#The file is updated every 0.5 seconds

#Library import section
import time
import pygtk
import gtk
#Loop forever!
while True:
    #Get the clipboard
    cb = gtk.clipboard_get()
    #assign the clipboard contents to a variable.
    cbText = cb.wait_for_text()
    if isinstance(cbText, str) == True:
        file = open(“/tmp/clipboard”,”w”)
        file.write(cbText)
    time.sleep(0.5)
After I completed this small program I learned of the existance of a clipboard manager called parcellite. In Ubuntu and Vinux you can install it with:
sudo apt-get install parcellite
You can adjust parcellite’s preferences by right clicking on it in the panel and selecting preferences. If you left click it you will get the clipboard’s contents and the history up to 100 previous selections depending on the setting in preferences. You can also access the clipboard history with the keyboard shortcut control+alt+h. In order to have parcellite start when you login, you will need to add it to startup applications in System, Preferences, Startup Applications. If you choose to use my script you can add it to startup applications instead.
If you use parcellite you can access the clipboard history in the console in the file located at:
~/.local/share/parcellite/history
Because it is a binary file it does not work in Gedit.
If you use my program or the clipboard manager glipper it will work in gedit. The file my program writes to is:
/tmp/clipboard
My program does not contain any history, only the last text in the clipboard.
After a bit more research I find I prefer using the clipboard manager Glipper:
sudo apt-get install glipper
To access the clipboard press control+alt+c. To access the history in the consolesimply open the file:
~/.glipper/history
Of course, if you use glipper you do not need my script.
Parcellite also has the ability to copy text from the terminal, gnome-terminal or any terminal opened from the desktop. If you try from the console you get an error about not being able to open the display. If you do:
echo “hello world” | parcellite
then the words "hello world" will be in your clipboard. You can accomplish the same thing by using a program called xclip:
sudo apt-get install xclip
To place something in the clipboard:
echo “hello world” | xclip -selection clipboard
You can download Clipboard to File by right clicking the link and selecting save link as.
After some descussion it was decided that probably the best way to cut out the lag caused by clipboard-to-file is to drop the loop completely and just bind the program to a keyboard shortcut. So, here is the new code, the download link will now also get you this code as well:
#!/usr/bin/python
#Clipboard to File Written by Storm Dragon
#Copyright May 2011 by Storm Dragon
#License GPL
#This program places any text in the clipboard into the file /tmp/clipboard

#Library import section
import time
import pygtk
import gtk
#Get the clipboard
cb = gtk.clipboard_get()
#assign the clipboard contents to a variable.
cbText = cb.wait_for_text()
if isinstance(cbText, str) == True:
    file = open(“/tmp/clipboard”,”w”)
    file.write(cbText)
I went to System, Preferences, Keyboard Shortcuts and added clipboard-to-file and set its shortcut to control+alt+v. This new method has all of the coolness with none of the slowness.
One more thing that will make sharing info between the GUI desktop and the console easier is a paste command for the IRC client irssi. I actually have 2 of them. These use assume you are using glipper. The first simply pastes the last thing in your clipboard history to irssi and sends it as a message:
/alias paste /exec -o cat /home/USER/.glipper/history | head -n 2 | tail -n 1
the other is for when you want to paste several lines. Each line will be sent as a message so be careful with this one. Irssi will prompt you if you try to send more than seven lines, so there is a safety net:
/alias read /exec -o cat /tmp/clipboard
To use this one just make sure the text you wanto to send as a message is in the file /tmp/clipboard.

Bookmark the permalink.

Comments are closed

  • Tip Jar