3

I'm running Mac OS 10.9 and I use the Text to Speech feature which allows you to use a hotkey that reads selected text in any application. I use this feature for reading out-loud PDF documents in Preview (Apple's PDF viewer).

The problem is that the selected text usually has word-break hyphens. The result is the Text to Speech feature doesn't read out the text properly.

Example:

enter image description here

Question:

Can you write me a short bash shell script that removes hyphens from the text in the clipboard? I will use automator too, so I can assign a hotkey, and to send the script's output to the Speak Text feature.

Edit: (in reply to goldilocks's comment) Automator allows me to copy selected text to the clipboard followed by a shell script followed by the Speak Text feature. See image below.

enter image description here

2nd Edit: in fact there might be a more concise way of accomplishing this:

enter image description here

under the 'Pass input' option there is a 'as argument' option. I'm not sure whether this means I can skip the 'copy to clipboard' action in the first image, but I thought I'd mention it just in case.

6
  • "Selected text" is not the same as text in the clipboard. If the application reads selected text, you may be out of luck unless OSX provides a means of working on that buffer.
    – goldilocks
    Mar 30, 2014 at 22:47
  • @goldilocks Please see my most recent edit. OS X does provide the means via automator. All i need the shell script to do is fetch the data in the clipboard, remove the hyphens, and print it out. The simple echo command I used (in the photo above) successfully spoke the text, "clipboard text here"
    – Far Zin
    Mar 30, 2014 at 23:06
  • I see you removed your comment on my answer. If for whatever reason the newlines have been stripped and replaced with spaces your sed 's/- //' would work, otherwise you need the longer one to deal with them.
    – Graeme
    Mar 31, 2014 at 0:24
  • It seems the script works now, but not in all instances. Based on my tests, it seems the Speak Text feature doesn't like formatted text. Is there code I can add so that the text piped to Speak Now isn't formatted? BTW, I genuinely appreciate the help.
    – Far Zin
    Mar 31, 2014 at 0:37
  • Probably, you would need to post an example of the raw text stream. I can have a look tomorrow. Btw, if you use @Graeme in your comment, I will get a notification for it.
    – Graeme
    Mar 31, 2014 at 2:01

1 Answer 1

2

On Linux the following would work:

xsel | sed ':rep /-$/ {N; s/-\n//; b rep}' | xsel -i

On OSX I am largely guessing (although the sed script should work ok). pbcopy seems to the the command to use. Assuming there is a selection buffer on OSX and pbcopy works from it by default, this might work:

pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}' | pbcopy

Looking at the online man page, it seems there are a number of 'pasteboards' that it can work with:

-pboard {general | ruler | find | font}
       specifies which pasteboard to copy to or paste from.  If no pasteboard is
       given,  the  general pasteboard will be used by default.

I have no idea which one (if any) is the one you want and I can't see any further documentation (although I haven't looked too deep). You could experiment with pbpaste -pboard xxx and see what comes out. There is a good chance one is for highlighted text while the other is for text copied with Cmd-C. Given a working option, you would just add it to both pbpaste and pbcopy.

The other thing that may go wrong is that pbcopy has issues reading and writing to the same pasteboard in at the same time. The simplest solution would be to use a non standard Unix utility called sponge. It is part of moreutils. Again Linux-centrically:

pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}' | sponge | pbcopy

Otherwise use a variable:

selection=$(pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}')
echo "$selection" | pbcopy

Update

Assuming Automator uses the stdout of the shell script, you would just select the to stdin option (this is where the data arrives if it is piped to the script). The the shell script would simply be:

#!/bin/sh
sed ':rep /-$/ {N; s/-\n//; b rep}'
0

You must log in to answer this question.

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