Services menu for selections

Hi!

This is based on some rep extension to sawfish by Stephen Farrell ( see sawfish.endorphin.org/SawfishWik … shServices ) which is supposedly based on something in the NeXTStep environment.

Somebody brought a similar idea on #fvwm recently, so I decided to have a go.

You select something with your mouse (or in your favorite browser/editor whatever) and then fire up this menu and decide what to do with it:

# Similar to the services in NeXTStep, and the according sawfish function:

# Change this function to add/modify services:
DestroyFunc ServicesAddMine
AddToFunc ServicesAddMine
+ I ServicesAdd    Echo                           "Exec exec " \
                                                  "|xmessage -file -"
+ I ServicesAdd    Webbrowser                     "Exec exec " \
                                                  "|xargs wwwt "
+ I ServicesAddWeb "Google: I'm Feeling Lucky"    "http://www.google.com/search?q=" \
                                                  "&btnI=OK"
+ I ServicesAddWeb "Google"                       "http://www.google.com/search?q="
+ I ServicesAddWeb "Google msgid"                 "http://www.google.de/groups?ie=UTF-8&oe=UTF-8&as_umsgid="\
                                                  "&lr=&hl=de"
+ I ServicesAddWeb "Tiny Url"                     "http://tinyurl.com/create.php?url="
+ I ServicesAddWeb "42.pl/url"                    "http://42.pl/url/index.php?url="
+ I ServicesAddWeb "Freshmeat"                    "http://freshmeat.net/search/?q="
+ I ServicesAddWeb "Woerterbuch"                  "http://mr-check.xipolis.net/Mrcheck.php?SB=" \
                                                  "&CID=tanto1"
+ I ServicesAdd    "Ding"                         "Exec exec " \
                                                  "|xargs ding -R "
+ I ServicesAddWeb "Leodict"                      "http://dict.leo.org/?lang=en&where=0&search="
+ I ServicesAddWeb "Jargon"                       "http://www.google.com/search?q=" \
                                                  " site:www.catb.org"
+ I ServicesAdd    "Unix man"                     "Exec exec " \
                                                  "|xargs runinscreen man "
+ I ServicesAdd    "Send mail"                    "Exec exec " \
                                                  "|xargs runinscreen mutt "

#  0: title, 1: prefix, 2: postfix
DestroyFunc ServicesAdd
AddToFunc ServicesAdd
+ I PipeRead `echo AddToFunc ServicesMenuContentGenerateCached I AddToMenu ServicesMenuMenu "\\\"$0\\\"" "$1 xclip -o $2"`

#  0: title, 1: prefix, 2: postfix
DestroyFunc ServicesAddWeb
AddToFunc ServicesAddWeb
+ I ServicesAdd "$0" "Exec exec " "|xargs -I{} wwwt '$1{}$2'"

# Function to cache service menu contents, in order to get rid of too many PipeReads
DestroyFunc ServicesMenuContentGenerateCached

# Generate the cache function
Function ServicesAddMine

# Re-generate the menu and pop it up
DestroyFunc ServicesMenu
AddToFunc ServicesMenu
+ I DestroyMenu recreate ServicesMenuMenu
+ I PipeRead 'echo SetEnv FVWMSELECTION \\\'`xclip -o`\\\''
+ I PipeRead 'echo AddToMenu ServicesMenuMenu \\\"Services for Selection: $FVWMSELECTION\\\" Title'
+ I AddToMenu ServicesMenuMenu "Restart" Restart
+ I Function ServicesMenuContentGenerateCached
+ I PopUp ServicesMenuMenu
+ I UnsetEnv FVWMSELECTION

This uses the program xclip to get the contents of the primary selection. Note that some programs use the secondary selection or the clipboard, so this might not always work as expected. It will also break if the selection should change while the menu is displayed, but since fvwm grabs the pointer while displaying menus, this should not be easily possible.

You can easily modify the ServicesAddMine function to get your own services. “xclip -o” will be inserted between $1 and $2.

I also use two shellscripts in my example services, and I include them here for convenience:

www fires up a page in a running browser if firefox or opera are running, otherwise starts w3m. If started by a symlink wwwf it will open a file in the local directory, the “wwwt” and “wwwtf” variants are non-functional with firefox (as it always starts a new tab here) but should normally fire up the page in a new tab.

#!/bin/sh

operarunning=""
myname=`basename $0`
if [ $myname = wwwt -o $myname = wwwtf ]
then
    OPTAB="-newpage"
    FIRTAB=", new-tab"
else
    OPTAB=""
    FIRTAB=""
fi

if [ $myname = wwwf -o $myname = wwwtf ]
then
    prepend='file://'`pwd`'/'
else
    prepend=""
fi

/bin/pidof opera >/dev/null && operarunning=true
for i in "$@"
do
        i=${prepend}$i
        if [ ${DISPLAY} ] ; then
            if [ "$operarunning" ]
            then
                    opera $OPTAB "$i"
            else
                    /usr/lib/mozilla-firefox/mozilla-firefox-xremote-client "openURL(${i}${FIRTAB})" || gnome-moz-remote "$i"|| netscape "$i" &
#                    mozilla-xremote-client "openURL(${i}${FIRTAB})" || gnome-moz-remote "$i"|| netscape "$i" &
            fi
        else
            w3m "$@"
        fi
done

‘runinscreen’ tries to find a running screen session and adds a new program to it, otherwise starts a new screen session in ‘myterm’ (a symlink to xterm):

#!/bin/sh
# SCREENBASE=/var/run/screen
SCREENBASE=/tmp/screens
LOGNAME=$(id -n -u)
screen -wipe >/dev/null 2>&1
firstscreen=$(/bin/ls $SCREENBASE/S-$LOGNAME/*|head -1)
if [ "$firstscreen" ]
then
    STY=$(basename $firstscreen)
    export STY
fi

if [ "$STY" ]
then
    xmessage -center -buttons "" -timeout 5 "Adding command \"$*\" to your screen." &
    rxvt -e screen -t "$1" $@
else
    myterm -name screen -e screen $@ &
fi

I hope somebody finds this useful :slight_smile:

Kind regards
Friedel