No internet connection
  1. Home
  2. How to

Get PT Selection Length and use for If Statement

By Dan Messersmith @Dan_Messersmith
    2023-06-28 16:06:47.036Z

    Hello,
    I am trying to create a macro for pasting room tone.
    When I edit audiobooks, I keep 500ms of room tone in my clipboard. I often select and replace breaths by using the Repeat to Fill selection feature in Pro Tools.

    My current issue is if I make a selection longer than 500ms then hit the Repeat to Fill Selection shortcut, I get an annoying "create fades" popup that I want to avoid.

    I want the macro to be able to do this:

    • If the selection length is less than 500ms, use Repeat to Fill selection
    • If the selection length is longer than 500ms, hit paste.

    Could someone help me figure out how to:

    • Get the length of a PT selection in milliseconds,
    • Use it in an if statement?

    Disclaimer: I have never used the "scripting" features in sound flow, I have only built macros with the builder.

    • 3 replies
    1. @Dan_Messersmith, here ya go!

      function getSelectionInMS() {
          const SECONDS_IN_A_MINUTE = 60;
          const MS_IN_A_SECOND = 1000;
          const MS_IN_A_MINUTE = MS_IN_A_SECOND * SECONDS_IN_A_MINUTE;
          let selectionLengthMinSecs;
      
          sf.ui.proTools.mainCounterDoWithValue({
              targetValue: "Min:Secs",
              action: () => {
                  selectionLengthMinSecs = sf.ui.proTools.selectionGet().selectionLength.trim();
              }
          });
      
          // @ts-ignore
          const minutes = Number(selectionLengthMinSecs.split(":")[0]);
          // @ts-ignore
          const [seconds, ms] = selectionLengthMinSecs.slice(2).split(".").map(value => Number(value));
      
          return (minutes * MS_IN_A_MINUTE) + (seconds * MS_IN_A_SECOND) + ms;
      }
      
      function main() {
          const selectionInMS = getSelectionInMS();
      
          if (selectionInMS < 500) {
              sf.ui.proTools.menuClick({ menuPath: ["Edit", "Paste Special", "Repeat to Fill Selection"] });
          } else {
              sf.ui.proTools.menuClick({ menuPath: ["Edit", "Paste"] });
          }
      }
      
      main();
      

      Just copy this into a new script and give it a shot!

      Let me know if you have any questions.

      1. DDan Messersmith @Dan_Messersmith
          2023-06-29 20:32:03.178Z

          Raphael,
          WOW! Thank you so much.
          I'd been wanting to make this for a couple of months now but had no idea how.
          You're a lifesaver!
          Thank you for your help and contribution to this community 🙂
          Dan

          1. Always happy to help :D