No internet connection
  1. Home
  2. How to

Split into mono - trouble with track select by name

By Robin Buyer @Robin_Buyer
    2022-01-01 20:41:37.578Z

    Hey all,

    New to SoundFlow but really excited about the possibilities. Working on a simple script to split stereo tracks into mono and delete the original stereo tracks as well as the right mono tracks. I keep getting a specific error that I haven't been able to figure out:

    01.01.2022 15:25:54.76 [Backend]: !! Command Error: Split to Mono [user:ckxe41gxt000t2k10uq6mbh7r:ckxe44kqr000v2k107bmtcgdq]:
    Object reference not set to an instance of an object. (Split to Mono: Line 40)
    System.NullReferenceException: Object reference not set to an instance of an object.
    at SoundFlow.Shortcuts.Ax.AxNodes.AxPtTrackListItem.get_IsSelected() + 0x3a
    at SoundFlow.Shortcuts.Ax.AxNodes.AxPtTrackListItem.GetLiveIsSelected() + 0x77
    at SoundFlow.Shortcuts.Automation.Actions.SelectTracksByNameAction.d__9.MoveNext() + 0x296
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at sfbackend!+0x6e9dab
    at sfbackend!+0x6e9ce6
    at SoundFlow.Shortcuts.Automation.AutoAction`1.d__20.MoveNext() + 0x313

    The error is referencing these lines:

        sf.ui.proTools.trackSelectByName({ 
            names: [TrackNames[x]],
            deselectOthers: true,
            });
    

    I'm sure I'm doing something dumb - any help would be appreciated. Full script below:

    sf.ui.proTools.appActivateMainWindow();
    
    //Store the number of selected tracks
    var TrackCount = sf.ui.proTools.selectedTrackCount;
    
    //Store the names of the selected stereo tracks
    var TrackNames = sf.ui.proTools.selectedTrackNames;
    
    //Split selected stereo tracks to mono
    sf.ui.proTools.menuClick({
        menuPath: ["Track","Split into Mono"],
    });
    
    //Store the names of the newly created mono tracks
    var TrackNamesMono = sf.ui.proTools.selectedTrackNames;
    
    //Using this var to select the first stereo track name stored in TrackNames
    var x=0;
    
    //Using this var to select the first right mono track name stored in TrackNamesMono
    var y=1;
    
    //Run the loop as many times as there were selected stereo tracks
    for(var i=0; i<TrackCount; i++)
    {
    
        //Select stereo track - this is the line that throws the error
        sf.ui.proTools.trackSelectByName({ 
            names: [TrackNames[x]],
            deselectOthers: true,
            });
    
        //Delete stereo track
        sf.ui.proTools.trackDelete();
    
        //Increase var to select the next stereo track name stored in TrackNames
        x++;
    
        //Select mono right track
        sf.ui.proTools.trackSelectByName({ 
            names: [TrackNamesMono[y]],
            deselectOthers: true,
            });
    
        //Delete mono right track
        sf.ui.proTools.trackDelete();
    
        //Increase var to select the next right mono track name stored in TrackNamesMono
        y+=2;
    };
    
    Solved in post #2, click to view
    • 3 replies
    1. Hey @Robin_Buyer !

      I couldn't replicate your exact error but I can see a few things going on here that can be improved.

      Here's a revised version of your script that should do the trick:

      function splitToMono() {
          function deleteTracks({ trackNames: names }) {
              sf.ui.proTools.trackSelectByName({
                  names,
                  deselectOthers: true,
              });
      
              sf.ui.proTools.trackDelete();
      
              sf.ui.proTools.mainWindow.invalidate();
      
              sf.keyboard.press({ keys: "left" }); // Hack to refresh menu items
          }
      
          function main() {
              sf.ui.proTools.appActivateMainWindow();
      
              const selectedTrackNames = sf.ui.proTools.selectedTrackNames;
      
              sf.ui.proTools.menuClick({
                  menuPath: ["Track", "Split into Mono"],
              });
      
              sf.ui.proTools.mainWindow.invalidate(); // Refresh cache since tracks have changed
      
              const monoTrackNames = sf.ui.proTools.selectedTrackNames;
      
              const tracksToDelete = [
                  ...Array.from(selectedTrackNames),
                  ...Array.from(monoTrackNames
                      .filter(tn => tn.endsWith('.R'))),
              ];
      
              deleteTracks({ trackNames: tracksToDelete });
          }
      
          main();
      }
      
      splitToMono();
      

      I'd also recommend to check out Chris Shaw's Track Data Utilities package in the store. He has a command called "Stereo Track to Single Mono Track" that is a more advanced version of this!

      Reply1 LikeSolution
      1. RRobin Buyer @Robin_Buyer
          2022-01-02 16:58:19.871Z

          Hey @raphaelsepulveda ,

          Thanks a bunch, works great. Definitely seems cleaner. Quick question - what is the cache you are referring to with the invalidate command?

          1. No problem!

            I'm referring to SoundFlow's cache here. Depending on the actions used, SoundFlow caches what is currently displayed on Pro Tools so it can run more efficiently. The cache gets updated automatically as needed, but sometimes we need to give it a hand.

            In this case, it's a good idea to invalidate the mainWindow since the amount of tracks in the session changed when the stereo track was split. This prevents an unexpected result next time you use sf.ui.proTools.selectedTrackNames;—the selected track names would've been the old selected tracks instead of the newly selected split tracks.

            Rule of thumb is, if you notice SoundFlow acting on old data, try invalidating with sf.ui.proTools.mainWindow.invalidate();

            Hope that makes sense!