3

I would like to be able to go anywhere (google chrome, sublime text editor, etc), then highlight + right click on some text, then click a custom right-click-menu button which will run a python script with the highlighted text as input. Is there any tool to do this in Ubuntu 14.04+?

I am aware of Nautilus, however Nautilus right-click commands don't seem to show up on google chrome or sublime text editor, and also I am not aware of any way Nautilus can pass highlighted text as input to a python script.

It would be quite useful to have this capacity, as it would let me automate some tedious aspects of my work-flow, so I wonder if there is a right tool for the job?

Thanks in advance!

1 Answers1

2

Another usage of xclip

The solution below uses xclip. xclip is not on your system by default, you will have to install it:

sudo apt-get install xclip

In its simplest form, you could do it in a very short script with the help of xclip. When we use the xclip -o command, the currently selected text is used to output, or as man xclip mentions:

   -o, -out
          print the selection to standard out (generally for piping to a
          file or program)

Using this, we can easily use the selected text to do anything, e.g.

#!/bin/bash
# get the currently selected text
text=$(xclip -o)
# print the selection into a file in your home directory
echo $text > ~/xclip_output.txt
# opening a file with the selected text as a title, in the current working directory
gedit "$text"

or, in your case:

<script> "$text"

If this exactly matches what you need depends on what you are actually doing with the input text. Possibly you would have to decide what to do with spaces, (single/double) quotes etc, but this is basically "how it can be done".

Adding it to a shortcut key

If you save the script above as use_text.sh, you can simply add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

/bin/bash /path/to/use_text.sh

to a key combination of your preference

See also: man xclip

Jacob Vlijm
  • 83,767