No internet connection
  1. Home
  2. How to

Wait for Previous Selection to change?

By Jack Byrne @Jack_Byrne
    2022-08-01 15:54:59.885Z

    Possibly an odd one, I've got a macro for audiobook work that replaces a selection with a given length of room tone, copied from another track, that uses restore last selection to go back to what I wanted to replace. However, sometimes if I'm working too quickly it'll revert to the selection I had before that (usually the clip just before or after if I'm doing a few of them). I assume there's something in the PT backend that takes a while for the previous selection to update, is there anything I can add to make soundflow wait for this? I'm sure I could just add time, but if there's a specific trigger I can use I'd rather do that as it's a little neater

    One of the scripts in question:

    
    
    function scrollToFirstTrack({ namePrefix }) {
        
        let firstTrack = sf.ui.proTools.visibleTrackHeaders.filter(t => t.normalizedTrackName.startsWith(namePrefix))[0];
        if (!firstTrack) throw `Could not find a visible track starting with "${namePrefix}"`;
    
        firstTrack.trackScrollToView();
    
    }
    
    scrollToFirstTrack({
        namePrefix: 'RT',
    });
    
    
    sf.ui.proTools.selectionSet({
        selectionLength: "0:03.500",
    });
    
    sf.ui.proTools.editModeSet({
        mode: "Shuffle",
    });
    
    sf.ui.proTools.menuClick({
        menuPath: ["Edit","Copy"],
    });
    
    sf.ui.proTools.menuClick({
        menuPath: ["Edit","Restore Last Selection"],
    });
    
    sf.ui.proTools.menuClick({
        menuPath: ["Edit","Paste"],
    });
    
    sf.keyboard.press({
        keys: "ctrl+shift+tab, ctrl+alt+shift+tab, ctrl+alt+shift+tab",
    });
    
    sf.ui.proTools.menuClick({
        menuPath: ["Edit","Fades","Delete"],
    });
    
    sf.keyboard.press({
        keys: "f",
    });
    
    sf.keyboard.press({
        keys: "right",
    });
    

    Thanks!

    • 4 replies
    1. J
      Jack Byrne @Jack_Byrne
        2022-08-01 16:09:21.633Z

        As a minor follow up, it'll sometimes fail and grab the wrong bit to replace if the room tone track isn't in view, but not always. Not managed to figure out how to make that repeatable, but if anything obvious occurs on looking at this script I'd appreciate it. It's easy enough to keep it in view while I'm working, but if I forget it does kill my flow a bit!

        1. In reply toJack_Byrne:
          Chris Shaw @Chris_Shaw2022-08-01 17:58:59.389Z2022-08-01 18:32:18.610Z

          "Restore Last Selection" may be the culprit here.
          I would put this at the top of your script - it gets the original selection:

          const originalTrack = sf.ui.proTools.selectedTrack;
          const origSelection = sf.ui.proTools.selectionGet();
          const { selectionStart, selectionEnd } = origSelection
          

          Then, instead of "Restore Last Selection"
          You can recall the original selection:

          sf.ui.proTools.trackSelectByName({names:[originalTrack.normalizedTrackName]})
          originalTrack.trackScrollToView()
          sf.ui.proTools.selectionSet({
              selectionStart,
              selectionEnd
          });
          

          The only other thing you could consider is replacing the "f" keyboard press with:

          //Create Fades
          // Open fades window
          sf.ui.proTools.menuClick({
            menuPath: ["Edit", "Fades", "Create..."],
          });
          
          // Define fades window
          const fadesWindow = sf.ui.proTools.windows.whoseTitle.contains("Fade").first;
          
          fadesWindow.elementWaitFor();
          
          //Create fade
          fadesWindow.buttons.whoseTitle.is("OK").first.elementClick();
          
          fadesWindow.elementWaitFor({ waitType: "Disappear" });
          

          Whenever possible replace key presses with menu driven commands. This ensures that SF waits until the command is completed before moving on.

          1. JJack Byrne @Jack_Byrne
              2025-04-10 15:07:28.228Z

              Alright I'm like 2 years later on this, I've just adapted to the issue, with the updated script I still get the same issue sometimes (I've also just removed all that stuff with fades, I think everything after the paste is gone. I'm going to experiment with adding manual ms wait times up top, I reckon PT just takes a certain amount of time to update the selection. If anyone happens to already know what that is that would help!

              1. In reply toChris_Shaw:
                JJack Byrne @Jack_Byrne
                  2025-04-28 14:54:49.312Z

                  Hey @Chris_Shaw!

                  So I'm an idiot, and had been trying to edit a duplicate version of the script, which was why no changes were happening. I now pretty much cannot get this thing to fail, no matter how fast I'm moving, so that is great. However, I've stumbled upon a small issue, it's not returning the original selection perfectly. There's almost always a small extra clip getting left behind, usually only a few samples in length.

                  Here's the script I used for this particular screenshot. I've got a random length of room tone as a single clip that I've selected, I run the script, then instead of replacing it outright with the new correct length, it seems to leave a tiny clip behind tacked onto the end. Anything obvious jump out that might be causing it?

                  const originalTrack = sf.ui.proTools.selectedTrack;
                  const origSelection = sf.ui.proTools.selectionGet();
                  const { selectionStart, selectionEnd } = origSelection
                  
                  function scrollToFirstTrack({ namePrefix }) {
                      
                      let firstTrack = sf.ui.proTools.visibleTrackHeaders.filter(t => t.normalizedTrackName.startsWith(namePrefix))[0];
                      if (!firstTrack) throw `Could not find a visible track starting with "${namePrefix}"`;
                  
                      firstTrack.trackScrollToView();
                  
                  }
                  
                  scrollToFirstTrack({
                      namePrefix: 'RT',
                  });
                  
                  
                  sf.ui.proTools.selectionSet({
                      selectionLength: "0:02.500",
                  });
                  
                  sf.ui.proTools.editModeSet({
                      mode: "Shuffle",
                  });
                  
                  sf.ui.proTools.menuClick({
                      menuPath: ["Edit","Copy"],
                  });
                  
                  sf.ui.proTools.trackSelectByName({names:[originalTrack.normalizedTrackName]})
                  originalTrack.trackScrollToView()
                  sf.ui.proTools.selectionSet({
                      selectionStart,
                      selectionEnd
                  });
                  
                  
                  sf.ui.proTools.menuClick({
                      menuPath: ["Edit","Paste"],
                  });