aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/UserScripts/Tak0-Autodispatch.sh
blob: b0607e13cc7cdbd83d6a71c8d77f5c70babe088c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# USAGE:
# 1) Run from terminal:
#    ./dispatch.sh <application_command> <target_workspace_number>
#    Example:
#    ./dispatch.sh discord 2
#
# 2) Call from Hyprland config (in hyprland.conf file):
#    exec-once = /path/to/dispatch.sh <application_command> <target_workspace_number>
#
# Logs are saved in dispatch.log file next to the script.
# If the window doesn't appear or is dispatched incorrectly — info will be there.
#
# Notes:
# - Script waits about ~9 seconds (30 iterations of 0.3 sec) for window to appear.
# - Uses hyprctl and jq, so these tools must be installed.

LOGFILE="$(dirname "$0")/dispatch.log"
# Log file path located next to the script.

APP=$1
# The application command or window class to launch or match.

TARGET_WORKSPACE=$2
# The target workspace number where the window should be moved.

# Check if required arguments are provided.
if [[ -z "$APP" || -z "$TARGET_WORKSPACE" ]]; then
  echo "Usage: $0 <application_command> <target_workspace_number>" >>"$LOGFILE" 2>&1
  exit 1
fi

echo "Starting dispatch of '$APP' to workspace $TARGET_WORKSPACE at $(date)" >>"$LOGFILE"
# Starting the dispatch process and logging the event.
# Avoid early workspace focus issues by switching workspace first.
hyprctl dispatch workspace "$TARGET_WORKSPACE" >>"$LOGFILE" 2>&1
sleep 0.4

# Launch the application in the background and disown it.
$APP &
disown
pid=$!

echo "Launched '$APP' with PID $pid" >>"$LOGFILE"
# Log the launched process ID.
# Wait for the application window to appear (matching window class).
for i in {1..30}; do
  win=$(hyprctl clients -j | jq -r --arg APP "$APP" '
        .[] | select(.class | test($APP;"i")) | .address' 2>>"$LOGFILE")

  if [[ -n "$win" ]]; then
    echo "Found window $win for app '$APP', moving to workspace $TARGET_WORKSPACE" >>"$LOGFILE"
    # Move the window to the target workspace.
    hyprctl dispatch movetoworkspace "$TARGET_WORKSPACE,address:$win" >>"$LOGFILE" 2>&1
    exit 0
  fi
  sleep 0.3
done

echo "ERROR: Window for '$APP' was NOT found or dispatched properly to workspace $TARGET_WORKSPACE at $(date)" >>"$LOGFILE"
# Log error if window was not found or dispatched correctly.
exit 1
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage