Setting (and getting) the selected_track from LiveAPI.

Tom Swirly's icon

Hello, javascripting Maxers.

I make pretty heavy use of Max For Live, but I do as much of my processing as possible in Javascript, not just because manipulating lists is so much easier in Javascript, but because I can easily "virtualize" my commands - store them as a chunk of data in a list somewhere so I can attach "any" Max For Live command to "any" trigger.

So far so good, but this week I'm trying to set the selected_track and failing. Here's my attempts!

function setValue(path, name, value) {
  post(path, ',', name, ',', value, 'n');
  var api = new LiveAPI(this.patcher);
  api.path = path;
  api.set(name, value);
};

// There is definitely a track id 6 in this project, as I can see by instrumenting
// M4L.api.SetSelectedTrackIndex.  In my production code, I compute this id,
// it isn't hard-coded.

function takeFocus() {
  setValue('live_set view', 'selected_track', 'id 6');
  setValue('live_set view', 'selected_track', '6');
  setValue('live_set view', 'selected_track', 6);
  setValue('live_set view', 'selected_track', ['id', '6']);
  setValue('live_set view', 'selected_track', ['id', 6]);
  setValue('live_set view', 'selected_track id', '6');
  setValue('live_set view selected_track', 'id', '6');
};

// RESULTS:
//   js: live_set view  ,  selected_track  ,  id 6
//   jsliveapi: set: no valid object id
//   js: live_set view  ,  selected_track  ,  6
//   jsliveapi: set: no valid object id
//   js: live_set view  ,  selected_track  ,  6
//   jsliveapi: set: no valid object id
//   js: live_set view  ,  selected_track  ,  id  6
//   jsliveapi: set: no valid object id
//   js: live_set view  ,  selected_track  ,  id  6
//   jsliveapi: set: no valid object id
//   js: live_set view  ,  selected_track id  ,  6
//   jsliveapi: Invalid syntax
//   js: live_set view selected_track  ,  id  ,  6
//   jsliveapi: Invalid syntax

As I mention in the comments, a track with id 6 definitely exist, and if I do the same thing in pure Max using live.path and live.object, it works fine...

Tom Swirly's icon

Apparently LiveAPI.set is less tolerant of list vs. string input than LiveAPI.path, but I worked it all out!

My goal was to select the Ableton Live track containing the Max For Live patch device!

and when I got rid of all my testing code it becomes simply

var api = new LiveAPI(this.patcher);
api.path = 'this_device canonical_parent';
var id = api.id;
api.path = ['live_set', 'view'];
api.set('selected_track', 'id', id);

You can find a function to do this in my code repository here.

Walmik Deshpande's icon

Thank you Tom, this helped me from pulling out my hair entirely! I hadnt realized that there are 3 params that can be passed to set for setting the selected track :P

I also checked out your repo.. pretty solid stuff you created a while ago.. hope you re gonna revisit the JS in Max soon again :)