#!/bin/bash
# Get Double Click Selection and run an external application with it

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

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

# Simulate double click
xte "mouseclick 1" "sleep $DCLICK_WAIT" "mouseclick 1"

# Get selection from primary X clipbloard
SELECTION="$(xsel -p | sed "s/^[[:space:]]*//; s/[[:space:]]*$//")"

# Substitue @SEL@ by the selected string
EXEC=$(echo $* | sed "s/@SEL@/$SELECTION/g")
echo "launching... $EXEC"
$EXEC
exit $?