Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get and Set X11 clipboard(s) in Linux

I have Foxit Reader running in WINE on Ubuntu 12.04. I want to copy and paste text into a bookmark, but I need to capitalize it (e.g., fusion becomes Fusion). I want to press F5 and run a python script. I understand this is possible with Autokey, but the latter has a documented bug with its clipboard handling.

So, now I'm looking for clipboard alternatives for Autokey. If my python script runs a shell, perhaps the shell could access the clipboard? xclip seemed promising but its documentation says, "Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications." I don't need standard in or a file; I need the proper X11 clipboard (aka selection).

In short, how can python or a shell read the existing X11 clipboard(s)?

like image 983
mellow-yellow Avatar asked Nov 14 '25 14:11

mellow-yellow


2 Answers

xclip -o | sed 's/^./\U&/g' | xclip -i

This will read the X clipboard, capitalize the content and overwrite it

like image 193
Riccardo Galli Avatar answered Nov 17 '25 08:11

Riccardo Galli


I realized that the -o parameter reads a selection, but you must specify which you need:

xclip -selection clipboard -o

From there, I used this StackOverflow answer. It works nicely.

#read clipboard, avoid autokey's get_selection bug
tag = subprocess.Popen(["xclip","-selection", "clipboard", "-o"],stdout=subprocess.PIPE).communicate()[0]

#https://stackoverflow.com/questions/764360/a-list-of-string-replacements-in-python
mapping = { "'":'', ',':'', '"':'', ';':'', '(':'', ')':'', '.':'', '-':' '}
for k, v in mapping.iteritems():
    tag = tag.replace(k, v)

#Camelcase, remove spaces, and append Caesar tag
tag=tag.title().replace(' ','')+"_"
like image 21
mellow-yellow Avatar answered Nov 17 '25 09:11

mellow-yellow