Overview
Toggles the state of a checkbox or switch element, flipping it from checked to unchecked or vice versa. This is the action equivalent of clicking a toggle control.Syntax
Parameters
Element reference from snapshot (must be a
checkbox or switch)Response
Response Fields
The action performed (
toggle)The element reference that was toggled
Element state after toggling
role(string): “checkbox” or “switch”value(string): “0” (unchecked) or “1” (checked)states(string[]): Includes “checked” if checked
AX-First Strategy
The toggle command:- Reads current checked state via
kAXValueAttribute - Performs
kAXPressActionto toggle - Verifies state changed
- Falls back to click if AX toggle fails
Usage Examples
Toggle Checkbox
Toggle Switch
Toggle Multiple Options
Conditional Toggle
Toggle vs Check/Uncheck
| Command | Behavior | Idempotent |
|---|---|---|
toggle | Flips current state | No |
check | Ensures checked | Yes |
uncheck | Ensures unchecked | Yes |
toggle when:
- You want to flip the current state
- Current state is unknown
- Simulating user clicking the checkbox
check/uncheck when:
- You need a specific end state
- Idempotency is required
- Running the same script multiple times
Supported Element Types
| Role | Toggle Behavior |
|---|---|
checkbox | Checked ↔ Unchecked |
switch | On ↔ Off |
radiobutton | Select (not recommended, use click) |
State Values
| State | Value | States Array |
|---|---|---|
| Unchecked | ”0” | ["enabled"] |
| Checked | ”1” | ["enabled", "checked"] |
| Indeterminate | ”2” | ["enabled", "mixed"] |
Error Cases
| Error Code | Cause | Recovery |
|---|---|---|
ELEMENT_NOT_FOUND | Ref doesn’t exist in current refmap | Run snapshot to refresh |
STALE_REF | Element no longer matches saved ref | Run snapshot and use new ref |
ACTION_FAILED | Toggle action not supported | Try click instead |
ACTION_NOT_SUPPORTED | Element is not a checkbox/switch | Wrong element type |
Verifying Toggle
Common Use Cases
- Settings: Toggle preferences on/off
- Feature Flags: Enable/disable features
- Filters: Toggle filter options
- Permissions: Grant/revoke permissions
- Form Controls: Toggle form options
Notes
- Toggle is NOT idempotent; calling twice returns to original state
- Use
check/uncheckfor idempotent operations - Some apps may use “switch” role for toggle switches
- Radio buttons should use
clickinstead oftoggle - State is included in
post_statefor verification - For tristate checkboxes, toggle cycles through all three states
- Always verify state after toggle if critical