Deactivate selected track with Lua

At the moment I’m trying to dig into Lua, but since I have only little experience in scripting, this is more of trial and error.
I have the following code, which checks for a selected track and prints out the trackname to the console:

ardour {
	  ["type"]    = "EditorAction",
	  name        = "",

	function factory (unused_params)
	  return function ()
		-- Selected track
		local sel = Session:route_by_selected_count(0)	
        	local track = sel : to_track();	

		-- Print Name of selectetd track
	   	local trackname = sel : name()


I’m trying to change some attribute of the selected track, such as deactivate it or change it’s visibility.
Can someone give me a hint, how to do that?
Also i’d like to check, if there’s only one track selected and if that track is a Midi Track.
At the moment I’m a bit lost, it’s hard for me, to understand how to apply the methods from the Lua Reference.
I’d be thankful for tipps and hints! :slight_smile:

For the case at hand, look at methods for “Route”:

In Ardour, everything that can move Audio or MIDI data around (Audio Track, MIDI Track, Busses) is-a Route. So to deactivate a track:

local sel = Session:route_by_selected_count(0)
if not sel:isnil() then
 sel:set_active (false, nil)

As for show/hide. This is a UI property. Have a look at the Snippet that comes with Ardour: ardour/s_showhide_track.lua at master · Ardour/ardour · GitHub

1 Like
--get the count of selected routes (.tracks here = tracks, busses & Master)
local selCount = Editor:get_selection().tracks:routelist():size()

if selCount == 1 then
  --only one route selected: get it
  local r = Session:route_by_selected_count(0)

  if r:presentation_info_ptr():flags() & ARDOUR.PresentationInfo.Flag.MidiTrack == ARDOUR.PresentationInfo.Flag.MidiTrack then
    -- it is a MIDI Track
1 Like

Or try cast:

if not r:to_track():isnil() and not r:to_track():to_midi_track():isnil() then
 -- r is-a MIDI track
1 Like

Awesome guys, thank you for your help! :blush:
I’m not at home this week, but but I will check out your suggestions asap. :+1:

When I enter your code into the scripting window, I get an error message
Error: [string "--get the count of selected routes (.tracks h..."]:2: attempt to call a nil value (method 'size')
although I had selected a track.
I also couldn’t find the method size() in the API. I just forgot to put a space before the brackets! :see_no_evil:
However, I still get the error message attempt to call a nil value (method 'size')

Oops. :routelist():size() not :size()
Fixed in the earlier post.

Spaces before brackets are not required. It’s the accepted convention/style for readability purposes, for some languages at least (?). I’m not sure when it became a thing but I missed the memo, so I’m often stuck in the old no-space ways.

1 Like

Great, thank you mate! :slight_smile:
I actually just seem to have problems, to understand what is happening there.
First I thought, routelist()returns a kind of array containing the selected tracks and the size()method returns the size of that array, but I’m beginning to think, that this assumption is wrong…
Thank you anyway, it’s a great help to me! :+1:

You’re welcome. I think your assumption is correct, something along those lines anyway.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.