-
-
Save academo/613c8e2caf970fabd260cfd12820bde3 to your computer and use it in GitHub Desktop.
#!/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 |
@Ashark if you only stop it but don't uninstall it it starts growing the list of scripts in kwinscript. I could not find a way to re-use an existing "installed" script so this is the solution I found to not pollute the kwinscripts list.
@academo For uninstalling, how do i know the script name, if i did not created it?
Can you try with the plasma-interactiveconsole
? Open a dbus viewer. Then run any kwin script in interactiveconsole. Now update the dbus viewer. You will see a new id is resided there (this is a bug, i want to fix this). Now, if you click the stop method for that id, it will disappear.
But you say it remains residing there is not using uninstall method. And the current code of interactiveconsole does not invoke uninstall method.
Can you confirm?
how do i know the script name, if i did not created it?
I don't know :) I never went that far with KWin scripts and the documentation is quite vague. I mostly found things by experimentation and bits from forums here and there.
Then run any kwin script in interactiveconsole. Now update the dbus viewer. You will see a new id is resided there (this is a bug, i want to fix this). Now, if you click the stop method for that id, it will disappear.
Yes I can reproduce this.
But you say it remains residing there is not using uninstall method. And the current code of interactiveconsole does not invoke uninstall method.
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?
Maybe they changed it in KDE, I too do not know.
What is the difference in
org.kde.kwin.Script.stop
andorg.kde.kwin.Scripting.unloadScript
? Is the latter needed? Please see this.