A simple plugin to add a label

I’m trying to write a plugin to add a label at the cursor. The following simple test code works, but only if there is a non zero length selection:

;nyquist plug-in
;version 3
;type analyze
;name "Label here..."
;action "Place a label at cursor..."
;info "test"
(list (list 0.0 "test"))

If there’s a non zero selection, it adds the label at the start of the selection as desired. But if it’s zero length, as it would be if I just clicked on the waveform, it just adds the label at the start of the track.

How do I deal with a zero length selection?

It won’t work for an “analyze” type plug-in because with no selection there is nothing to “analyze”.
It won’t work for an “process” type plug-in because with no selection there is nothing to “process”.
It will work with a “generate” type plug-in.

(btw, I suppose you’re aware that you can create a point label with Ctrl+B)

I changed it to a generate plug-in, and that works well, thanks. But is there any way to get it to always add the labels to a particular label track? It keeps adding new ones.

(btw, I suppose you’re aware that you can create a point label with > Ctrl+B> )

Yes, but I need to add lots of labels with the same text, so I wanted to make it as simple as possible. I could use control+alt+v to paste text to the label as it creates it, but I’ve got about 5 pieces of text to insert dozens of time randomly. I was going to make several plug-ins, and assign them to control+1, control+2, etc.

I can make it do it if I make a selection and extend it down into the label track, but that’s not very convenient. If there’s no way to make it do that directly, is there a way to programatically extend the selection. I can do it with the keyboard by pressing shift+right then shift+down, then it works nicely.

Perhaps I should be using an external keyboard macro for this instead.

Only by selecting both the audio track and the label track.

To do this, click on the audio track, then click on the “track control panel” (the box on the left) of the label track.
Alternatively, if you have just one audio track and one label track you can click on either of the tracks then Ctrl+shift+K (select in all tracks), then generate the label.

Sorry it’s a bit awkward - we really need a new “type” of plug-in that does not require an audio track. It has previously been suggested that there should be a “tools” or “utilities” type plug-in, but this would probably require a new menu item in the main Audacity menu. (There was previously a “Tools” menu, but there were so few things in it that it was removed again). I’m not sure how this type of feature request should be handled, but if you’d like Audacity to have a “tools” type plug-in that does not require a track selection, then you are very welcome to suggest it on the Features Request board.

That’s another possibility.
On Linux you can do it with “AutoKey”.
On Windows there is “AutoHotKey” which I assume can do something similar (I’ve never used it).

I’ve discovered that it works fine if I just click where I want the label in the label track. It’s not as precise as clicking on the bump in the waveform that I want to label, but it’ll do.

I’ve come across these limitations before, and thought all it needs is a way to manipulate the selection inside the plugin. Perhaps that’s technically difficult.

A built in key macro facility would probably let a lot more people do these sorts of things too, rather than learning nyquist programming or installing their own keyboard macro program.

That has definitely been suggested before, but does not appear to be listed on the wiki, so that would be worth raising on the “Feature Request” forum board: Adding Features - Audacity Forum

Now that I’ve come to test this on a long track, it’s not working as I want it. As soon as I add the label, it does a Fit Project zoom, so I have to zoom back in again to do the next label.

It looks like Generate plug-ins always do this zoom when they’re done. Is there any way around it?

Once you have an audio track with a label track below it, Click on the label track where you want the label, then press “Up Cursor”, then press “Enter”, then generate the label.

Thanks, that works, but why? I can see that it’s extending the cursor position into the audio track. What does the Enter do? Put focus on the audio track? Why does that prevent it zooming out?

If I still have to do fiddly things like that to enter the labels, I think I’ll have to start experimenting with AutoHotKey.

It makes the audio track “selected”.
(you will notice that the track control panel on the left of the audio track becomes highlighted)

If there is an audio track selected, a “Generate” plug-in will generate audio into the selected track(s).
In this case we are generating “no audio” - in effect that’s the same as generating audio of zero duration. Audacity will zoom out to accommodate the generated audio, but because there isn’t any Audacity does not need to zoom out.

If there is no audio track selected, then a generate plug-in will create a new track. In this case Audacity will zoom out to show the generated audio in the context of the entire project. Personally I’m not sure how useful this behaviour is, but after lengthy discussions the developers decided that this was the behaviour that they wanted.

The underlying reason for why this is fiddly is because you are doing something that neither generate, effect or analyze type plug-ins are supposed to do. I think that rally we need a “Tools” or “Utilities” type plug-in that does not require an audio selection for plug-ins that do not return audio (such as this plug-in).

I’m now trying to use AutoHotKey to do the same thing, and it’s working but slightly oddly. I know this isn’t an AutoHotKey forum, but I’m wondering if anyone can suggest why it’s doing this.

I want to click on the (zoomed in) waveform to place the cursor, then have the AutoHotKeyScript press control+B to create a label in the track under it, then type a “?” in the label and press Enter to close the label editing.

My AutoHotKeyScript is

^+1::
Send ^b?{Enter}
return

(The first line defines the hotkey, the second sends the keys, the third quits the script.)

I click on the waveform then press control+shift+1. It places the label in the correct place, but then the display scrolls to the start of the track. If I press the right arrow key, I find that the cursor was where I left it.

Why does the display scroll to the start of the track?

I can work around it by adding a Right key after the Enter

Send ^b?{Enter}{Right}

but I’d like to understand why it’s necessary.

I’m afraid I can’t help with AutoHotKey as I’ve not used it in years and I’m not even using Windows.
The Nyquist section is one of the quieter corners of the forum, so you may do better to start a new topic in the General Audio Programming section: http://forum.audacityteam.org/viewforum.php?f=21

Done. It’s at AutoHotKey to add a label (Windows)

The least fiddly workaround that I can come up with:

Using the “generate” type plug-in for creating the label,

  • “Edit menu > Preferences > Keyboard”
  • Select “Generate” as the category, then select your plug-in in the list
  • Set a shortcut for the plug-in as Ctrl+shift+J (see here for details: Audacity Manual)

Now all that you need to do to create the label:

  1. Click on the audio track where you want the label
  2. Press Ctrl+shift+K
  3. Press Ctrl+shift+J

(You could use different shortcut keys, but Ctrl+shift+K is already set by default and Ctrl+shift+J is right next to it and unused by default)