KiwiDesk never triggers sketchybar events itself. Instead, you
write a hook in your init.lua that listens to KiwiDesk events
and calls sketchybar --trigger <custom-event> via
KiwiDesk.exec. Your sketchybar items subscribe to that custom
event and update when it fires.
This decouples the two processes: sketchybar’s script runs in
its own Lua sandbox, outside the app watchdog and main-thread
restrictions.
Wedged daemon → runaway triggers.sketchybar --trigger normally exits in milliseconds. If the
sketchybar daemon is absent or wedged, the trigger blocks instead
— and because these hooks fire on every KiwiDesk event,
unbounded fire-and-forget would let thousands of stuck --trigger
processes pile up (re-parented to launchd, some alive for hours),
measurably loading the scheduler.
KiwiDesk.exec bounds this automatically: each trigger gets a
30 s default timeout and identical in-flight commands are
deduped, so even a permanently wedged daemon leaves at most
one stuck child per distinct trigger, reaped every 30 s (see
KiwiDesk.exec). KiwiDesk
also warns in its log once more than 20 children are outstanding.
If you hit a pile-up from an older config (or a hard-wedged
daemon), clear the clients and restart the daemon:
Terminal window
pkill-f"sketchybar --trigger"# clients only, not the daemon
A managed window disappears (closed/minimized/hidden/vanished); desktop names the Desktop holding a vanished window, else nil
window_moved_to_space
window_id, app, from, to, bundle_id
See caveats
layer_change
from, to
The active keyboard layer changes (layer names, default included)
Caveats:
window_created and window_destroyed track the visible
window set, not app lifecycle — deminiaturizing and macOS
Desktop switches also fire them. Filter on the reason
argument to ignore the artifacts (vanished/returned
bursts on every Mission Control round-trip). A window closed
while its Desktop was off-screen sends its corrective
closed when KiwiDesk next reads the Desktops — at the next
Desktop switch, or within about five seconds — so it fires
two destroys, vanished then closed. Keep a
desktop_change handler that re-queries state anyway: it is
the shape that stays right whatever a consumer filters.
space in Lua callbacks is always a string (the space id).
A window on an unknown space receives "" (empty string
instead of nil, so the argument list doesn’t truncate). In
the CLI JSON event stream, the key is space_id and the
value is null for unknown spaces.
window_moved_to_space fires only on explicit
move_to_space commands with a different target;
bulk operations like profile loads stay silent.
The widget creates one item per space, pre-allocates window
icon slots beneath each, and polls state via
kiwidesk get_state | jq on every kiwidesk_update event. A
cold-start watchdog polls until the first successful read, then
goes event-driven.
-- desktop_change stays in the list: a window closed while
-- its Desktop was off-screen is reported closed only when
-- KiwiDesk next reads the Desktops, so the Mission Control
-- round-trip re-queries rather than waiting (see the caveats
-- above). monitor_change redraws the bar when a screen
-- connects or disconnects.
for_, eventinipairs({
"space_change",
"layout_change",
"focus_change",
"desktop_change",
"monitor_change",
"window_created",
"window_destroyed",
"window_moved_to_space",
}) do
KiwiDesk.on(event, function()
KiwiDesk.exec(
"sketchybar --trigger kiwidesk_update"
)
end)
end
Keep your hooks in init.lua under GUI-managed settings
When the Settings app takes ownership of your config
(adopting it into gui.json), only the managed parts of
init.lua go inert — tiling settings, keybindings, window
rules. KiwiDesk.on(...) event hooks are not managed: they
keep running from init.lua on every reload, and init.lua
stays the only place they can live. Don’t delete your
sketchybar hooks because “the GUI owns the config now” —
that silently kills the bar.
With more than one screen, the widget above brightens only
active_space, and every Space item draws on every screen’s bar.
Each get_state Space also reports shown and display
(get_state), so the widget can
brighten every Space on show and draw each Space on its own
screen’s bar, empty Spaces included. These changes to the
widget do it.
First, read the two fields in the jq filter, ahead of the window
list, which stays last:
Third, map KiwiDesk’s screen ids to SketchyBar’s. display is
the screen’s list_monitors id, the same number SketchyBar
reports as DirectDisplayID, while an item’s display property
takes SketchyBar’s arrangement-id. Add this function above
update_spaces(), and build the map on every update, since a
screen’s id changes when it reconnects:
localfunctionarrangement_ids()
localids= {}
for_, dinipairs(SBAR.query("displays")) do
ids[d.DirectDisplayID] =d["arrangement-id"]
end
returnids
end
Then, in the loop that updates each Space, brighten every Space
on show and place the Space and its window slots on its screen:
localarrangement=arrangement_ids()
for_, space_keyinipairs(space_names) do
localw=spaces[space_key]
localis_focused= (space_key==active_space)
localon_show=is_focusedor (w~=nilandw.shown)
-- A Space on no screen draws on the focused screen.
localwhere= (wandarrangement[w.display])
or"active"
localspace_data=spaces_store[space_key]
ifspace_datathen
space_data.item:set({
display=where,
icon= {
color=on_showand0xffffffff
or0xff888888
}
})
for_, win_iteminipairs(space_data.win_items) do
win_item:set({ display=where })
end
-- (window slots as before, with on_show in place
-- of is_focused for icon_color; the green focused
-- window keeps is_focused)
Moving a Space to another screen sends space_change, which
redraws the bar.
A screen split into areas
shows one Space per area, and SketchyBar still draws one bar for
the whole screen. With the Multiple screens changes, that bar
brightens each area’s Space but lists the Spaces in space_names
order, so two areas’ Spaces mix. These changes build on those and
group them: each area’s Spaces sit together, in the areas’ order
across the screen, with a gap between areas. A screen that is not
split keeps one group, as above.
Each get_state Space carries
area, and list_monitors lists each split screen’s areas. An
area key holds the screen’s name and a colon, so the filter writes
the area’s place on its screen instead: 0 for the left or top area,
and for every Space on a screen that is not split. When
list_monitors fails, or a release has no areas, every place is
0 and each screen keeps one group.
First, read list_monitors beside get_state, and write that
place after display. Replace the io.popen call in
update_spaces():
Third, add this function above update_spaces(). It orders the
items by screen, then area, then their place in space_names, and
puts the gap before each area’s first Space. The gap replaces the
Space item’s own left padding, so set SPACE_PADDING to the
padding_left your config gives it. The function moves the items
only when the grouping changes:
localSKETCHYBAR="/opt/homebrew/bin/sketchybar"
localSPACE_PADDING=0
localAREA_GAP=12
locallast_order=""
localfunctiongroup_by_area(spaces,arrangement)
localorder= {}
fori, keyinipairs(space_names) do
localw=spaces[key]
order[i] = {
key=key,
index=i,
-- A Space on no screen goes last.
screen= (wandarrangement[w.display]) ormath.huge,
area= (wandw.area) or0,
}
end
table.sort(order, function(a,b)
ifa.screen ~=b.screen thenreturna.screen <b.screen end
for_, win_iteminipairs(spaces_store[s.key].win_items) do
table.insert(names, win_item.name)
end
end
localjoined=table.concat(names, "")
localgrouping=joined..table.concat(gaps, ",")
ifgrouping==last_orderthenreturnend
fori, sinipairs(order) do
spaces_store[s.key].item:set({
padding_left=SPACE_PADDING
+ (gaps[i] ~="" andAREA_GAPor0)
})
end
-- Remembered only once the reorder ran, so a failed one retries.
ifos.execute(SKETCHYBAR.." --reorder " ..joined) then
last_order=grouping
end
end
Last, call it at the end of update_spaces(), after the loop that
updates each Space:
group_by_area(spaces, arrangement)
Moving a Space to another area sends space_change, which redraws
the bar. Splitting a screen or making it whole again can send no event,
and then the bar regroups on the next one; run
sketchybar --trigger kiwidesk_update to regroup it at once.
If sketchybar loads items conditionally (e.g., only when
KiwiDesk is running) and its item loader fires before
KiwiDesk’s config process reaches the event loop, the initial
update_spaces() call in the item script will fail (the socket
isn’t ready). Add a grace period to your loader:
Terminal window
sleep1 && sketchybar--additemkiwidesk_spacesright
Or use the watchdog above, which polls until the first
successful read.
io.popen inside sketchybar’s SbarLua
The sketchybar widget script runs in sketchybar’s own Lua
process, not inside KiwiDesk’s. sketchybar bundles its own
Lua (SbarLua), separate from KiwiDesk’s embedded 5.5 VM, and
it is a normal unrestricted runtime — so io.popen is fully
available. Inside KiwiDesk’s Lua VM, io.popen is disabled
to prevent blocking the main thread — use KiwiDesk.exec
with a callback instead (see Lua
reference).
A theme picker in your bar can repaint KiwiDesk too: its colours
and the KiwiShelf font are CLI commands. One script holds the
themes, and a picker item runs it.
The commands change the live look, and only until the next
profile apply. Loading a profile, a Desktop binding switching
profiles, a screen-count change, reload_config and a KiwiDesk
restart all put the profile’s look back, which is why the
script carries a profile entry. The reverse holds too:
save_profile after a theme pick saves that theme into the
profile.
With Liquid Glass on, the fill tints the glass
(kiwishelf.set_fill_color).
A bar’s usual faint wash (0x40 alpha) barely tints it; a
theme’s solid pill colour reads as tinted glass.
KiwiDesk takes a font’s family name
(kiwishelf.set_font_family).
If the bar uses the same font, give sketchybar the family’s real
style name: Charter’s regular face is Roman and Apple
Chancery’s only style is Chancery, and sketchybar falls back to
the system font without a word on a style the family lacks.