Open application in workspace Hyprland
Tuesday 10 September 2024

I wanted to open couple applications when I start my machine. and I want each application to open in specific workspace.

the following ruby script will do that. it will run the command and wait for a window to appear, then moves it to the workspace. it’ll try to do that 50 times and waits for 0.1 second between tries. so if the command is wrong for example the script will exit after 5 seconds anstead of stucking.

 1#!/usr/bin/env ruby
 2
 3require 'json'
 4
 5command = ARGV.shift
 6workspace = ARGV.shift
 7
 8pid = Process.fork
 9if pid.nil?
10  exec command
11else
12  Process.detach(pid)
13
14  50.times do
15    windows = JSON.parse `hyprctl clients -j`
16    found = windows.any? { |win| win['pid'] == pid }
17    if found
18      `hyprctl dispatch movetoworkspace "#{workspace},pid:#{pid}"`
19      break
20    end
21
22    sleep 0.1
23  end
24end

I use it in my hyprland config as follows:

exec-once = open-in-workspace emacs 1
exec-once = open-in-workspace kitty 2
exec-once = open-in-workspace google-chrome-stable 3
exec-once = open-in-workspace slack 4

I was doing these steps manually everytime I start my machine in the morning. this script will save me a bit of effort.

Edit

turns out Hyprland has a syntax to open the application in a specific workspace. knew it from an issue on github.

exec-once = [workspace 1 silent] emacs
exec-once = [workspace 2 silent] kitty
exec-once = [workspace 3 silent] google-chrome-stable
exec-once = [workspace 4 silent] slack

hyprland linux archlinux

See Also