Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing selection clipboard from ipython

I want to access the text in the clipboard from within ipython.

I got this far (not even sure if this is the best way, just found by poking around in ipython magics sources):

import IPython
from IPython.core.hooks import clipboard_get
ip = IPython.get_ipython()
my_string = clipboard_get(ip)

And it kinda works for stuff I've copied manually, but I want to get the "other" clipboard - the one you get when you use the middle mouse click. The selection buffer or whatever it's called.

Any ideas?

like image 273
wim Avatar asked Aug 13 '26 19:08

wim


1 Answers

You can get X Window's "middle mouse button" selection (called the PRIMARY selection) through Tkinter:

import Tkinter # Replace "Tkinter" with "tkinter" for Python 3.x.
tk = Tkinter.Tk()
tk.withdraw()
print(tk.selection_get())

Another solution is to run xclip and get its output. (If you don't have xclip installed it can be found in most Linux distributions' package repositories.)

import subprocess

print(subprocess.check_output(['xclip', '-o', '-selection', 'PRIMARY']))
like image 78
nwk Avatar answered Aug 16 '26 11:08

nwk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!