3

For example, I open a pdf file or a web page in gnome, use mouse double click some text, so a word is selected, how can I get this word in a background running daemon written with python-dbus?

Some simple but working piece of script is appreciated greatly.

Thanks!

1
  • AFAIK, this behavior is application dependent: if Application X doesn't export the text, you are out of luck. Use D-feet or alike to introspect the methods of an application. Commented Nov 16, 2009 at 13:47

2 Answers 2

7

You don't need D-Bus, simply listen to changes for the middle-click (Selection) clipboard with for example Gtk:

import gtk

def _clipboard_changed(clipboard, event):
    text = clipboard.wait_for_text()

clip = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
clip.connect("owner-change", _clipboard_changed)
Sign up to request clarification or add additional context in comments.

2 Comments

WOW, awesome! Works great! Just need an extra line: gtk.main() to let the program keep running. I want to implement something like the usual translation software. I guess I can open up a new window and place it around the mouse. And only if I can insert dynamic annotate to the pdf files being read, perfect.
I had trouble with the gtk.gdk.SELECTION_PRIMARY. It only contained clips that had been manually copied using the mouse. I sometimes populate the clipboard using xclip in a bash terminal, so the selection setting that works for me (for both mouse and scripted clipboard content updates) was gtk.gdk.SELECTION_CLIPBOARD. I also added a conditional to check for NoneType before passing the contents on, otherwise you will get the string "None" in your variable named text above.
0

Gnome Do has a few plug-ins that use the selected text. I'm not sure how it is implemented (and if it uses DBus), but the code should reveal all. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.