Skip to content

Instantly share code, notes, and snippets.

@meowtochondria
Created August 12, 2024 17:32
Show Gist options
  • Save meowtochondria/8b99b8fbf364eec41ef664138b43b2f5 to your computer and use it in GitHub Desktop.
Save meowtochondria/8b99b8fbf364eec41ef664138b43b2f5 to your computer and use it in GitHub Desktop.
Launch or bring wezterm into focus
#!/bin/sh
test -z "$WEZTERM_CONFIG_FILE" && test -f /home/dev/src/scripts/wezterm.lua && export WEZTERM_CONFIG_FILE=/home/dev/src/scripts/wezterm.lua
DESKTOP_FILE='/usr/share/applications/org.wezfurlong.wezterm.desktop'
# get binary name by looking at 'Exec' value in .desktop file.
BIN=$(grep -P '^Exec=' $DESKTOP_FILE | cut -f 2 -d '=' |cut -f 1 -d ' ')
# wezterm does put its name in window title. so wmctrl is not able to find it.
# check if app is already running by looking for its PID
PID=$(pgrep "$BIN")
# launch app if pid does not exist
test -z "$PID" && /usr/bin/gio launch "$DESKTOP_FILE" && exit 0
# control would reach here only if an instance of app is running, which means PID would not be empty.
# grab hex identity of the window using pid and wmctrl
WINDOW_IDENTITY=$(/usr/bin/wmctrl -lp | grep "$PID" | cut -f 1 -d ' ')
# raise the window using the identity.
/usr/bin/wmctrl -i -a "$WINDOW_IDENTITY"
@meowtochondria
Copy link
Author

meowtochondria commented Aug 12, 2024

  1. You'll need to install wmctrl
  2. Make sure line #3 points to correct file for you. You can remove this line if you use config in a place that wezterm automatically loads.
  3. DESKTOP_FILE in line #5 is default path when you install using .deb file. In case your installation method is different, change it accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment