No internet connection
  1. Home
  2. How to

How to return a single number from a number of conditions

By Iain Anderson @Iain_Anderson
    2020-10-19 22:25:01.879Z

    Hi there, i'm stuck again. My coding inexperience showing here once more.

    Here's a code i'm working on. Happy that all the parts perform as expected but i'm struggling to take it on to the next level as I don't know how to script the outcome i'm after. What i'd like to happen is have a single number extracted from a clipname and have SoundFlow paste the clip on a track name that matches the extracted number.

    Ie, based on the code below... I want a set of commands that perform, if the number slateLast = 1, copy clip to track "slate 1", if number slateLast = 2, copy clip to track "slate 2" and so on.

    In total I have 10 tracks finishing with slateLast = 0, copy to track "slate 10".

    I've reached the stage where I know what I want it to do, i just don't know what the script code would be.

    Any help greatly received!

    Thanks in advance.

    Iain

    sf.ui.proTools.appActivateMainWindow();
    
    //Make sure we have no search in Clips list - if we had, Clip Rename won't work (Pro Tools bug)
    sf.keyboard.press({ keys: 'cmd+shift+d' });
    
    //Clip Clip->Rename...
    sf.ui.proTools.menuClick({ menuPath: ['Clip', 'Rename...'] });
    
    //Wait for 'Name' dialog to come up, assign the dialog to dlg variable
    var dlg = sf.ui.proTools.windows.whoseTitle.is('Name').first.elementWaitFor({}, 'Could not find "Name" dialog').element;
    
    //Find the first text field
    var textField = dlg.groups.whoseTitle.is('Name').first.textFields.first;
    
    //Get the current Name
    var clipName = textField.value.value;
    
    //Close Window
    dlg.buttons.whoseTitle.is('Cancel').first.elementClick();
    
    //Wait for the dialog to close
    dlg.elementWaitFor({ waitForNoElement: true });
    
    //Extract slateNumber
    var str = clipName;
    var slateNumber = str.split("T")[0];
    
    //Extract last number of slateNumber
    var str = slateNumber;
    var slateLast = str.slice(-1);
    
    if (slateLast === "5") {
        sf.ui.proTools.menuClick({
            menuPath: ["Edit", "Copy"],
        });
    
        sf.ui.proTools.trackSelectByName({
            names: ["slate 5"],
        });
    
        sf.ui.proTools.menuClick({
            menuPath: ["Edit", "Paste"],
        });
    
    } else { alert("Sorry slate is not ending in 5") };
    
    • 1 replies
    1. Hi Iain,

      Try this:

      function getClipName() {
          //Make sure we have no search in Clips list - if we had, Clip Rename won't work (Pro Tools bug)
          sf.keyboard.press({ keys: 'cmd+shift+d' });
      
          //Clip Clip->Rename...
          sf.ui.proTools.menuClick({ menuPath: ['Clip', 'Rename...'] });
      
          //Wait for 'Name' dialog to come up, assign the dialog to dlg variable
          var dlg = sf.ui.proTools.windows.whoseTitle.is('Name').first.elementWaitFor({}, 'Could not find "Name" dialog').element;
      
          //Find the first text field
          var textField = dlg.groups.whoseTitle.is('Name').first.textFields.first;
      
          //Get the current Name
          var clipName = textField.value.value;
      
          //Close Window
          dlg.buttons.whoseTitle.is('Cancel').first.elementClick();
      
          //Wait for the dialog to close
          dlg.elementWaitFor({ waitForNoElement: true });
      
          return clipName;
      }
      
      function getSlateNumberWithoutLeadingZeroes() {
          //Extract slateNumber
          var str = getClipName();
          var slateNumber = str.split("T")[0];
      
          //Strip 0's from start of slateNumber
          return slateNumber.replace(/^0+/, '');
      }
      
      function main() {
      
          sf.ui.proTools.appActivateMainWindow();
      
          let slateNumber = getSlateNumberWithoutLeadingZeroes();
      
          sf.ui.proTools.menuClick({
              menuPath: ["Edit", "Copy"],
          });
      
          sf.ui.proTools.trackSelectByName({
              names: ["Slate " + slateNumber],
          });
      
          sf.ui.proTools.menuClick({
              menuPath: ["Edit", "Paste"],
          });
      
      }
      
      main();
      
      

      Note I capitalized the reference to the "Slate 1", "Slate 2" track(s)