-
Notifications
You must be signed in to change notification settings - Fork 0
/
focusme
105 lines (84 loc) · 2.35 KB
/
focusme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
###Functionality:
###It works by querying the open window names/classes for a string, then:
#- If found it activates/focuses it.
#- If not found it will launch the specified command.
#- If the window is found and it's active it will launch a new one.
#needs xdotool
#apt install xdotool or apk add xdotool
#___
#Launch command in the background and exit
#launch <command>
function launch () {
if [[ ! $1 ]]
then
exit
fi
$1 &>/dev/null &
exit
}
#Search window by given string in name, class etc. and return the resulting window ids or the first|last one
#searchwindow <windowname> <type> [first|last]
function searchwindow () {
# local cmd="xdotool search --onlyvisible $1 2>/dev/null"
if [[ ! $1 || ! $2 ]]
then
echo >&2 "searchwindow usage: searchwindow <windowname> <type> [first|last]";
exit 1
fi
case $3 in
'first')
local out=`xdotool search --onlyvisible --$2 $1 2>/dev/null | grep -m1 '[0-9]\+'`
;;
'last')
local out=`xdotool search --onlyvisible --$2 $1 2>/dev/null | tail -1`
;;
*)
local out=`xdotool search --onlyvisible --$2 $1 2>/dev/null`
;;
esac
#Note: Must double quote the echoed variable in order to preserve multiple lines,
#since echo just prints a space delimited list of lines in a single line
echo "$out"
}
function checkdeps () {
command -v xdotool &>/dev/null || { echo >&2 "Xdotool required! Aborting."; exit 1; }
}
function checkparams () {
if [ $1 -eq 0 ]
then
echo "FocusME activates the given window by name, launch one if none found or the active window matches"
echo "More info: https://github.com/dixflat/FocusME"
echo ""
echo "Usage: focusme <windowname> [command]"
exit
fi
}
###
##
#
checkparams $#
checkdeps
WINDOWID=`searchwindow $1 class first`
WINDOWID_ACTIVE=`xdotool getactivewindow`
WINDOWACTIVENAME=`xdotool getwindowname $WINDOWID_ACTIVE`
WINDOWACTIVECLASS=`xprop -id $WINDOWID_ACTIVE WM_CLASS`
NAME=$1
COMMAND=$2
#No matching window found, launching one
if [[ ! $WINDOWID ]]
then
launch "$COMMAND"
exit
fi
shopt -s nocasematch
#Active window matches NAME or its ID, so launch a new one
if [[ $WINDOWACTIVECLASS =~ "$NAME" || $WINDOWID_ACTIVE == $WINDOWID ]]
then
launch "$COMMAND"
else
#highlight a matching window
xdotool windowactivate $(searchwindow $1 class last)
fi
shopt -u nocasematch
#___