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!

5
  • If I understand well, you have a python script and would like to run it with the selected text asargument? If so, would a shortcut key do? Jul 28, 2015 at 15:31
  • @JacobVlijm Yeah I guess that could do, I highlight the text, press some shortcut key, and that runs a python script with the selected text as argument. Is there a way to do this?
    – applecider
    Jul 28, 2015 at 15:41
  • Absolutely, I have to run, but will post later today if noone did :) Jul 28, 2015 at 15:45
  • @JacobVlijm Awesome! I would appreciate that a lot
    – applecider
    Jul 28, 2015 at 15:48
  • possible duplicate of Terminal : open path with middle-click/context menu?
    – A.B.
    Jul 28, 2015 at 19:32

1 Answer 1

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

3
  • Why this: echo $text > ~/xclip_output.txt :)
    – A.B.
    Jul 28, 2015 at 19:20
  • @A.B. Just a silly example. It was my first test line. I left it as an example :) Jul 28, 2015 at 19:29
  • Brilliant, that worked pretty smoothly!
    – applecider
    Jul 28, 2015 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .