No internet connection
  1. Home
  2. How to

General Toggle Action for two or more commands?

By Jon Shamieh @Jon_Shamieh
    2019-12-28 20:13:14.615Z

    Hi, I am wondering if there is a general toggle action I can use to switch between commands. You helped me create a script to toggle commands on this thread:
    https://forum.soundflow.org/-443/how-to-toggle-between-smart-tool-trim-tce-and-smart-tool-trim-standard

    but I am wondering if there is an easy way I can build them in the future. I want to toggle between these two scrips for example: "Set Nudge to Timecode 1 second" and "Set Nudge to Timecode 1 Frame"

    Solved in post #2, click to view
    • 6 replies
    1. Hi Jon.

      For others reading this, this also refers to the first few paragraphs of my comment here:
      https://forum.soundflow.org/-1435#post-2

      You can do a generic toggle action by using a global state variable. That is a variable that will keep its value when the script is called again.

      You would do something like this (pseudo-code):

      • Set the state variable to the opposite of what it was (if it was true, set to false, if it was false, set to true).
      • Check if the state variable is true - then do task A
      • or else (if it was false) - do task B.

      In Javascript, that looks like this:

      //This reverses the value of globalState.myVariable
      globalState.myVariable = !globalState.myVariable;
      
      if (globalState.myVariable) {
          //Do task A
      } else {
          //Do task B
      }
      

      You would then insert the two actions you want to toggle between where it says "Do task A" / "Do task B" (at a new line under those comments).

      If you use this in other scripts, make sure to use something other than "myVariable"

      ReplySolution
      1. As mentioned in my previous comment here the downside of using this approach is that the script doesn't know what's going on in Pro Tools.
        So the script will toggle back and forth between executing command A and command B regardless of the state inside Pro Tools.
        So where this approach - a general toggle - enables you to generally make a toggle action, it is recommended to use logic that reads the actual state to determine which action to take, whenever possible (as we did in the post you were referring to).

        1. JJon Shamieh @Jon_Shamieh
            2019-12-28 21:45:40.178Z

            Gotcha, thank you. Although I do understand in theory what you are saying, it's a little over my head at the moment as far as how to implement via a script - for example, I am not sure why this doesn't work:

            //This reverses the value of globalState.myVariable
            globalState.myVariable = !globalState.myVariable;

            if (globalState.myVariable) {
            //sf.ui.proTools.nudgeSet({
            value: "00:00:01:00.00",
            });
            } else {
            //sf.ui.proTools.nudgeSet({
            value: "00:00:00:01.00",
            });
            }

            But it's OK for now, I think I'll be able to get it eventually, there's just a learning curve I am battling at the moment :)

            1. Just remove the double slashes //
              They mean "ignore everything after the slashes on this line"

              1. JJon Shamieh @Jon_Shamieh
                  2019-12-28 22:19:37.298Z

                  that worked!

                  1. Needed this today... this was one of my forum stops to find it, figured I'd put the 'check status version' here for anybody else who went looking with these key terms. Thanks to @samuel_henriques from another posting (How to change specific Pro Tools nudge values (in msec)) for most of this code.

                    sf.ui.proTools.appActivateMainWindow();
                    
                    const nudgeBtn = sf.ui.proTools.mainWindow.gridNudgeCluster.nudgeControls
                    const nudgeValue = nudgeBtn.nudgeValue.value.invalidate().value
                    
                    if (nudgeValue === "00:00:00:01.00") {
                        sf.ui.proTools.nudgeSet({
                            value: '00:00:01:00.00',
                        });
                    } else {
                        sf.ui.proTools.nudgeSet({
                            value: '00:00:00:01.00',
                        });
                    }