Skip to content

Instantly share code, notes, and snippets.

@emersonmx
Last active February 16, 2017 04:56
Show Gist options
  • Select an option

  • Save emersonmx/c5fac111c8fbc2ddb6d870fd4df67c45 to your computer and use it in GitHub Desktop.

Select an option

Save emersonmx/c5fac111c8fbc2ddb6d870fd4df67c45 to your computer and use it in GitHub Desktop.
Script to open or switch an app
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <app_name> [app]"
exit
fi
app=$1
app_name=$1
if [[ $# -ge 2 ]]; then
app=$2
fi
workspace_number=`wmctrl -d | grep '\*' | cut -d' ' -f 1`
win_list=`wmctrl -lx | grep $app_name | grep " $workspace_number " | awk '{print $1}'`
active_win_id=`xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}' | awk -F', ' '{print $1}'`
if [ "$active_win_id" == "0" ]; then
active_win_id=""
fi
# get next window to focus on, removing id active
switch_to=`echo $win_list | sed s/.*$active_win_id// | awk '{print $1}'`
# if the current window is the last in the list ... take the first one
if [ "$switch_to" == "" ];then
switch_to=`echo $win_list | awk '{print $1}'`
fi
if [[ -n "${switch_to}" ]]
then
(wmctrl -ia "$switch_to") &
else
shift
shift
($app "$@") &
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment