-
-
Save troyp/a94c270bdfee22ba7376267745218b9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Usage: ww -f "window class filter" -c "run if not found" | |
| # Usage: ww -fa "window title filter" -c "run if not found" | |
| ## Find and contribute to a more updated version https://github.com/academo/ww-run-raise | |
| POSITIONAL=() | |
| while [[ $# -gt 0 ]]; do | |
| key="$1" | |
| case $key in | |
| -c | --command) | |
| COMMAND="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -f | --filter) | |
| FILTERBY="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| -fa | --filter-alternative) | |
| FILTERALT="$2" | |
| shift # past argument | |
| shift # past value | |
| ;; | |
| *) # unknown option | |
| POSITIONAL+=("$1") # save it in an array for later | |
| shift # past argument | |
| ;; | |
| esac | |
| done | |
| set -- "${POSITIONAL[@]}" # restore positional parameters | |
| SCRIPT_CLASS_NAME=$( | |
| cat <<EOF | |
| function kwinactivateclient(targetApp) { | |
| var clients = workspace.clientList(); | |
| for (var i=0; i<clients.length; i++) { | |
| client = clients[i]; | |
| if (client.resourceClass == targetApp){ | |
| workspace.activeClient = client; | |
| break; | |
| } | |
| } | |
| } | |
| kwinactivateclient('REPLACE_ME'); | |
| EOF | |
| ) | |
| SCRIPT_CAPTION=$( | |
| cat <<EOF | |
| function kwinactivateclient(targetApp) { | |
| var clients = workspace.clientList(); | |
| for (var i=0; i<clients.length; i++) { | |
| client = clients[i]; | |
| if (client.caption == targetApp){ | |
| workspace.activeClient = client; | |
| break; | |
| } | |
| } | |
| } | |
| kwinactivateclient('REPLACE_ME'); | |
| EOF | |
| ) | |
| CURRENT_SCRIPT_NAME=$(basename $0) | |
| # ensure the script file exists | |
| function ensure_script { | |
| if [ ! -f SCRIPT_PATH ]; then | |
| if [ ! -d "$SCRIPT_FOLDER" ]; then | |
| mkdir -p "$SCRIPT_FOLDER" | |
| fi | |
| if [ "$1" == "class" ]; then | |
| SCRIPT_CONTENT=${SCRIPT_CLASS_NAME/REPLACE_ME/$2} | |
| else | |
| SCRIPT_CONTENT=${SCRIPT_CAPTION/REPLACE_ME/$2} | |
| fi | |
| echo "$SCRIPT_CONTENT" >"$SCRIPT_PATH" | |
| fi | |
| } | |
| if [ -z "$FILTERBY" ] && [ -z "$FILTERALT" ]; then | |
| echo You need to specify a window filter. Either by class -f or by title -fa | |
| exit 1 | |
| fi | |
| IS_RUNNING=$(pgrep -o -a -f "$COMMAND" | grep -v "$CURRENT_SCRIPT_NAME") | |
| if [ -n "$IS_RUNNING" ] || [ -n "$FILTERALT" ]; then | |
| SCRIPT_FOLDER="$HOME/.wwscripts/" | |
| SCRIPT_NAME=$(echo "$FILTERBY$FILTERALT" | md5sum | head -c 32) | |
| SCRIPT_PATH="$SCRIPT_FOLDER$SCRIPT_NAME" | |
| if [ -n "$FILTERBY" ]; then | |
| ensure_script class $FILTERBY | |
| else | |
| ensure_script caption $FILTERALT | |
| fi | |
| #SCRIPT_NAME="ww$RANDOM" | |
| SCRIPT_NAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) | |
| # install the script | |
| ID=$(dbus-send --session --dest=org.kde.KWin --print-reply=literal /Scripting org.kde.kwin.Scripting.loadScript "string:$SCRIPT_PATH" "string:$SCRIPT_NAME" | awk '{print $2}') | |
| # run it | |
| dbus-send --session --dest=org.kde.KWin --print-reply=literal "/$ID" org.kde.kwin.Scripting.run | |
| dbus-send --session --dest=org.kde.KWin --print-reply=literal "/$ID" org.kde.kwin.Script.run | |
| # stop it | |
| dbus-send --session --dest=org.kde.KWin --print-reply=literal "/$ID" org.kde.kwin.Scripting.stop | |
| dbus-send --session --dest=org.kde.KWin --print-reply=literal "/$ID" org.kde.kwin.Script.stop | |
| # uninstall it | |
| dbus-send --session --dest=org.kde.KWin --print-reply=literal /Scripting org.kde.kwin.Scripting.unloadScript "string:$SCRIPT_NAME" | |
| elif [ -n "$COMMAND" ]; then | |
| $COMMAND & | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment