#!/bin/bash
# getdclick: simulate double-clic and get selected word

# Time to wait between clicks (test what works for you)
DCLICK_WAIT=0.1

# Check parameters
if [ "$1" = "" ]; then
	echo "usage: getdclick app"
	echo "string '@SEL@' in 'app' will be substituted by X clipboard selection"
	echo
	echo "Simulate double-click on X and get selected word"
	exit 1
fi

# Before double-click, simulate a Release of all buttons
xte "mouseup 1" "mouseup 2" "mouseup 3" "mouseup 4" "mouseup 5"

# Now simulate double click
xte "mouseclick 1" "sleep $DCLICK_WAIT" "mouseclick 1"

# Get selection first word from primary X clipbloard
SEL="$(xsel -p | awk '{print $1}')"

if [ "$SEL" = "" ]; then exit 0; fi

# Substitute @SEL@ by the selected string
EXEC=$(echo $* | sed "s/@SEL@/$SEL/g")
echo "launching... $EXEC"
exec $EXEC
