Search:  
Gentoo Wiki

Clipboard_speak


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Introduction

If you have installed speechd and festival, you may wish to have festival read the contents of the clipboard. Here is a simple script which will read the contents of the clipboard and pipe them to festival.

Installation

Simply download this file, store in your path (~/bin or /usr/bin), and start pastespeak from the command line. If you use KDE, you can call it from your ~/.kde/Autostart folder to have text reading capabilities as soon as you log in.

/usr/bin/pastespeak

#!/bin/bash
#PasteSpeak
#Script to check for a change in clipboard contents, and pipe change to tts
#Requires: KDE, dcop, klipper, tts service (We used Festival)
#by RFC_1149 (#gentoo on Freenode)
#Released under GNU General Public Licence v2
#v 0.3

#Sanity Checks
 
#Check user entered the correct parameters
if [ -z $1 ]; then
echo "Usage: $0 <time> [maxlength] [output]"
echo "Where"
echo "<time> is the number of seconds to wait between polling the clipboard"
echo "[maxlength] is the maximum letters to speak (default 1000)"
echo "[output] is the history file"
exit 1
fi
 
#Check programs exist
which dcop > /dev/null && which klipper > /dev/null && which festival > /dev/null && which wc > /dev/null
if [ "$?" = "0" ]; then
:
else
echo "You do not have the required programs installed. Please ensure they are installed and in your path"
echo "$0 requires festival, dcop, wc, and klipper"
exit 1
fi
 
#Declare Vars - I could probably use better names
CONTENTS=	#the current clipboard contents
REPTIME=$1	#the amount of seconds between checking
OLDCONTENT= 	#the previous clipboard contents
OF=$3
 
if [ -z $2 ]; then
MAXLENGTH=1000
else
MAXLENGTH=$2
fi
 
if [ -z $3 ]; then
OF="/dev/null"
fi
 
#Since I do not know of a way to notify this(); when the contents change, we will check the clipboard at intervals
 
klipper > /dev/null & #starts klipper in background (duh) - may already be running

OLDCONTENT="`dcop klipper klipper getClipboardContents`"
OLDCONTENT="`echo $OLDCONTENT | sed 's/[^a-zA-Z0-9 ]//g' | cut -c 1-$MAXLENGTH `"

while(true); do
sleep $REPTIME
CONTENTS="`dcop klipper klipper getClipboardContents`"
CONTENTS="`echo $CONTENTS | sed 's/[^a-zA-Z0-9 ]//g' | cut -c 1-$MAXLENGTH `"
if [ "$OLDCONTENT" = "$CONTENTS" ]; then
: #No change, restart loop
else
   CONTENTS="`echo $CONTENTS | sed 's/[^a-zA-Z0-9 ]//g' | cut -c 1-$MAXLENGTH `"
   echo "$CONTENTS" >> $OF
   echo "$CONTENTS" | festival --tts - #Speak the contents of the clipboard
   OLDCONTENT=$CONTENTS #update the refernce reading, start again

fi
done

#Cleanup
unset OLDCONTENT CONTENTS REPTIME MAXLENGTH OF
exit 0

About

This script was written in the early hours after a user on #Gentoo wondered if the concept was possible. The script is quick and dirty - please feel free to improve it, and leave comments on this talk page. I will check back regularly and would be grateful for any improvements or suggestions. - RFC_1149@FREENODE

Retrieved from "http://www.gentoo-wiki.info/Clipboard_speak"

Last modified: Fri, 05 Sep 2008 02:02:00 +0000 Hits: 3,826