Files
scwrypts/zsh/system/i3/launch-or-show
T

119 lines
3.1 KiB
Bash
Raw Normal View History

2022-08-22 16:04:03 -06:00
#!/bin/zsh
2023-02-21 18:44:27 -07:00
DEPENDENCIES+=(
i3-msg
2022-08-22 16:04:03 -06:00
xdotool
xrandr
)
2023-02-21 18:44:27 -07:00
REQUIRED_ENV+=()
use system/desktop/notify
CHECK_ENVIRONMENT
2022-08-22 16:04:03 -06:00
#####################################################################
LAUNCH_OR_SHOW() {
2023-02-21 18:44:27 -07:00
INFO $@
2022-08-22 16:04:03 -06:00
local USAGE="
usage: <path-executable> [client-class] [...options...]
options
-c, --client <string> if different from the executable name, xprop CLIENT_CLASS
-s, --scale <value> (default: 0.8 or 0.5 if screen width >3000px)
-x, --x-offset <value> (default: 0.0)
-y, --y-offset <value> (default: 0.0)
-a, --always-launch invoke executable even if client-class exists
-n, --no-resize don't resize the window (ignores -sxy flags)
-h, --help print this message and exit
Makes it easy to bind appications to key shortcuts without having to
spin up redundant instances or cycle through the scratchpad queue.
2023-02-21 18:44:27 -07:00
Performs a variety of tasks based on states:
1) starts and application
2) adds all instances of the specified application to the scratchpad
3) (toggle) hides all visible instances
4) (toggle) shows all scratchpad-hidden instances
2022-08-22 16:04:03 -06:00
"
local APPLICATION CLIENT_CLASS
local XFFSET=0.0
local YFFSET=0.0
local SCALE=0.8
[[ $(xrandr | grep primary | awk '{print $4;}' | sed 's/x.*//') -gt 3000 ]] \
&& SCALE=0.5
local ALWAYS_LAUNCH=0
local RESIZE=1
while [[ $# -gt 0 ]]
do
case $1 in
-c | --client ) CLIENT_CLASS="$2"; shift 1 ;;
-x | --x-offset ) XFFSET=$2; shift 1 ;;
-y | --y-offset ) YFFSET=$2; shift 1 ;;
-s | --scale ) SCALE=$2; shift 1 ;;
-a | --always-launch ) ALWAYS_LAUNCH=1 ;;
-n | --no-resize ) RESIZE=0 ;;
2023-02-21 18:44:27 -07:00
-h | --help ) USAGE; exit 0 ;;
2022-08-22 16:04:03 -06:00
* )
[ ! $APPLICATION ] && APPLICATION="$1" \
2023-02-21 18:44:27 -07:00
|| ERROR "extra positional argument '$1'"
2022-08-22 16:04:03 -06:00
esac
shift 1
done
2023-02-21 18:44:27 -07:00
[ ! $APPLICATION ] && ERROR 'path-executable required'
2022-08-22 16:04:03 -06:00
[ ! $CLIENT_CLASS ] && CLIENT_CLASS=$APPLICATION
[ $APPLICATION ] && {
__CHECK_DEPENDENCY $APPLICATION || {
2023-02-21 18:44:27 -07:00
ERROR "$APPLICATION is not installed"
NOTIFY "ERROR: $APPLICATION not found"
2022-08-22 16:04:03 -06:00
}
}
2023-02-21 18:44:27 -07:00
ERROR_CHECK
2022-08-22 16:04:03 -06:00
local LAUNCH_APP=$ALWAYS_LAUNCH
2023-02-21 18:44:27 -07:00
STATUS "looking for window process ids"
2022-08-22 16:04:03 -06:00
xdotool search --class $CLIENT_CLASS || LAUNCH_APP=1
[[ $LAUNCH_APP -eq 1 ]] && {
2023-02-21 18:44:27 -07:00
STATUS 'launching application'
2022-08-22 16:04:03 -06:00
i3-msg "exec --no-startup-id $APPLICATION;"
sleep .5
}
2023-02-21 18:44:27 -07:00
STATUS 'getting target window size'
2022-08-22 16:04:03 -06:00
WINDOW_SIZE=$(\
xrandr \
| grep 'connected primary' \
| sed 's/.*connected primary \([^x]*\)x\([^+]*\).*/\1 \2/' \
| awk -v f=$SCALE -v x=$XFFSET -v y=$YFFSET \
'{print int($1*f+x)," ",int($2*f+y);}'\
)
2023-02-21 18:44:27 -07:00
INFO "window size: $WINDOW_SIZE"
2022-08-22 16:04:03 -06:00
2023-02-21 18:44:27 -07:00
STATUS 'moving window to scratchpad'
2022-08-22 16:04:03 -06:00
i3-msg "[class=$CLIENT_CLASS] move scratchpad"
[[ $RESIZE -eq 1 ]] \
2023-02-21 18:44:27 -07:00
&& STATUS 'resizing window' \
2022-08-22 16:04:03 -06:00
&& i3-msg "[class=$CLIENT_CLASS] resize set $WINDOW_SIZE"
2023-02-21 18:44:27 -07:00
STATUS 'pulling window from scratchpad to foreground'
2022-08-22 16:04:03 -06:00
i3-msg "[class=$CLIENT_CLASS] scratchpad show"
2023-02-21 18:44:27 -07:00
STATUS 'moving window to center of current screen'
2022-08-22 16:04:03 -06:00
i3-msg "[class=$CLIENT_CLASS] move position center"
}
#####################################################################
LAUNCH_OR_SHOW $@