auto-selecting programatically


I’d like to automatically select a region adjacent to my selected region, using a macro or Python or something. Is this possible?

For example, I’ve selected a region starting at 01:25 and ending at 01:30. I want to auto-select the region from 01:20 to 01:25 with a single operation.

I’ve been looking at the macros and I think this would be possible if I could set a new selection as 100% of the old selection and starting at T-100% of the old selection starting point and ending at T-0% of the old selection. But I see no way to supply my select parameters with values relative to the current selection’s size (ideally in samples, not seconds).

Thanks. :slight_smile:

For that example, you could create a macro containing the “Select Time” command:

Optionally, create a keyboard shortcut to run the macro: Shortcuts Preferences - Audacity Manual

Thanks Steve. This does get me part of the way there. Is there a way to prompt the user for an input? The selection size needs to be variable, and ideally the number of samples, not seconds.

So, something like…


  • prompt user for number of samples
  • select start=-NumSamples, end=0, relativeTo=SelectionStart

Audacity macros can’t do that. Macros are just a list of commands that run in sequence. They don’t support user interaction or conditional operations. For those kind of features a more flexible programming language is required, such as Nyquist (built into Audacity) or an external language such as Python.

The Nyquist language can issue macro commands. For example;
The “SelectTime:” command shown in my last post can be sent from Nyquist using “AUD-DO”

(aud-do "SelectTime: End=0 RelativeTo=\"SelectionStart\" Start=\"-5\"")

A couple of addition pieces that we need so that this command works:

  1. Audacity needs to be told that the code will be modifying the project using macro / scripting commands.
    We do this by adding a special comment at the top of the script that declares the plug-in / script as a “Tool” type effect:
;type tool
  1. Nyquist should always return a valid value to Audacity when it completes.
    Valid values include: numbers, strings (text), sounds, stereo sounds, “label lists” (specially formatted lists that tell Audacity to add labels).
    If we don’t want to send a value to Audacity at the end of the script, we can send an empty string, which Audacity accepts as “valid” and ignores it. An empty string return value is a “no-op” (no operation command).

Putting this together gives us:

;type tool
(aud-do "SelectTime: End=0 RelativeTo=\"SelectionStart\" Start=\"-5\"")

The above can be run in the Nyquist Prompt effect (see: Nyquist Prompt - Audacity Manual)
Try it.

(more to follow…)

To do that, the code can tell Audacity to produce a GUI with a control to set the required duration.
See this page for an overview: Missing features - Audacity Support

The obvious choice for the type of control, is to use a “Time” widget. See: Missing features - Audacity Support


;control duration "Duration" time "" 1.0 0 nil

Note that the time format is set by the time format selected in the “Selection Toolbar” (See: Selection Toolbar - Audacity Manual)


The Time widget shown above sets the variable “duration” to a numeric value: default = 1, minimum = 0, maximum = anything (“nil”)
Regardless of the time format used by the control, the value of the variable is always in seconds (converted automatically when necessary by the widget).

So the next thing that we need to do is to modify the AUD-DO command, so that “Start” = negative “duration”.
An easy way to do this is to construct the scripting command first, then send it to Audacity with “AUD-DO”.

;type tool

;control duration "Duration" time "" 1.0 0 nil

(setf command
    (format nil
            "SelectTime: End=0 RelativeTo=\"SelectionStart\" Start=~s"
            (- duration)))

(aud-do command)


Try it.

Finally, the Nyquist script can be made into an installable plug-in by adding a few additional “header” comments.
See here for details of what is required: Missing features - Audacity Support

Top Tip:

Nyquist scripts must be written in plain text. A good plain text editor for Windows in NotePad++ (