No internet connection
  1. Home
  2. Script Sharing

Wait for Pro Tools to Open Session

By Chad Wahlbrink @chadwahlbrink
    2022-01-11 19:16:58.823Z2022-01-25 14:29:27.953Z

    After much toil and trouble trying to get a script to wait for Pro Tools to launch to start other actions, I discovered this little bit of code.

            sf.ui.proTools.appWaitForActive();
    
            while (!sf.ui.proTools.invalidate().hasValidUI) {
                sf.wait({ intervalMs: 200 })
            }
    
            while (sf.ui.proTools.invalidate().windows.filter(w => w.title.value.match(/^(Edit:|Mix:)/)).length.valueOf() == 0) {
                sf.wait({ intervalMs: 200 })
            }
    
    

    It took me SO long to realize I had to use the below bit to wait for Pro Tools to fully launch and have valid UI ↓

    while (!sf.ui.proTools.invalidate().hasValidUI) {
            sf.wait({ intervalMs: 200 })
        }
    

    I hope this helps someone else someday!

    • 5 replies
    1. Chad Wahlbrink @chadwahlbrink
        2022-01-25 14:29:48.995Z

        Updated this with an even more robust way to handle this!

        1. Ryan DeRemer @Ryan_DeRemer
            2022-04-07 17:24:16.699Z

            Hey @chadwahlbrink ,

            Awesome bit of code, thanks for sharing! I have a question. What exactly is the "valid UI" the code is waiting for?

            I just copy/pasted this code and put a Finder action after it, and the Finder action triggered while the initial loading popup was still going. Should this code be waiting for that loading popup to go away? I'm wondering if it's another quirk in my system.

            1. This line of code:

              while (!sf.ui.proTools.invalidate().hasValidUI) {
                          sf.wait({ intervalMs: 200 })
                      }
              

              is saying "AS long asPT doesn't have a valid UI (User Interface - ie. an open window and valid menubar), wait 200 ms and check again.

              So your Finder action is triggered as soon as a PT window of any sort appears onscreen.

              What you need to do is put this after the code above and before your Finder action:

              sf.ui.proTools.waitForNoModals();
              

              This will wait for all loading and activity windows to close before proceeding.
              Depending on how long it takes for PT to open a session the you may have to repeat it a couple of times

              1. Ryan DeRemer @Ryan_DeRemer
                  2022-04-08 02:15:49.997Z

                  @Chris_Shaw so the sf.ui.proTools.waitForNoModals(); command specifically targets this loading dialog?

                  I ended up finding this bit of code for checking for the "Create New.." to be enabled. Working so far. You have to put a wait command before it to wait for the loading to begin so the PT menu bar is accessible. Yours seems a bit more direct so I'll give it a try.

                  //WAIT FOR PRO TOOLS TO LAUNCH
                  function waitForProTools() {
                      var i = 0
                      while (!sf.ui.proTools.getMenuItem('File', 'Create New...').isEnabled) {
                          if (i++ <= 100) sf.wait({ intervalMs: 100 });
                      }
                  }
                  
                  1. Ryan DeRemer @Ryan_DeRemer
                      2022-04-08 02:53:26.443Z

                      @Chris_Shaw now I'm torn.

                      After the Finder action, the script goes back to PT to do other stuff.

                      When I used this code, the script triggers the Finder actions before the PT loading screen is done, BUT it still waits until PT is done loading before moving onto the PT stuff, without throwing any errors. Actually kind of nice, but I'm hesitant to trust it.

                      function waitForProTools() {
                          while (!sf.ui.proTools.invalidate().hasValidUI) {
                              sf.wait({ intervalMs: 200 })
                          }
                          sf.ui.proTools.waitForNoModals();
                      }
                      

                      When I use this code, the Finder action waits until PT is fully done loading, which SEEMS more stable in my mind, but takes more time.

                      function waitForProTools() {
                          while (!sf.ui.proTools.invalidate().hasValidUI) {
                              sf.wait({ intervalMs: 200 })
                          }
                          for (var i = 0; i < 10; i++) {
                              sf.ui.proTools.waitForNoModals();
                          }
                      }
                      

                      Thoughts?