App Intents and Shortcuts Integration
Relevant source files
The following files were used as context for generating this wiki page:
VoiceInk leverages Apple's App Intents framework to expose core application functionality to the macOS Shortcuts app and Siri. This allows users to create powerful, customized workflows and control the application using voice commands. Alongside App Intents, the application uses the KeyboardShortcuts library to provide user-configurable global hotkeys for quick access to common actions.
This dual approach provides two distinct but complementary methods for automation: deep system integration via App Intents for complex workflows, and immediate, keyboard-driven control via global hotkeys for frequent tasks.
Core Components
The integration is built around several key components that work together to provide a seamless user experience.
AppShortcuts Provider
The AppShortcuts struct serves as the central definition point for all shortcuts exposed to the system. It conforms to AppShortcutsProvider and is responsible for declaring which intents are available as shortcuts.
- Shortcut Declaration: It provides an array of
AppShortcutinstances, currently including theToggleMiniRecorderIntent. - Siri Phrases: It defines custom phrases that users can speak to Siri to invoke the shortcuts, such as "Toggle recorder in VoiceInk".
- Dynamic Parameter Updates: It includes a crucial static method,
updateAppShortcutParameters(), which is called at application launch to refresh any dynamic data used by the intents, such as the list of available Power Modes.
This is how the application ensures the Shortcuts app has the most up-to-date information.
// VoiceInk/VoiceInk.swift:103
AppShortcuts.updateAppShortcutParameters()
Sources: VoiceInk/AppIntents/AppShortcuts.swift, VoiceInk/VoiceInk.swift:103
KeyboardShortcuts Library
For in-app global hotkey configuration, VoiceInk uses the third-party KeyboardShortcuts library. This functionality is exposed to the user in the settings panel, allowing them to assign custom key combinations to various actions.
The following diagram illustrates the relationship between the main shortcut-related components.
Defined Shortcuts and Hotkeys
VoiceInk defines several actions that can be triggered by shortcuts. These are managed through both the AppIntents framework and the KeyboardShortcuts library.
| Shortcut Name | Description | Configuration UI | Source File |
|---|---|---|---|
toggleMiniRecorder | Primary hotkey for toggling recording. Can be a modifier key or a custom shortcut. | SettingsView | SettingsView.swift |
toggleMiniRecorder2 | Secondary hotkey for toggling recording. | SettingsView | SettingsView.swift |
pasteLastTranscription | Pastes the most recent original (un-enhanced) transcription. | SettingsView | SettingsView.swift |
pasteLastEnhancement | Pastes the AI-enhanced transcript if available, otherwise falls back to the original. | SettingsView | SettingsView.swift |
retryLastTranscription | Re-transcribes the last recorded audio using the current model and copies the result. | SettingsView | SettingsView.swift |
cancelRecorder | An optional, custom shortcut to cancel an ongoing recording session. | SettingsView | SettingsView.swift |
Toggle Recorder (App Intent) | Toggles recording and can optionally apply a Power Mode. | Shortcuts App | AppShortcuts.swift |
Sources: VoiceInk/Views/Settings/SettingsView.swift:23-152, VoiceInk/AppIntents/AppShortcuts.swift:13-20
App Intent Implementation: ToggleMiniRecorderIntent
This is the primary AppIntent exposed by VoiceInk. It allows users to start or stop a recording from outside the application, with an optional parameter to apply a specific Power Mode.
Intent Parameters
The intent defines one parameter:
powerMode: An optionalPowerModeAppEntity. When a user creates a shortcut in the Shortcuts app, they can choose a specific Power Mode from a dynamically populated list to be applied when the shortcut runs.
Sources: VoiceInk/AppIntents/ToggleMiniRecorderIntent.swift:19-20
Dynamic Options with PowerModeQuery
To provide the list of available Power Modes to the Shortcuts app, the PowerModeAppEntity uses PowerModeQuery.
PowerModeAppEntity: A lightweight struct that represents aPowerModeConfigin a way that App Intents can understand. It contains theidand adisplayRepresentation(name and icon).PowerModeQuery: This class implements theEntityQueryprotocol. ItssuggestedEntities()method fetches all enabledPowerModeConfigobjects fromPowerModeManager.sharedand maps them toPowerModeAppEntityinstances. This is how the Shortcuts app gets the list of available modes.
Sources: VoiceInk/AppIntents/ToggleMiniRecorderIntent.swift:39-64, VoiceInk/PowerMode/PowerModeConfig.swift:188-191
Execution Flow
When the ToggleMiniRecorderIntent is triggered, its perform() method executes the following logic:
- It retrieves the shared
WhisperStateinstance, which manages the recording state. - It checks if the optional
powerModeparameter was provided. - If a
powerModeis present, it uses its ID to fetch the fullPowerModeConfigfromPowerModeManager. - The fetched configuration is then applied to the
WhisperState. - Finally, it calls
whisperState.toggleRecording()to start or stop the recording.
The sequence diagram below visualizes this process.