Skip to content

Commit 672bb07

Browse files
authored
Add toggle_mute and set_brightness actions (#18)
* feat: add empty_trash and toggle_do_not_disturb macOS actions Add two new system actions: - empty_trash: empties the macOS Trash via Finder AppleScript - toggle_do_not_disturb: toggles Focus/DND mode via Shortcuts with fallback to opening Focus settings Both actions follow the existing pattern in system_actions.cpp and are registered under the 'system' category. * Add toggle_mute and set_brightness actions - toggle_mute: mute/unmute via AppleScript volume settings - set_brightness: set screen brightness, tries brightness CLI then falls back to key simulation via System Events --------- Co-authored-by: harvenstar <harvenstar@users.noreply.github.com>
1 parent 37be040 commit 672bb07

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

src/actions/system_actions.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,64 @@ static ActionResult action_get_ip_address(const std::string& args_json) {
160160
"{\"action\": \"get_ip_address\", \"local\": \"" + local_ip + "\", \"public\": \"" + pub_ip + "\"}"};
161161
}
162162

163+
static ActionResult action_toggle_mute(const std::string& args_json) {
164+
(void)args_json;
165+
auto r = run_applescript("set curVol to output muted of (get volume settings)\n"
166+
"set volume output muted (not curVol)\n"
167+
"if curVol then\n return \"unmuted\"\nelse\n return \"muted\"\nend if");
168+
if (r.success) {
169+
std::string state = trim_output(r.output);
170+
return {true, "Audio " + state, "", "{\"action\": \"toggle_mute\", \"state\": \"" + state + "\"}"};
171+
}
172+
return {false, "", r.error, "{\"error\": \"" + r.error + "\"}"};
173+
}
174+
175+
static ActionResult action_set_brightness(const std::string& args_json) {
176+
std::string level = json_get_string(args_json, "level");
177+
if (level.empty())
178+
return {false, "", "Brightness level required (0-100)", "{\"error\": \"missing level\"}"};
179+
// brightness value is 0.0-1.0 for the shell command
180+
float pct = std::stof(level) / 100.0f;
181+
if (pct < 0.0f) pct = 0.0f;
182+
if (pct > 1.0f) pct = 1.0f;
183+
char buf[32];
184+
snprintf(buf, sizeof(buf), "%.2f", pct);
185+
auto r = run_shell(std::string("brightness ") + buf + " 2>/dev/null");
186+
if (!r.success) {
187+
// fallback: use AppleScript via System Events
188+
int keyPresses = static_cast<int>(pct * 16);
189+
std::string script = "tell application \"System Events\"\n"
190+
" repeat 16 times\n"
191+
" key code 145\n" // brightness down
192+
" end repeat\n"
193+
" repeat " + std::to_string(keyPresses) + " times\n"
194+
" key code 144\n" // brightness up
195+
" end repeat\n"
196+
"end tell";
197+
r = run_applescript(script, 10000);
198+
}
199+
if (r.success)
200+
return {true, "Brightness set to " + level + "%", "",
201+
"{\"action\": \"set_brightness\", \"level\": " + level + "}"};
202+
return {false, "", r.error, "{\"error\": \"" + r.error + "\"}"};
203+
}
204+
205+
static ActionResult action_empty_trash(const std::string& args_json) {
206+
(void)args_json;
207+
auto r = run_applescript(
208+
"tell application \"Finder\" to empty trash");
209+
if (r.success) return {true, "Trash emptied", "", "{\"action\": \"empty_trash\"}"};
210+
return {false, "", r.error, "{\"error\": \"" + r.error + "\"}"};
211+
}
212+
213+
static ActionResult action_toggle_do_not_disturb(const std::string& args_json) {
214+
(void)args_json;
215+
auto r = run_shell("open 'x-apple.systempreferences:com.apple.Focus-Settings.extension'");
216+
if (r.success)
217+
return {true, "Opened Focus settings", "", "{\"action\": \"toggle_do_not_disturb\"}"};
218+
return {false, "", r.error, "{\"error\": \"" + r.error + "\"}"};
219+
}
220+
163221
void register_system_actions(ActionRegistry& registry) {
164222
registry.register_action(
165223
{"screenshot", "Take a screenshot",
@@ -250,6 +308,42 @@ void register_system_actions(ActionRegistry& registry) {
250308
"What's my IP address?",
251309
"rcli action get_ip_address '{}'"},
252310
action_get_ip_address);
311+
312+
registry.register_action(
313+
{"empty_trash", "Empty the Trash",
314+
"{}",
315+
true,
316+
"system",
317+
"Empty my trash",
318+
"rcli action empty_trash '{}'"},
319+
action_empty_trash);
320+
321+
registry.register_action(
322+
{"toggle_mute", "Mute or unmute system audio",
323+
"{}",
324+
true,
325+
"system",
326+
"Mute my Mac",
327+
"rcli action toggle_mute '{}'"},
328+
action_toggle_mute);
329+
330+
registry.register_action(
331+
{"set_brightness", "Set screen brightness (0-100)",
332+
"{\"level\": \"0-100\"}",
333+
true,
334+
"system",
335+
"Set brightness to 50 percent",
336+
"rcli action set_brightness '{\"level\": \"50\"}'"},
337+
action_set_brightness);
338+
339+
registry.register_action(
340+
{"toggle_do_not_disturb", "Toggle Do Not Disturb / Focus mode",
341+
"{}",
342+
true,
343+
"system",
344+
"Turn on do not disturb",
345+
"rcli action toggle_do_not_disturb '{}'"},
346+
action_toggle_do_not_disturb);
253347
}
254348

255349
} // namespace rcli

0 commit comments

Comments
 (0)