从 Roger Pate 出色的 one liner 中汲取灵感,我为 pytranslate 创建了一个简单的循环脚本。这目前是临时的——因为我还没有实现错误捕获——等待新的编辑。
#!/bin/bash
# Primary Clipboard poller using xsel (middle click) and pytranslate
# Checks for changes every 1 second
# If change has occured, a command is executed (pytranslate here)
########## Information ##########
# It now stores definitions in a text file - saves bandwith and reduces hits on google (caseless)
# Works for Romance languagse
#TODO
# Character based langauges
# Catch errors
if [ ! -e "pytranslatecache" ]; then
touch pytranslatecache
fi
while [ 1 ]
do
OLDENTRY="$(xsel -p)"
sleep 1
NEWENTRY="$(xsel -p)"
if [ "$NEWENTRY" != "$OLDENTRY" ] ; then
if [ "$(grep -F -o -i "$NEWENTRY" pytranslatecache)" = "$NEWENTRY" ] ; then
echo "From Cache:"
echo "$(grep -i "$NEWENTRY" pytranslatecache)"
else
DEFINITION=""$(pytranslate -s fr "$(xsel -p)")""
echo "$NEWENTRY"":"$DEFINITION
echo "$NEWENTRY"":"$DEFINITION >> pytranslatecache
fi
fi
# Catch Errors - Commands
if [ $? != 0 ]; then
{
echo "Failed to translate string."
} fi
done