Skip to content

macro

This directory contains files that implement a macro recording and playback system. It allows users to record sequences of voice commands and replay them later with a single command.

The core functionality is split between the Talon file macro.talon and the Python file macro.py.

macro.talon

This file defines the voice commands that users will use to interact with the macro system. It provides commands for:

  • Starting and stopping macro recording (macro record, macro stop).
  • Playing back a macro, either the last recorded or a named one (macro play [{user.saved_macros}]).
  • Copying a macro to the clipboard as a Talon command, either the last recorded or a named one (macro copy [{user.saved_macros}], macro copy as <user.text>).
  • Saving a macro with a name (macro save as <user.text>).
  • Listing and closing the list of saved macros (macro list, macro list close).

macro.py

This file contains the Python implementation of the macro system. It defines the actions that are performed when the voice commands in macro.talon are spoken. Key parts of this file are:

  • Actions class: This class defines all the actions the macro.talon file uses, including recording, stopping, saving, playing, and copying macros.
  • macro_record(): Initializes a new macro recording by resetting the macro list and sets the recording flag to True.
  • macro_stop(): Stops the macro recording and removes the "stop", "play", or "save" command from the macro.
  • macro_save(name): Saves the current macro under the specified name and updates a list of saved macros called user.saved_macros.
  • macro_play(name): Plays back the macro by using actions.mimic() to simulate the recorded voice commands. If a name is specified it will load the macro saved under that name, otherwise it will replay the last recorded command.
  • macro_copy(name): Copies the macro as a series of mimic() commands to the clipboard as text that can be pasted into a Talon file. If a name is specified, it will load that macro. If a name is not specified, it will copy the most recently recorded macro.
  • macro_append_command(words): Appends the command that was just uttered to the macro list.
  • speech_system.register("pre:phrase", fn): Registers a callback function fn that is called before a phrase is processed. This function is responsible for listening to what was spoken while recording and passing it on to the macro_append_command function to be added to the current macro list.
  • macro_list_gui(): Creates an imgui interface to display a list of the user's saved macros.

How they work together

The .talon file provides the user interface for the macro functionality, allowing the user to control the macro recording and playback process with voice commands. The .py file handles the logic, state management, and execution.

When a user speaks a command like "macro record," the corresponding action in macro.py is triggered and the system starts recording the user's subsequent voice commands. These commands are stored in the macro list in macro.py. When the user says "macro stop", the recording stops and the macro list contains a series of lists of words that were spoken during the recording.

Commands like "macro play" or "macro save as" then access that list to replay or save them for later use.