Journey's End

Nov 13
2006

A somewhat useful bash script

I am one of those weird people who like to run a full screen X11 in OS X - its just me. This means of course I run into certain problems when I need to copy-n-paste between Aqua and X11.

Copying from Aqua into X11 works fine, but not the other way around. This means its extremely trouble some when I need to open a URL from IRC since I use irssi for IRC, running inside screen over ssh.

And thus the following bash script was born to let me select some text, press a key combination in fluxbox and open up safari with the selection, or to perform a search on wikipedia or google.

!/bin/bash

MAX_LEN=32;
NON_URL_BUTTONS="google:1,wiki:2,define:3,cancel:99";
URL_BUTTONS="safari:0,cancel:99";

sel=`/Users/steve/usr/bin/xsel`;

if [ ${#sel} -eq 0 ]; then
    xmessage -buttons ok -default ok -nearmouse "Empty selection!";
    exit;
fi

hasurl=`expr "$sel" : '.*http://.*'`;
if [ $hasurl -gt 0 ]; then
    sel=${sel#*http://};
    sel=${sel%% *};
    sel="http://$sel";
    buttons=$URL_BUTTONS;
else
    buttons=$NON_URL_BUTTONS;
fi

if [ ${#sel} -gt $MAX_LEN ]; then
    label="${sel:0:$MAX_LEN}...";
else
    label=$sel;
fi

xmessage -buttons $buttons -default     `expr "$buttons" : '\(^[a-z]*\)'` -nearmouse "$label";

case $? in
    0)
        url="${sel}";
        ;;
    1)
        url="http://www.google.com/search?q=${sel}";
        ;;
    2) 
        url="http://en.wikipedia.org/wiki/${sel}";
        ;;
    3) 
        url="http://www.google.com/search?q=define:${sel}";
        ;;
    99)
        exit
        ;;
esac

url=${url// /%20};

echo "$url"
open /Applications/Safari.app/ "$url"

xsel used here is by Conrad Parker, and you may change xmessage to gmessage if that is more appealing to you.

Cheers,
Steve

ts=10:14