Domanda

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?

È stato utile?

Soluzione

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']))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top