Skip to content

Instantly share code, notes, and snippets.

@shaneutt
Created November 2, 2025 22:33
Show Gist options
  • Select an option

  • Save shaneutt/b910ae93319957498a0e2193bb9bedbb to your computer and use it in GitHub Desktop.

Select an option

Save shaneutt/b910ae93319957498a0e2193bb9bedbb to your computer and use it in GitHub Desktop.
xfce-app-switcher.sh
#!/bin/bash
# ------------------------------------------------------------------------------
# xfce-app-switcher.sh
#
# A script that lets you pin both launching and switching to an application
# using the same hotkey, similar to what you get by default in other
# environments like i3 or Cinnamon.
#
# To use: Open XFCE Keyboard settings and create a shortcut that runs this
# script with your desired program as an argument.
# ------------------------------------------------------------------------------
if [ $# -eq 0 ]; then
echo "Usage: $0 <program_name> [args...]" >&2
echo "Example: $0 firefox" >&2
echo "Example: $0 flatpak run firefox" >&2
exit 1
fi
set -euox pipefail
COMMAND="$@"
if [ "$1" = "flatpak" ] && [ "$2" = "run" ]; then
PROGRAM="${3##*.}"
else
PROGRAM="$1"
fi
if ! command -v wmctrl &> /dev/null; then
echo "wmctrl is not installed. Install it with: sudo dnf install wmctrl" >&2
exit 1
fi
set +e pipefail
wmctrl -x -a "$PROGRAM"
FAILED=$?
set -e
if [ $FAILED -eq 0 ]; then
echo "Switched to $PROGRAM"
else
echo "No window found for $PROGRAM, launching it..."
nohup $COMMAND >/dev/null 2>&1 &
disown
RETRIES=10
ATTEMPTS=0
while [ $ATTEMPTS -lt $RETRIES ]; do
ATTEMPTS=$((ATTEMPTS + 1))
if wmctrl -lx | grep -i "$PROGRAM" >/dev/null 2>&1; then
echo "$PROGRAM launched successfully"
wmctrl -x -a "$PROGRAM"
exit 0
fi
sleep 1
done
echo "Error: $PROGRAM was launched but window did not appear after ${RETRIES} seconds" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment