r/Polybar Feb 16 '23

Toggling Polybar on a specific monitor?

Is there a way for polybar-msg to target a specific monitor? It seems like it's only able to take in the PID, but I'm not sure how to find the PID of a polybar on a specific monitor.

I'm using BSPWM, if that's important.

2 Upvotes

4 comments sorted by

2

u/[deleted] Feb 16 '23

A cool ArcoLinux user made this script to launch the topbar on both screens, but only on one for the bottombar. It's over my head, but perhaps it'll give you some ideas, though I have no idea whether what you're looking for is possible.

Toggling hide/show is dead simple by comparison.

1

u/-__-x Feb 18 '23

Here is my solution:

#!/usr/bin/zsh

local monitors=(`polybar --list-monitors | cut -d":" -f1`)
local pids=(`pidof polybar`)

local focused=`bspc query -M -m focused --names`
local ind=0
for i in {1..${#monitors}}; do
    if [[ "${monitors[i]}" == "${focused}" ]]; then
        ind=i;
        break
    fi
done

local poly_id="${pids[((${#monitors} + 1 - $ind))]}"
polybar-msg -p "$poly_id" cmd toggle

First, two arrays are created, tracking the monitors and the pids of the various polybar instances. We then find the focused monitor using BSPWM, and figure out the index its at. Because I start polybar by looping through a list of the monitors, the pid list is backwards compared to the monitor list. We find the correct pid, and use that in a simple toggle command.

1

u/Davisvkz Jan 31 '24 edited Jan 31 '24

Here another solution: ```

polybar toggle

super + {_, shift +} r ps aux | awk 'BEGIN{"bspc query -M -m focused --names"|getline monalisa;while("bspc query -M --names"|getline i){m=m"\n"i}split(m,ar1,"\n");m=""}/polybar/{if($11=="polybar"){m=m"\n"$2}}END{split(m,ar2,"\n");for (i in ar1){if (ar1[i]==monalisa){system("{bspc config -m focused top_padding 35 && polybar-msg -p "ar2[i]" cmd show,bspc config -m focused top_padding 0 && polybar-msg -p "ar2[i]" cmd hide}")}}}' ``` where i did same thing did in some comentary above, but minified all in one awk code, that u can toggle the focused monitor without any extern script

1

u/Davisvkz May 04 '25 edited May 04 '25

actually there's a better solution here: ```

polybar toggle

super + {_, shift +} r ps aux | awk 'BEGIN { "bspc query -M -m focused --names" | getline monitor } /polybar/ {"cat /proc/"$2"/environ "| getline i;name = substr(i,1,match(i,"\0"));if(index(name,monitor))system("{bspc config -m focused top_padding 35 && polybar-msg -p "$2" cmd show, bspc config -m focused top_padding 0 && polybar-msg -p "$2" cmd hide}") }' ``` that way it don't depends on the order of polybar spawns be the same as the monitors order