This demo implementation shows how to directly communicate with SketchyBar from C or C++ to create advanced and fast helper programs.
The relevant function to send messages to SketchyBar is defined in the
sketchybar.h
header:
char* sketchybar(char* message);
it takes a message, e.g. "--query bar", and returns the response from sketchybar.
Additionally you can run the event server, which makes the helper listen for event calls from SketchyBar:
void event_server_begin(mach_handler handler, char* mach_helper);
where the handler
is a function pointer to a function where all events are
handled:
void handler(env env);
and the mach_helper
string is used to register the helper.
In SketchyBar (starting from v2.9.0) the helper needs to be registered for an item, e.g
sketchybar --set <name> mach_helper=<string>
where the string is the same as used to register the helper.
Note: The helper must run before the mach_helper
property in SketchyBar is
set.
To compile the example run:
clang -std=c99 helper.c -o helper
You could decide to copy the helpers source code to the sketchybar config and compile and run it automatically in sketchybarrc:
HELPER=git.felix.helper
killall helper
cd $HOME/.config/sketchybar/helper && make
$HOME/.config/sketchybar/helper/helper $HELPER &
Run the binary
./helper test
and add an item to your sketchybar like so:
sketchybar --add item front_app_mach left \
--set front_app_mach mach_helper=test \
--subscribe front_app_mach front_app_switched
and note how the item communicates with the helper to handle the
front_app_switched
event