Skip to content

Instantly share code, notes, and snippets.

@3lpsy
Created June 11, 2021 17:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3lpsy/9fc13dae3ba9c176013e3f6457b458e2 to your computer and use it in GitHub Desktop.
Save 3lpsy/9fc13dae3ba9c176013e3f6457b458e2 to your computer and use it in GitHub Desktop.
How to automatically launch apps in certain workspaces in SwayWM

Fun Stuff

I recently figured out how to launch applications in sway on login (or config reload) in specific workspaces. I tried doing this a year or so ago using procedural calls to swaymsg with manual changes to workspaces but it failed due to the delay in launching apps (the workspace would change too quickly). However, I recently figured out how to actually do it by combining swaymsg with assign.

First, you'll want to get the app_id, class or title of window you want to launch. Then you'll want to add it to the your sway config file.

The syntax looks like:

assign MATCH_CRITERIA → WORKSPACE_NAME

The arrow is optional (i beleive). It's kind of annoying you can't alias workspaces so if you use unicode expect to copy and paste. I've listed mine below. The first two are gui applications. However, the last two are command line applications that will be launched with alacritty with a custom title. You'll of course need to figure out a way to launch cli applications with specific titles/classes if you want to match on them.

assign [app_id="firefox"]  → "1: "
assign [title="Joplin"] → "2: "
assign [title="Bandwhich"] → "3: "
assign [title="Bottom"] → "3: "

Next, you'll want to create a script like swayinitapp.sh. This will be exec'd by your sway config at the end to launch the applications via swaymsg.

#!/bin/bash
set -e;

if ! pgrep firefox >/dev/null; then echo "Launching firefox..." && swaymsg "exec /usr/bin/firefox"; fi
if ! pgrep joplin >/dev/null; then echo "Launching joplin..." && swaymsg "exec $HOME/.local/scripts/xjoplin"; fi
if ! pgrep btm >/dev/null; then echo "Launching bottom..." && swaymsg 'exec alacritty -t "Bottom" -e "/usr/bin/btm"'; fi
if ! pgrep bandwhich >/dev/null; then echo "Launching bandwhich..." && swaymsg 'exec alacritty -t "Bandwhich" -e "/usr/bin/bandwhich"'; fi
if [[ ! $(ps aux | grep io.elementary.music >/dev/null) ]]; then echo "Launching music player..." && swaymsg "exec io.elementary.music"; fi

I had some issue with pgrep and io.elementary because I'm a scrub. Also, I actually wrapped /usr/bin/bandwhich is a script that leveraged sudo rules to launch as root due to permission issues but you should get the idea. Joplin is also launched via xwayland via a script as you can see.

You'll of course want to customize it.

Finally, just add the call the the script at the end of your sway config. Make sure the script is executable. You can change "exec" to "exec_always" if you want. The if statements ensure that apps won't be launched twice and you can reload your sway config to automatically restore your desired laucned apps (if closed).

...
# assign stuff
...
exec /path/to/swayinitapp.sh

And that's it. I'm really just posting cause I failed to get this working a while back and was happy to see it was so much easier than I thought.

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