No internet connection
  1. Home
  2. How to

Using Prompt to get a String. Does not work inside an If/Else?

By Owen Granich-Young @Owen_Granich_Young
    2022-01-27 19:49:33.275Z

    @samuel_henriques bugging you again, or anybody who sees the problem really, but you wrote these two scripts.

    LOW PRIORITY I know I've been asking countless quesitons.

    So I'm taking your 'next memory location by name' script from here -> Identical Marker Navigation #post-16

    And because my spelling is TERRIBLE trying to slot in your prompt to pull the memory location name instead from here -> Using Prompt to get Item.map for memory locations? #post-4

    However when I use the below Script, the Prompt Menu only flashs for a brief second.

    function getNextMatchedMemoryLocation(locationName) {
    
        let mainCounter = sf.ui.proTools.getCurrentTimecode().stringValue
    
        //  if main counter is bars beats, remove last three digits, since memory locations list ignores them
        mainCounter.match(/\|/) ? mainCounter = mainCounter.slice(0, -3) : null
    
        let cleanMainCounter = Number(mainCounter.replace(/[^0-9]/g, '').trim())
    
        const memoryLocations = sf.proTools.memoryLocationsFetch().collection["list"].filter(x =>
            Number(x.mainCounterValue.replace(/[^0-9]/g, '').trim()) > cleanMainCounter &&
            x.name.match(locationName)
        );
    
        try {
            sf.ui.proTools.memoryLocationsGoto({
                memoryLocationNumber: memoryLocations[0].number
            });
        } catch (err) { log(`End of location markers containing:\n${locationName}`) }
    }
    
    function prompt(item) {
        let prompt = sf.interaction.popupSearch({
            items: item.map(x => ({ name: x["Name"] }))
        }).item.name
        return prompt
    };
    
    sf.ui.proTools.appActivate();
    
    const ctrlIsEnabled = event.keyboardState.hasControl
    let memoryLocations = sf.proTools.memoryLocationsFetch().collection['List']
    let promptValue = prompt(memoryLocations)
    
    if (globalState.markerName == undefined || ctrlIsEnabled) {
    
        globalState.markerName = promptValue;
    
    } else {
    
        getNextMatchedMemoryLocation(globalState.markerName)
    }
    

    I tried putting the let 's inside the if{} as well.

    HOWEVER, if I take it out and make it it's own standalone script, the prompt works correctly.

    function prompt(item) {
        let prompt = sf.interaction.popupSearch({
            items: item.map(x => ({ name: x["Name"] }))
        }).item.name
        return prompt
    };
    
    
    let memoryLocations = sf.proTools.memoryLocationsFetch().collection['List']
    let promptValue = prompt(memoryLocations)
    
    globalState.markerName = promptValue;
    

    But why? And also, how would I correct that?

    Thanks!
    Owen

    • 10 replies

    There are 10 replies. Estimated reading time: 11 minutes

    1. Kitch Membery @Kitch2022-01-27 20:23:46.735Z

      Hi Owen,

      prompt is a key word reserved for Javascript. It can't be used as the name of the function.

      try this code and you'll understand what I mean;

      const result = prompt("What is your name");
      log(result);
      
      1. Kitch Membery @Kitch2022-01-27 20:32:39.597Z

        I'm not totally sure what this script is meant to achieve however based on what I can see this may work;

        function getNextMatchedMemoryLocation(locationName) {
        
            let mainCounter = sf.ui.proTools.getCurrentTimecode().stringValue;
        
            //  if main counter is bars beats, remove last three digits, since memory locations list ignores them
            mainCounter.match(/\|/) ? mainCounter = mainCounter.slice(0, -3) : null;
        
            let cleanMainCounter = Number(mainCounter.replace(/[^0-9]/g, '').trim());
        
            const memoryLocations = sf.proTools.memoryLocationsFetch().collection["list"].filter(x =>
                Number(x.mainCounterValue.replace(/[^0-9]/g, '').trim()) > cleanMainCounter && x.name.match(locationName)
            );
        
            try {
                sf.ui.proTools.memoryLocationsGoto({
                    memoryLocationNumber: memoryLocations[0].number
                });
            } catch (err) { log(`End of location markers containing:\n${locationName}`) }
        }
        
        function getMemoryLocation(item) {
            let prompt = sf.interaction.popupSearch({
                items: item.map(x => ({ name: x["Name"] }))
            }).item.name
            return prompt;
        };
        
        sf.ui.proTools.appActivate();
        
        const ctrlIsEnabled = event.keyboardState.hasControl;
        let memoryLocations = sf.proTools.memoryLocationsFetch().collection['List'];
        
        
        if (globalState.markerName == undefined || ctrlIsEnabled) {
            let targetMemoryLocation = getMemoryLocation(memoryLocations);
            globalState.markerName = targetMemoryLocation;
        
        } else {
            getNextMatchedMemoryLocation(globalState.markerName);
        }
        
        

        Updated

        1. Kitch Membery @Kitch2022-01-27 20:38:04.075Z

          Just updated this as you were getting the memory locations outside the if block. :-)

          Hope that works. (I've not fully tested it.)

          1. Hmm... with the new rename code it's still just flashing the Prompt menu over here, not letting me search through it...

            1. For reference here the orignal code I'm trying to shoehorn the prompt version into so I don't have to spell :P

              function getNextMatchedMemoryLocation(locationName) {
              
                  let mainCounter = sf.ui.proTools.getCurrentTimecode().stringValue
              
                  //  if main counter is bars beats, remove last three digits, since memory locations list ignores them
                  mainCounter.match(/\|/) ? mainCounter = mainCounter.slice(0, -3) : null
              
                  let cleanMainCounter = Number(mainCounter.replace(/[^0-9]/g, '').trim())
              
                  const memoryLocations = sf.proTools.memoryLocationsFetch().collection["list"].filter(x =>
                      Number(x.mainCounterValue.replace(/[^0-9]/g, '').trim()) > cleanMainCounter &&
                      x.name.match(locationName)
                  );
              
                  try {
                      sf.ui.proTools.memoryLocationsGoto({
                          memoryLocationNumber: memoryLocations[0].number
                      });
                  } catch (err) { log(`End of location markers containing:\n${locationName}`) }
              }
              
              sf.ui.proTools.appActivate();
              
              const ctrlIsEnabled = event.keyboardState.hasControl
              
              if (globalState.markerName == undefined || ctrlIsEnabled) {
              
                  globalState.markerName = prompt(`Please Enter Marker Name`);
              
              } else {
              
                  getNextMatchedMemoryLocation(globalState.markerName)
              }
              
              1. Kitch Membery @Kitch2022-01-27 23:25:23.400Z

                Hmm It was working for me. I'll take a look at it when I get a chance. If @samuel_henriques does not beat me to it :-)

                Rock on!

                1. samuel henriques @samuel_henriques
                    2022-01-28 11:22:58.823Z

                    Hey guys,

                    It looks like it's working, but I'm not sure how stable it is. Let me know how it goes.
                    I made a small change. When you choose a new marker name. it will select the next one from the current position.

                    @Kitch, about the function prompt(item) now that you noted this is reserved, I'll try to remember that and fix it. It's been working so far but I can see now I was lucky. Thank you.

                    function promptMemLocList(item) {
                        let prompt = sf.interaction.popupSearch({
                            items: item.map(x => ({ name: x["Name"] }))
                        }).item.name
                        return prompt
                    };
                    
                    
                    
                    function getNextMatchedMemoryLocation(locationName) {
                    
                        let mainCounter = sf.ui.proTools.getCurrentTimecode().stringValue
                    
                        //  if main counter is bars beats, remove last three digits, since memory locations list ignores them
                        mainCounter.match(/\|/) ? mainCounter = mainCounter.slice(0, -3) : null
                    
                        let cleanMainCounter = Number(mainCounter.replace(/[^0-9]/g, '').trim())
                    
                        const memoryLocations = sf.proTools.memoryLocationsFetch().collection["list"].filter(x =>
                            Number(x.mainCounterValue.replace(/[^0-9]/g, '').trim()) > cleanMainCounter &&
                            x.name.includes(locationName)
                        );
                    
                        try {
                            sf.ui.proTools.memoryLocationsGoto({
                                memoryLocationNumber: memoryLocations[0].number
                            });
                        } catch (err) { log(`End of location markers containing:\n${locationName}`) }
                    }
                    
                    sf.ui.proTools.appActivateMainWindow();
                    
                    
                    const ctrlIsEnabled = event.keyboardState.hasControl
                    
                    if (globalState.markerName == undefined || ctrlIsEnabled) {
                    
                        let memoryLocations = sf.proTools.memoryLocationsFetch().collection['List']
                    
                        const memLocToSearch = promptMemLocList(memoryLocations)
                        sf.ui.proTools.appActivateMainWindow();
                        globalState.markerName = memLocToSearch
                        getNextMatchedMemoryLocation(memLocToSearch)
                    
                    
                    } else {
                    
                        getNextMatchedMemoryLocation(globalState.markerName)
                    }
                    
                    1. Kitch Membery @Kitch2022-01-28 12:29:52.300Z

                      @Owen_Granich_Young & @samuel_henriques you champions!

                      Here's my take on it;

                      This one will only show unique memory location names in the popup search and will refocus Pro Tools when the popup selection closes.

                      function goToNextMatchedMemoryLocation(locationName) {
                          let currentTimecode = sf.ui.proTools.getCurrentTimecode().stringValue;
                      
                          //  if main counter is bars beats, remove last three digits, since memory locations list ignores them
                          currentTimecode.match(/\|/) ? currentTimecode = currentTimecode.slice(0, -3) : null;
                      
                          let cleanMainCounter = Number(currentTimecode.replace(/[^0-9]/g, '').trim());
                      
                          const memoryLocations = sf.proTools.memoryLocationsFetch().collection["list"].filter(x =>
                              Number(x.mainCounterValue.replace(/[^0-9]/g, '').trim()) > cleanMainCounter && x.name.match(locationName)
                          );
                      
                          try {
                              sf.ui.proTools.memoryLocationsGoto({ memoryLocationNumber: memoryLocations[0].number });
                          } catch (err) {
                              log(`End of location markers containing:\n${locationName}`);
                          }
                      }
                      
                      function main() {
                          //Activate Pro Tools Main Window
                          sf.ui.proTools.appActivateMainWindow();
                      
                          //Get Control Modifier state    
                          const ctrlIsEnabled = event.keyboardState.hasControl;
                      
                          if (globalState.markerName == undefined || ctrlIsEnabled) {
                      
                              //Retrieve memory locations
                              let memoryLocations = sf.proTools.memoryLocationsFetch().collection['List'];
                      
                              let memoryLocationNames = memoryLocations.map(ml => ml["Name"]);
                      
                              //Filter out Memory location name duplicates
                              let uniqueMemoryLocationNames = [...new Set(memoryLocationNames)];
                      
                              //Popup Search
                              let targetMemoryLocation = sf.interaction.popupSearch({
                                  items: uniqueMemoryLocationNames.map(name => ({ name: name })),
                              }).item.name
                      
                              sf.ui.proTools.appActivateMainWindow();
                      
                              //Set the globalState marker name
                              globalState.markerName = targetMemoryLocation;
                      
                          } else {
                              //Go to next memory location
                              goToNextMatchedMemoryLocation(globalState.markerName);
                          }
                      }
                      
                      main();
                      

                      Rock on!!

                      1. DAMN @Kitch you FANCY!

                        I go to sleep with a small problem and a couple ideas in my head to fix it and wake up to a full level up on the script!

                        Gonna take it for a spin shortly.

                        Thanks both of you as always!

                  • I was gonna see if it was an iPad stream deck issue, like if I put it on a keyboard trigger it would work. But I'll try the new fancy in a bit.