Ubuntu – Text to speech for selected text ubuntu 16.04

16.04text to speech

While a lot of options exist to read aloud text pasted to the command line, what's the best software/tool to achieve the same function for highlighted text? I am looking for something similar to mac OS but haven't been able to find anything.

Already tried this command :

bash -c "gespeaker --play-text=\"$(xsel | sed -e :a -e '$!N;s/\n/ /;ta')\""

But doesn't work.Any suggestions?

Best Answer

user597291 here again (don't have an account). Figured out how to do it with a single keybinding.

You can use xclip to take the primary selected text (i.e. highlighted text), then pipe that into the clipboard.

From there use xsel to take the clipboard text and pipe that to espeak. I also recommend sanitizing newlines, otherwise espeak will only read the last paragraph.

Method 1 (separate file):

The way I do it, the keybinding calls a script that does all of this which looks like sh ~/.custom-scripts/play-selected-text in the keyboard binding command.

The script looks like this.

#! /bin/bash

xclip -out -selection primary | xclip -in -selection clipboard
xsel --clipboard | tr "\n" " " | espeak

Method 2 (directly in keybind):

If you don't need a separate folder for custom scripts, you can just put this into the keyboard binding command section.

xclip -out -selection primary | xclip -in -selection clipboard; xsel --clipboard | tr "\n" " " | espeak
Related Question