Home > python > Copy string to X clipboards

Copy string to X clipboards

Problem

In a post on my other blog, I’ve presented two command-line utilities (xclip and xsel) to manipulate X selections.

At the end of that post, I pointed out a problem with these tools: they copy strings to one clipboard only. As we know, under X there are actually 3 different clipboards: the “primary”, the “secondary”, and the third that is simply called “clipboard”. The paste operation is available with the middle mouse button (for “primary”) or with Shift + Insert (for “clipboard”).

It must have happened to you too that you wanted to paste the contents of the clipboard and accidentally you pasted the contents of the other clipboard. It’s quite annoying… So, let’s make a Python script that works just like xclip or xsel, but the specified string is copied to all clipboards. Thus, you can use any paste method to insert your text :)

Solution

Here, I will copy the specified string to the “primary” and “clipboard” clipboards. The “secondary” clipboard is not very clear for me…

Our script will be a wrapper around xsel, thus first we need to install this tool:

sudo apt-get install xsel

Then, here is the script:

#!/usr/bin/env python

# tocb.py ("to clipboard(s)")

import sys
import subprocess

###############################################

def text_to_clipboards(text):
    # "primary":
    xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
    xsel_proc.communicate(text)
    # "clipboard":
    xsel_proc = subprocess.Popen(['xsel', '-bi'], stdin=subprocess.PIPE)
    xsel_proc.communicate(text)

###############################################

stuff = sys.stdin.read()
text_to_clipboards(stuff)

In Python, sys.stdin is a file-like object; we read everything from the standard input, then this string is copied to the clipboards using the xsel tool.

Usage

I recommend saving the script above under the name “tocb.py” somewhere (for instance in ~/python/clipboard/tocb.py). Then put a symbolic link on the script called “tocb“. Make sure that the symbolic link is in the PATH. The usage of the script is similar to xsel or xclip:

cat file.txt | tocb

Now the contents of file.txt is on the clipboards. You can paste it either with the middle mouse button or with Shift + Insert.

Future work

The script could be improved to treat some command line parameters too like “-c” to clear to clipboards or “-o” to print the contents of the clipboard, etc.

Update (20110402)

The script is pushed to GitHub: https://github.com/jabbalaci/Bash-Utils.

About these ads
Categories: python Tags: , ,
  1. July 10, 2012 at 18:32

    Works perfect! I’m using it within a program of my own. Have you already looked into clearing the clipboards? I would love to know how to do that. (I’m new to python..)

    • July 11, 2012 at 20:26

      Very simple. Just copy the empty string: text_to_clipboards("").

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

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

Follow

Get every new post delivered to your Inbox.

Join 72 other followers

%d bloggers like this: