koekeishiya / skhd

Simple hotkey daemon for macOS
MIT License
6.05k stars 204 forks source link

Same shortcut for two different actions #334

Open hamadahiro opened 7 months ago

hamadahiro commented 7 months ago

I have set up a shortcut in skhdrc to toggle the topmost of sketchybar using two different key combinations:

# sketchybar
fn - b : sketchybar --bar topmost=on
fn - m : sketchybar --bar topmost=off

Is it possible to toggle the topmost with just one combination? If topmost is "on", turn it "off", and vice versa?

bustinbung commented 7 months ago

One option would be to use sketchybar --query and jq to find if the value of the setting and decide what to do from there. For example:

# in a script file somewhere (I keep mine in ~/.config/skhd/scripts)

# -- topmost.sh -- #
#!/usr/bin/env bash

# queries sketchybar's bar element and returns a JSON object with properties
# jq filters for the "topmost" property in the object and returns true if the value of topmost is "on"
if $(sketchybar --query bar | jq '.topmost == "on"'); then
  # if topmost is on, turn off
  sketchybar --bar topmost=off
else
  # vice versa
  sketchybar --bar topmost=on
fi

Then in skhdrc:

fn - m : ~/.config/skhd/scripts/topmost.sh