No internet connection
  1. Home
  2. How to

Select Track as trigger

By Steve Giammaria @Steve_Giammaria
    2019-09-13 15:06:17.230Z

    I want to fire a macro every time any track is selected... is there a way to do this?

    I want to fire "open track output" which will focus my channel on the icon... thus getting a "focus fader follows track selection" functionality that is lacking on my D-Control.

    Solved in post #7, click to view
    • 13 replies
    1. Hi @Steve_Giammaria.

      You could use a script like this do perform something like it (it may give you some errors here and there but it should work)...

      var lastFocusedTrackName;
      
      function main() {
          var newName = sf.ui.proTools.selectedTrackNames[0];
          if (newName === lastFocusedTrackName) return;
      
          lastFocusedTrackName = newName;
      
          sf.ui.proTools.selectedTrack.trackOutputToggleShow();
      }
      
      function runForever(name, action, interval, timeout) {
          var now = (new Date).valueOf();
          if (now - globalState[name] < timeout) throw 0; //Exit if we were invoked again inside the timeout
      
          globalState[name] = now;
          sf.engine.runInBackground(function () {
              try {
                  while (true) {
                      sf.engine.checkForCancellation();
                      globalState[name] = (new Date).valueOf();
      
                      action();
      
                      sf.wait({ intervalMs: interval, executionMode: 'Background' });
                  }
              } finally {
                  globalState[name] = null;
              }
          });
      }
      
      runForever("isFollowFocusedTrackRunning", main, 500, 5000);
      

      Assign an Application Trigger on Pro Tools being activated, and it should make sure to always be running when Pro Tools is.

      1. You can stop the script (it continues to run in the background) by pressing Ctrl+Shift+Escape if you have default SF triggers enabled in your profile.

      2. S
        In reply toSteve_Giammaria:
        Steve Giammaria @Steve_Giammaria
          2019-09-16 13:33:40.416Z

          Ah thanks!

          this is so close to perfect.

          it errors and stops when no track is selected.

          I'm very green when it comes to coding, but
          what does sf.ui.proTools.selectedTrackNames[0] return when no track is selected?

          we need somthing like:

          if newName != NOTHING lastFocusedTrackName = newName;

          1. SSteve Giammaria @Steve_Giammaria
              2019-09-16 14:56:42.186Z

              or something like:
              if (newName === "") return;

              1. SSteve Giammaria @Steve_Giammaria
                  2019-09-16 16:53:37.054Z

                  figured it out.... I'm learning haha...

                  needs:

                  if (newName === undefined) return;

                  to avoid it dumping out if no track is selected!

                  1. Great find, @Steve_Giammaria!

                    A complete script that works could look like this then:

                    var lastFocusedTrackName;
                    
                    function main() {
                        var newName = sf.ui.proTools.selectedTrackNames[0];
                        if (newName === lastFocusedTrackName || newName === undefined) return;
                    
                        lastFocusedTrackName = newName;
                    
                        sf.ui.proTools.selectedTrack.trackOutputToggleShow();
                    }
                    
                    function runForever(name, action, interval, timeout) {
                        var now = (new Date).valueOf();
                        if (now - globalState[name] < timeout) throw 0; //Exit if we were invoked again inside the timeout
                    
                        globalState[name] = now;
                        sf.engine.runInBackground(function () {
                            try {
                                while (true) {
                                    sf.engine.checkForCancellation();
                                    globalState[name] = (new Date).valueOf();
                    
                                    action();
                    
                                    sf.wait({ intervalMs: interval, executionMode: 'Background' });
                                }
                            } finally {
                                globalState[name] = null;
                            }
                        });
                    }
                    
                    runForever("isFollowFocusedTrackRunning", main, 500, 5000);
                    
                    Reply1 LikeSolution
                    1. SSteve Giammaria @Steve_Giammaria
                        2019-09-17 14:48:38.802Z

                        Oooh that's cleaner than I was doing. thanks!

                        1. In reply tochrscheuer:
                          Aalex alcanjim @alex_alcanjim
                            2020-10-12 10:32:10.865Z

                            Hi @chrscheuer!

                            I'm trying to use this script and something is going wrong when i don't have any track selected. SF pops an error that say "Object reference not set to an instance of an object"

                            That occurs when I click maybe between two tracks and then no track appear as selected. The thing is that, when this error pops up, I have to re-launch the script for use it, because it doesn't recover by itself.

                            Is there any way to make this script to ignore any "non selected track" situation?

                            Thanks so much in advance!

                    2. In reply toSteve_Giammaria:
                      Fokke van Saane @Fokke
                        2019-09-29 19:29:55.464Z

                        Ah, that is interesting, that you can use Selecting a track as trigger. Or, is it running all the time?

                        1. In essence it's accomplishing #1 by doing #2. The specific action modes we're using here ensure that it's running in Background mode though, which means it's not taking up space from other SF commands running. And you can configure the interval for how often you want it to query PT to see if anything has changed.

                          Note you can use this method (scripts running in the background repeatedly checking things) to do all sorts of interesting things :)

                          1. Fokke van Saane @Fokke
                              2019-09-29 19:40:48.336Z

                              Isn't that taking up system resources by constantly checking things?

                              1. Depends on what you do in your loop :) The above script doesn't incur any noticeable performance penalty in my setup. If you asked it to compute a million decimals of PI every 200 ms you could make SoundFlow go to 100% on one core. So of course it should be designed intelligently whatever you set up that way.

                                1. Remember many things are constantly running on a computer, or run in the same way as here. The audio thread in a DAW being one example. If code is designed well you can have it running a crazy amount of things and the CPU usage would be barely noticeable.