Skip to content

Instantly share code, notes, and snippets.

@devidw
Last active October 18, 2024 15:03
Show Gist options
  • Save devidw/ae515eb029bcb1320292cdfd10b7ba9c to your computer and use it in GitHub Desktop.
Save devidw/ae515eb029bcb1320292cdfd10b7ba9c to your computer and use it in GitHub Desktop.
sketchybar spaces
for i in 5 4 3 2 1 6 7 8 9
do
ITEM_ID="space.${i}"
POS=$(node -e "console.log(Number(${i}) > 5 ? 'e' : 'q')")
sketchybar --add space $ITEM_ID $POS \
--set $ITEM_ID space=$i \
--set $ITEM_ID icon=$i \
--set $ITEM_ID icon.font="Chivo Mono:Bold:20" \
--set $ITEM_ID background.height=28 \
--set $ITEM_ID padding_left=5 \
--set $ITEM_ID padding_right=0 \
--set $ITEM_ID icon.padding_left=0 \
--set $ITEM_ID icon.padding_right=5 \
--set $ITEM_ID label.padding_left=0 \
--set $ITEM_ID label.padding_right=0 \
--set $ITEM_ID label.font="sketchybar-app-font:Regular:14.0" \
--set $ITEM_ID label.font.size=20 \
--set $ITEM_ID script="$CONFIG_DIR/spaces.js" \
--set $ITEM_ID click_script="yabai -m space --focus $i" \
--set $ITEM_ID background.corner_radius=10
sketchybar --add item space.${i}.spacer $POS \
--set space.${i}.spacer \
padding_left=2.5 \
padding_right=2.5
done
#!/Users/devidw/.nvm/versions/node/v18.20.2/bin/node
const { execSync } = require("node:child_process")
const SID = Number(process.env.SID)
const SELECTED = process.env.SELECTED === "true"
console.log({
SID,
SELECTED
})
const windows = JSON.parse(execSync("/opt/homebrew/bin/yabai -m query --windows").toString())
const spaceWindows = windows.filter(a => a.space === SID && a["is-hidden"] === false && a["is-minimized"] === false)
const windowIds = spaceWindows.map(a => `win.${SID}.${a.id}`)
// we have to remove the last created group for this space, because there seems
// to be no way to update or to re-add a group, so whenever we focus on a space
// we remove its previous group, sot he create at the end of the script will
// work
if (SELECTED) {
// console.error({ SELECTED })
try {
execSync(`/opt/homebrew/bin/sketchybar --remove win.${SID}`)
} catch (e) {
// console.warn(e)
}
}
/**
* remove out-dated windows
*/
{
let shownOnes = undefined
try {
shownOnes = JSON.parse(execSync(`
/opt/homebrew/bin/sketchybar --query win.${SID}
`).toString())
console.log(shownOnes.bracket)
} catch (e) {
// console.warn(e)
}
if (shownOnes) {
const toBeRm = []
for (const shownOne of shownOnes.bracket) {
if (shownOne === `space.${SID}` || windowIds.includes(shownOne)) {
continue
}
toBeRm.push(shownOne)
}
console.log({ toBeRm })
if (toBeRm.length > 0) {
execSync(`
/opt/homebrew/bin/sketchybar ${toBeRm.map(a => `--remove ${a}`).join(" ")}`)
}
}
}
/*
* add windows
*/
for (const [index, win] of spaceWindows.entries()) {
const itemId = `win.${SID}.${win.id}`
const itemPos = SID > 5 ? "e" /*right*/ : "q" /*left*/
//background.color=${itemPos === "e" && index === 0 ? "0xffff0000" : "00x00ff0000"}\
const cmd = `
/opt/homebrew/bin/sketchybar --add item ${itemId} ${itemPos} \
--set space.${SID} \
icon.color=${SELECTED ? "0xa0ffffff" : "0x80ffffff"} \
--set ${itemId} \
background.padding_right=${index === 0 ? "5" : "0"}\
background.drawing=true \
background.height=10 \
background.image.scale=0.75 \
background.image="app.${win.app}" \
--move ${itemId} ${SID > 5 ? "after" : "before"} space.${SID}
`
// console.log(cmd)
execSync(cmd)
}
/**
* add group containing space indicator and window items
*/
execSync(`
/opt/homebrew/bin/sketchybar --add bracket win.${SID} space.${SID} '/win\.${SID}.*/' \
--set win.${SID} \
background.height=28 \
background.border_width=${SELECTED ? "0" : "1"} \
background.border_color=${SELECTED ? "0x80ffffff" : "0x80ffffff"} \
background.corner_radius=${SELECTED ? "5" : "5"} \
background.color=${SELECTED ? "0x80ffffff" : "0x00ffffff"}
`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment