No internet connection
  1. Home
  2. How to

Ignore and skip commands without pausing a script

By Masa Fukui @Masa_Fukui
    2022-08-24 00:01:22.086Z

    Hello,
    I am wondering if there's a way to skip a command and continue to do following actions without stopping.
    In this script which I borrowed from this forum.
    I think it's basically doing:

    Step 1. Search text in Comments.
    Step 2. Select tracks that have the text
    Step 3. Assign outputs accordingly

    But before going to Step2, if there are no such tracks found, this script pauses and a pop up window appears and let me know what to do next.

    BUT I want it to ignore it and keep on doing the following commands which are search, select and assign outputs without asking, even if there are no such comments found in tracks.

    How do I do that?

    I know that in this script it has to skip some commands like assigning outputs if there's no text found, so it might be more complicated...
    I'm quite new here, it's way too hard for me to figure it out....So Please help!

    Thanks!

    sf.ui.proTools.appActivateMainWindow();
    
    sf.ui.proTools.windows.invalidate();
    
    const visibleTracks = sf.ui.proTools.trackGetVisibleTracks().names;
    var firstTrackFound;
    let foundTracks = [];
    
    // Make sure we're on the Edit window. Required to scroll to first selected track.
    if (!sf.ui.proTools.getMenuItem("Window", "Edit").isMenuChecked) {
        sf.ui.proTools.menuClick({
            menuPath: ["Window", "Edit"],
        })
    }
    
    // Make sure comments are visible
    if (!sf.ui.proTools.getMenuItem("View", "Edit Window Views", "Comments").isMenuChecked) {
        sf.ui.proTools.menuClick({
            menuPath: ["View", "Edit Window Views", "Comments"],
        });
    }
    
    while (true) {
        // Ask for search query
        const searchQuery = 
        
    /// example of search list   ("word1 word2 word3")
        ("k")
    
    
        // Format query to RegEx
        const searchQueryRegEx = new RegExp(searchQuery.split(" ").join("|").replace(/^/, "\\b(").replace(/$/, ")\\b"), "i");
    
        // Deselect all tracks
        sf.ui.proTools.trackDeselectAll();
    
    
    
        // Search visible tracks for search query
        for (var i = 0; i < visibleTracks.length; i++) {
            var trackCommentHasSearch = sf.ui.proTools.trackGetByName({ name: visibleTracks[i] }).track.textFields.first.value.value.match(searchQueryRegEx);
    
            if (trackCommentHasSearch) {
                if (!firstTrackFound) { // Scroll to first track found
                    sf.ui.proTools.trackGetByName({ name: visibleTracks[i], }).track.trackScrollToView();
                    firstTrackFound = !firstTrackFound;
                } else {
                    sf.ui.proTools.trackSelectByName({ names: [visibleTracks[i]], deselectOthers: false });
                }
                foundTracks.push(visibleTracks[i]);
            }
        }
        if (foundTracks.length !== 0) {
            break;
        } else if (!confirm("No tracks found. Try another search?")) {
            break;
        }
    }
    
    
    sf.ui.proTools.appActivate();
    
    
    sf.ui.proTools.selectedTrack.trackScrollToView();
    
    
    sf.wait({
        intervalMs: 500,
    });
    
    
    sf.ui.proTools.selectedTrack.trackOutputSelect({
        outputPath: ["bus","bus menu 129-256","KICK▲ (Stereo)"],
        selectForAllSelectedTracks: true,
    });
    
    
    sf.ui.proTools.appActivateMainWindow();
    
    sf.ui.proTools.windows.invalidate();
    
    const visibleTracks = sf.ui.proTools.trackGetVisibleTracks().names;
    var firstTrackFound;
    let foundTracks = [];
    
    
    sf.wait({
        intervalMs: 500,
    });
    
    
    // Make sure we're on the Edit window. Required to scroll to first selected track.
    if (!sf.ui.proTools.getMenuItem("Window", "Edit").isMenuChecked) {
        sf.ui.proTools.menuClick({
            menuPath: ["Window", "Edit"],
        })
    }
    
    // Make sure comments are visible
    if (!sf.ui.proTools.getMenuItem("View", "Edit Window Views", "Comments").isMenuChecked) {
        sf.ui.proTools.menuClick({
            menuPath: ["View", "Edit Window Views", "Comments"],
        });
    }
    
    while (true) {
        // Ask for search query
        const searchQuery = 
        
    /// example of search list   ("word1 word2 word3")
        ("s")
    
        // Format query to RegEx
        const searchQueryRegEx = new RegExp(searchQuery.split(" ").join("|").replace(/^/, "\\b(").replace(/$/, ")\\b"), "i");
    
        // Deselect all tracks
        sf.ui.proTools.trackDeselectAll();
    
        // Search visible tracks for search query
        for (var i = 0; i < visibleTracks.length; i++) {
            var trackCommentHasSearch = sf.ui.proTools.trackGetByName({ name: visibleTracks[i] }).track.textFields.first.value.value.match(searchQueryRegEx);
    
            if (trackCommentHasSearch) {
                if (!firstTrackFound) { // Scroll to first track found
                    sf.ui.proTools.trackGetByName({ name: visibleTracks[i], }).track.trackScrollToView();
                    firstTrackFound = !firstTrackFound;
                } else {
                    sf.ui.proTools.trackSelectByName({ names: [visibleTracks[i]], deselectOthers: false });
                }
                foundTracks.push(visibleTracks[i]);
            }
        }
        if (foundTracks.length !== 0) {
            break;
        } else if (!confirm("No tracks found. Try another search?")) {
            break;
        }
    }
    
    
    sf.ui.proTools.appActivate();
    
    sf.ui.proTools.selectedTrack.trackScrollToView();
    
    
    sf.wait({
        intervalMs: 500,
    });
    
    
    sf.ui.proTools.selectedTrack.trackOutputSelect({
        outputPath: ["bus","bus menu 129-256","SNARES▲ (Stereo)"],
        selectForAllSelectedTracks: true,
    });
    
    
    
    Solved in post #2, click to view
    • 4 replies
    1. Hey @Masa_Fukui,

      First of all, welcome to SoundFlow! I'm impressed you're tackling a script of this complexity so early on. That's awesome!

      If I understood you correctly, the script below should do the trick.

      All you have to do is update the searchQueriesAndOutputPaths object at the top with all the search queries and output paths you'd like and this script will run through them all.

      If it can't find a track with a search query in the comments, it will skip to the next one.

      Let me know how it goes!

      const searchQueriesAndOutputPaths = [
          { searchQuery: "k", outputPath: ["bus", "bus menu 129-256", "KICK▲ (Stereo)"] },
          { searchQuery: "s", outputPath: ["bus menu 129-256", "SNARES▲ (Stereo)"] }
      ];
      
      function selectTracksThatContainStringInComments({ searchQuery }) {
          const searchQueryRegEx = new RegExp(searchQuery
              .split(" ")
              .join("|")
              .replace(/^/, "\\b(")
              .replace(/$/, ")\\b"),
              "i"
          );
      
          sf.ui.proTools.appActivateMainWindow();
          sf.ui.proTools.mainWindow.invalidate();
      
          // Make sure we're on the Edit window
          if (!sf.ui.proTools.getMenuItem("Window", "Edit").isMenuChecked) {
              sf.ui.proTools.menuClick({
                  menuPath: ["Window", "Edit"],
              });
          }
      
          // Make sure comments are visible
          if (!sf.ui.proTools.getMenuItem("View", "Edit Window Views", "Comments").isMenuChecked) {
              sf.ui.proTools.menuClick({
                  menuPath: ["View", "Edit Window Views", "Comments"],
              });
          }
      
          sf.ui.proTools.trackDeselectAll();
      
          // Search visible tracks comments for search query
          sf.ui.proTools.trackGetVisibleTracks().names.forEach(trackName => {
              const trackCommentHasSearch = sf.ui.proTools.trackGetByName({ name: trackName })
                  .track.textFields.first.value.value
                  .match(searchQueryRegEx);
      
              if (trackCommentHasSearch) {
                  sf.ui.proTools.trackSelectByName({
                      names: [trackName],
                      deselectOthers: false
                  });
              }
          });
      }
      
      function trackScrollToView() {
          const selectedTracks = sf.ui.proTools.selectedTrackNames;
          sf.ui.proTools.selectedTrack.trackSelect();
      
          sf.ui.proTools.selectedTrack.trackScrollToView();
      
          sf.ui.proTools.trackSelectByName({
              names: selectedTracks
          });
      }
      
      function routeTrackOutput({ outputPath }) {
          sf.ui.proTools.appActivateMainWindow();
      
          trackScrollToView();
      
          sf.ui.proTools.selectedTrack.trackOutputSelect({
              outputPath,
              selectForAllSelectedTracks: true,
          });
      }
      
      function main() {
          searchQueriesAndOutputPaths.forEach(query => {
              const { searchQuery, outputPath } = query;
      
              selectTracksThatContainStringInComments({
                  searchQuery,
              });
      
              if (!sf.ui.proTools.selectedTrackCount) return;
      
              routeTrackOutput({ outputPath });
          });
      }
      
      main();
      
      Reply1 LikeSolution
      1. MMasa Fukui @Masa_Fukui
          2022-08-25 02:20:30.616Z

          Hello Raphael!!

          Thank you very much for your fast reply!!
          The script is amazing....All problems gone, and I am happy mixing!!
          I know professionals like you can solve these obstacles no problem, but it's very hard for me to overcome even just a little thing like this skipping and go arounds...

          I'm wondering where I can study more like this Java Script other than this forum which is very helping enough for us to get started and write some scripts for shortcuts and automations.

          Because, right now, I am just Puzzled with these const, RegExp, and all that unknown codes...

          1. Happy to hear that!

            This playlist by Steve Griffith on YouTube is a great resource to quickly learn all the Javascript basics.

            Also, https://www.w3schools.com/js/ helped me a lot when I was starting out!

            It might seem like a lot at first, but I suggest focusing on learning one new concept every day, and before you know it, everything will start to make sense!

            1. MMasa Fukui @Masa_Fukui
                2022-08-25 17:27:50.589Z

                I'm already digging those resources!! It seems a long way to go, but certainly interesting!!
                Thanks for sharing!!