-
-
Save academo/613c8e2caf970fabd260cfd12820bde3 to your computer and use it in GitHub Desktop.
This file contains 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 |
Maybe they changed it in KDE, I too do not know.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Ashark
Yes I can reproduce this.
I can see the
ww
script showing up briefly and disappearing from qdbusviewer session bus. I commented the uninstall line and this still happened so the uninstall method is not what removes it from the dbus list.I commented stop and the id didn't remain there either so the stop is not the one removing it from the dbus list either.
My memory fails me since I did this some time ago and I am not working with Kwin scripts anymore but I remember there was a place where I could see the list of scripts installed via the dbus and if stop and unload were not called, the list kept growing and growing. Perhaps this list disappeared from newer kde versions?