Skip to content

Instantly share code, notes, and snippets.

@elundmark
Created October 27, 2013 14:43
Show Gist options
  • Save elundmark/7183030 to your computer and use it in GitHub Desktop.
Save elundmark/7183030 to your computer and use it in GitHub Desktop.
#!/bin/bash
terminalcmd="lxterminal"
# already running
[[ "$(pgrep -x dmenu)" ]] && exit 0
# delete unsafe old shell file
[[ -f "/tmp/dmenu_run_bash.sh" ]] && rm "/tmp/dmenu_run_bash.sh"
# my default first listed apps
MYFAVS="""firefox
banshee
vlc
gpodder
subl
thunderbird
gimp
"""
if [[ ! -z "$1" ]] && [[ "$1" = "history" ]] ; then
# get full history
RECENTS="$(tac $HOME/.dmenu_history)"$'\n'
CMD=$(echo "$RECENTS" | dmenu -l 20 -nf "#EEEEEE" -nb "#00000A" -sb "#527EAC")
else
# 3 most recent commands, see history
RECENTS="$(tac $HOME/.dmenu_history | head -n 3)"$'\n'
# dmenu command
CMD=$(echo "$RECENTS""$MYFAVS""`dmenu_path`" | dmenu -l 10 -nf "#EEEEEE" -nb "#00000A" -sb "#527EAC")
fi
# cache origial command for history
ORGCMD="$CMD"
# exit if no cmd was given (esc)
[[ ! "$ORGCMD" ]] && exit 1
# detect terminal app
FIRST_LETTER="`echo -n "$CMD" | head -c 1`"
# if terminal cmd remove initial ; from $CMD
[[ "$FIRST_LETTER" = ";" ]] && CMD="${CMD#';'}"
# set PROg to be the first word
PROG=$(echo -n "$CMD" | cut -f1 -d ' ')
# get the full path for PROG
PROGPATH="$(which $PROG)"
if [[ ! $? -eq 0 ]] ; then
# if which failed set the app path to ""
PROGPATH=""
PROGDIR=""
else
PROGDIR="$(dirname "$PROGPATH")""/"
fi
# history check
IN_HIST=0
while read -r last_cmd; do
# if current cmd is in the recent history then do not store
if [[ "$last_cmd" = "$ORGCMD" ]] ; then
IN_HIST=1
break
fi
done <<< "$RECENTS"
if [[ $IN_HIST -eq 0 ]] ; then
# not in history, store it now, echo for debugging
echo "$ORGCMD" | tee -a $HOME/.dmenu_history
fi
# terminal app
if [[ "$FIRST_LETTER" = ";" ]] ; then
# if the first letter is ; we create a temp file and launch that in a terminal
# set to include aliases
# source my aliases
# sleep to let the terminal window adjust
# call bash at the end to prevent the window from closing
echo $'#!/bin/bash\n\nshopt -s expand_aliases\n\nsource ~/.bash_aliases\n\nsleep 0.2s\n\n'"$PROGDIR""$CMD"$'\n\n'"/bin/bash" > /tmp/dmenu_run_bash.sh
$terminalcmd --working-directory="$HOME" --command="/bin/bash /tmp/dmenu_run_bash.sh" &
else
# if no instance of the app has been started, launch one now
if [ -z "`wmctrl -lx | grep -i "$PROG"`" ] ; then
$CMD &
else
# search for existing app on any desktop and move it to the current desktop
MATCHING_WINDOWS="`wmctrl -lx | grep -i $PROG`"
while read -r WINDOW; do
# skip matches like a firefox webpage or open document with the cmd in the title
if [[ ! "$PROG" = "firefox" && "$WINDOW" =~ Navigator\.Firefox ]] \
|| [[ ! "$PROG" = "subl" && "$WINDOW" =~ sublime_text\.Sublime_text ]] ; then
continue
else
APP_ON_ANY_DESK="`echo -n "$MATCHING_WINDOWS" | cut -d ' ' -f 1`"
break
fi
done <<< "$MATCHING_WINDOWS"
# if a valid window was found
if [[ ! -z $APP_ON_ANY_DESK ]] ; then
wmctrl -i -R $APP_ON_ANY_DESK
# we think this is what we want
else
$CMD &
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment