Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "--process" option to override searched for process #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,42 @@ ww only works in KDE. It works in X11 and Wayland.

if you want to raise or jump to an open firefox window:

`ww -f firefox -c firefox`
```bash
ww -f firefox -c firefox
```

if you want to raise or jump to an app with an specific class:

`ww -f kitty.terminal -c 'kitty --class kitty.terminal'`
```bash
ww -f kitty.terminal -c 'kitty --class kitty.terminal'
```

Note: In this example `kitty` allows you to pass the `class` option that sets the window class.
This is a kitty feature, not a ww feature.

If you want to raise any window that matches a title. JS regexp allowed:

`ww -fa 'Zoom meeting'`
```bash
ww -fa 'Zoom meeting'
```

Sometimes the command to start a program only indirectly runs the actual
program, making it not directly findable, such as when using flatpaks. Then you
can use `-p` to separate the string used when searching for the application:
```bash
ww -f steam -p steam -c 'flatpak run --branch=stable --arch=x86_64 --command=/app/bin/steam --file-forwarding com.valvesoftware.Steam'
```

(The value specified to `-p` is directly passed along to `pgrep`, see "How does
it work?" below.)

## Paramaters:
```
-h --help show this help
-f --filter filter by window class
-fa --filter-alternative filter by window title (caption)
-c --command command to check if running and run if no process is found
-p --process overide the process name used when checking if running, defaults to --command
```

# Create shortcuts
Expand All @@ -53,7 +70,7 @@ Internally ww uses 2 main things to work: `pgrep` and "on demand" KWin scripts.

When you run, for example `ww -f firefox c -firefox`, ww tries to find a process running with the exact command:

`pgrep -o -f firefox`
`pgrep -o -a -f firefox`

This detects if the application is running or not.

Expand Down
16 changes: 13 additions & 3 deletions ww
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# Usage: ww -f "window class filter" -c "run if not found"
# Usage: ww -fa "window title filter" -c "run if not found"
# Usage: ww -f "window class filter" -c "run if not found" [-p "process name"]
# Usage: ww -fa "window title filter" -c "run if not found" [-p "process name"]

TOGGLE="false"
POSITIONAL=()
Expand All @@ -13,6 +13,11 @@ while [[ $# -gt 0 ]]; do
shift # past argument
shift # past value
;;
-p | --process)
PROCESS="$2"
shift # past argument
shift # past value
;;
-f | --filter)
FILTERBY="$2"
shift # past argument
Expand All @@ -39,6 +44,10 @@ while [[ $# -gt 0 ]]; do
esac
done

if [ -z "$PROCESS" ]; then
PROCESS=$COMMAND
fi

set -- "${POSITIONAL[@]}" # restore positional parameters

if [ -n "$HELP" ]; then
Expand All @@ -52,6 +61,7 @@ Paramaters:
-fa --filter-alternative filter by window title (caption)
-t --toggle also minimize the window if it is already active
-c --command command to check if running and run if no process is found
-p --process overide the process name used when checking if running, defaults to --command
EOF
exit 0
fi
Expand Down Expand Up @@ -106,7 +116,7 @@ if [ -z "$FILTERBY" ] && [ -z "$FILTERALT" ]; then
exit 1
fi

IS_RUNNING=$(pgrep -o -a -f "$COMMAND" | grep -v "$CURRENT_SCRIPT_NAME")
IS_RUNNING=$(pgrep -o -a -f "$PROCESS" | grep -v "$CURRENT_SCRIPT_NAME")

if [ -n "$IS_RUNNING" ] || [ -n "$FILTERALT" ]; then

Expand Down
148 changes: 148 additions & 0 deletions ww_kde6
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/bin/bash
# Usage: ww -f "window class filter" -c "run if not found" [-p "process name"]
# Usage: ww -fa "window title filter" -c "run if not found" [-p "process name"]

TOGGLE="false"
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-c | --command)
COMMAND="$2"
shift # past argument
shift # past value
;;
-p | --process)
PROCESS="$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
;;
-t | --toggle)
TOGGLE="true"
shift # past argument
;;
-h | --help)
HELP="1"
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

if [ -z "$PROCESS" ]; then
PROCESS=$COMMAND
fi

if [ -n "$HELP" ]; then
cat <<EOF
ww. Utility to raise or jump an applications in KDE. It interacts with KWin using KWin scripts and it is compatible with X11 and Wayland

Paramaters:

-h --help show this help
-f --filter filter by window class
-fa --filter-alternative filter by window title (caption)
-t --toggle also minimize the window if it is already active
-c --command command to check if running and run if no process is found
-p --process overide the process name used when checking if running, defaults to --command
EOF
exit 0
fi

SCRIPT_TEMPLATE=$(
cat <<EOF
function kwinactivateclient(clientClass, clientCaption, toggle) {
var clients = workspace.windowList();
var compareToCaption = new RegExp(clientCaption || '', 'i');
var compareToClass = clientClass;
var isCompareToClass = clientClass.length > 0
for (var i = 0; i < clients.length; i++) {
var client = clients[i];
var classCompare = (isCompareToClass && client.resourceClass == compareToClass)
var captionCompare = (!isCompareToClass && compareToCaption.exec(client.caption))
if (classCompare || captionCompare) {
if (workspace.activeWindow != client) {
workspace.activeWindow = client;
} else if (toggle) {
client.minimized = true;
}
break;
}
}
}
kwinactivateclient('CLASS_NAME', 'CAPTION_NAME', TOGGLE);
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
SCRIPT_CONTENT=${SCRIPT_TEMPLATE/CLASS_NAME/$1}
SCRIPT_CONTENT=${SCRIPT_CONTENT/CAPTION_NAME/$2}
SCRIPT_CONTENT=${SCRIPT_CONTENT/TOGGLE/$3}
#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 "$PROCESS" | grep -v "$CURRENT_SCRIPT_NAME")

if [ -n "$IS_RUNNING" ] || [ -n "$FILTERALT" ]; then

# trying for XDG_CONFIG_HOME first
SCRIPT_FOLDER_ROOT=$XDG_CONFIG_HOME
if [[ -z $SCRIPT_FOLDER_ROOT ]]; then
# fallback to the home folder
SCRIPT_FOLDER_ROOT=$HOME
fi

SCRIPT_FOLDER="$SCRIPT_FOLDER_ROOT/.wwscripts/"
SCRIPT_NAME=$(echo "$FILTERBY$FILTERALT" | md5sum | head -c 32)
SCRIPT_PATH="$SCRIPT_FOLDER$SCRIPT_NAME"
ensure_script "$FILTERBY" "$FILTERALT" "$TOGGLE"

# install the script
ID=$(dbus-send --session --print-reply --dest=org.kde.KWin /Scripting org.kde.kwin.Scripting.loadScript "string:$SCRIPT_PATH" "string:$SCRIPT_NAME" | awk 'END {print $2}')
# run the script
dbus-send --session --print-reply --dest=org.kde.KWin /Scripting/Script$ID org.kde.kwin.Script.run >/dev/null 2>&1
dbus-send --session --print-reply --dest=org.kde.KWin /Scripting/Script$ID org.kde.kwin.Scripting.run >/dev/null 2>&1
# stop the script
dbus-send --session --print-reply --dest=org.kde.KWin /Scripting/Script$ID org.kde.kwin.Script.stop >/dev/null 2>&1
dbus-send --session --print-reply --dest=org.kde.KWin /Scripting/Script$ID org.kde.kwin.Scripting.stop >/dev/null 2>&1

# uninstall the script
dbus-send --session --print-reply --dest=org.kde.KWin /Scripting org.kde.kwin.Scripting.unloadScript "string:$SCRIPT_NAME" >/dev/null 2>&1
elif [ -n "$COMMAND" ]; then
$COMMAND &
fi