No internet connection
  1. Home
  2. How to

sometimes you need to convert strings to a number

By samuel henriques @samuel_henriques
    2021-03-18 15:59:07.215Z2021-03-18 16:35:51.781Z

    Hello everyone,

    I came across this, working on a script that selects the next memory location by matching word on memory location name. My first version of the script, changes main counter to samples and filters memory location by name and location in samples larger than current location. So far from my test it's been reliable, you can find it here.
    Identical Marker Navigation

    In the mean time I made a version that doesn't have to change the main counter, it replaces any symbols separating the numbers on any time of counter and uses them as plain numbers to know witch marker is after current location.

    But I'm finding different behaviours depending if I'm using memoryLocationsFetch or memoryLocationsFetchFromGui or memoryLocationsFetchFromPtx.

    And weird as well in the sense, some counters work but ignore markers or stop working, but work again. But if in time code, never fails. I would expect samples not to fail, but thats not the case.

    I'm hoping it has something to to with the way I'm reading the memory locations, but, so far, can't figure it out.

    Any ideas?

    And as always, thank you so much!!

    heres the last code:

    function findLocationNameMatch(locationName) {
    
        const mainCounter = sf.ui.proTools.getCurrentTimecode().stringValue
        let cleanMainCounter = mainCounter.replace(/[ :\+\|.]/g,'').trim()
    
    
        //  if maulter is bars beats, remove last three digits, since memory locations list ignores them
        mainCounter.match(/\|/) ? cleanMainCounter = cleanMainCounter.slice(0, -3) : null
    
    
    
        const memoryLocations = sf.proTools.memoryLocationsFetch().collection.list.filter(x =>
            x.mainCounterValue.replace(/[ :\+\|.]/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.appActivateMainWindow();
    
    
    findLocationNameMatch("yadda")
    
    
    
    Solved in post #11, click to view
    • 15 replies
    1. samuel henriques @samuel_henriques
        2021-03-18 16:36:38.669Z

        UPDATE: found some errors but didn't improve my problem. Updated script.

        1. Kitch Membery @Kitch2021-03-18 19:02:43.339Z

          Hi Legend!

          Did you try using;

              const mainCounterInSamples = sf.ui.proTools.selectionGetInSamples().selectionStart;
          

          ...to get the samples?

          1. Kitch Membery @Kitch2021-03-18 19:09:20.229Z

            I read your post wrong. ignore my reply above.

            1. In reply toKitch:
              samuel henriques @samuel_henriques
                2021-03-18 19:10:32.941Z

                hey Master Kitch,

                I did as well, thats how the other script is working. But I'm trying to do this without the change Main Counter step. If we remove the symbols, any counter is a number that will fine for this. Do you know how I can find the diferences between memoryLocationsFetch, memoryLocationsFetchFromGui. and memoryLocationsFetchFromPtx?

                I think I'm using the wrong one and I have some mistake on my code (probably Regex) or the order I'm doing something.

                Thanks Kitch!!

                1. Kitch Membery @Kitch2021-03-18 20:02:12.690Z

                  I think the first two return the same object, however the "Ptx" version does not return an object for me at all.

                  Simply logging log(sf.proTools.memoryLocationsFetch()); should show you the differences.

                  You can use this to see code, to see if the regex is returning the result you want..

                  let memoryLoactions = sf.proTools.memoryLocationsFetch().collection['list'].map(l => ({
                      name: l.name,
                      joinedMainCounterReadout: l['MainCounterValue'].replace(/[ :\+\|.]/g, ''),
                  }));
                  
                  log(memoryLoactions);
                  
                  1. samuel henriques @samuel_henriques
                      2021-03-18 20:53:56.423Z

                      thank you Kitch, I'll try this as well.
                      For the "Ptx" to get the object, you need to save the session after creating the location markers. That might be why you'r not getting anything. It took me a few hours a while ago when I first met it, changed so much code to figure what I was doing wrong before noticing, I was working on a code for someone on the forum and immediately added a save line at beginning of the script :)

                      1. Kitch Membery @Kitch2021-03-18 21:14:25.924Z

                        Ahh I see :-)

                        I've never used that one before, that makes complete sense.

                        Try aproaching it like this so you can use the original element to reselect;

                        function getNextMatchedMemoryLocation(name, currentLocationTime) {
                            let memoryLoactions = sf.proTools.memoryLocationsFetch().collection['list'].map(l => ({
                                name: l.name,
                                joinedMainCounterReadout: l['MainCounterValue'].replace(/[ :\+\|.]/g, ''),
                                mainCounterValue: l
                            }));
                        
                            return memoryLoactions
                                .filter(ml => ml.name === name)
                                .filter(ml => ml.joinedMainCounterReadout > currentLocationTime)[0];
                        }
                        
                        let nextMemoryLocation = getNextMatchedMemoryLocation('Location 1', '00000908');
                        
                        log(nextMemoryLocation.mainCounterValue);
                        
                        

                        Let me know if that makes sense.

                        Rock on!

                        1. samuel henriques @samuel_henriques
                            2021-03-19 09:31:15.512Z

                            Hey Kitch,

                            Thank you for your function, it's a bit confusing for my understanding, but it makes sense, and I managed to make it work.
                            It gave similar results to my previous script, witch let me to think, that it's not the memoryLocationsFetch problem, and I changed the regex to [^0-9] witch is simpler but got me the same problems.
                            So I found this:

                            let a = "2859148"
                            
                            let b = "122008320"
                            
                            log(b>a)// false
                            

                            but:

                            
                            let a = "0000002859148"
                            
                            let b = "122008320"
                            
                            
                            log(b>a)  //true
                            

                            this is the mf problem!!!

                            1. samuel henriques @samuel_henriques
                                2021-03-19 09:36:30.370Z

                                still....not the fix

                                1. Kitch Membery @Kitch2021-03-19 09:39:24.254Z

                                  Hi @samuel_henriques,

                                  Have you tried converting the strings to a number first?

                                  let a = Number("2859148");
                                  
                                  let b = Number("122008320");
                                  
                                  log(b>a); //True
                                  

                                  and

                                  let a = Number("0000002859148");
                                  
                                  let b = Number("122008320");
                                  
                                  log(b>a); //True
                                  
                                  Reply1 LikeSolution
                                  1. samuel henriques @samuel_henriques
                                      2021-03-19 09:53:47.610Z

                                      AAAAAHHHHHHH!! That's it. You were so fast I thought there should be a javascript method to fix this and was looking for this solution!!
                                      Thank you so much Master Kitch.

                                      I think I'm only one step away to get this script right.

                                      1. Kitch Membery @Kitch2021-03-19 09:56:19.949Z

                                        Awesome!!

                                        There are many little things like this that once you deal with it once you never forget again :-)

                                        Love your work, Samuel!
                                        Rock on

                                      2. In reply toKitch:

                                        Just to chime in here:
                                        You can also use + instead of Number
                                        So instead of:

                                        let a = Number("0000002859148");
                                        
                                        let b = Number("122008320");
                                        

                                        You can use:

                                        let a = +("0000002859148");
                                        
                                        let b = +("122008320");
                                        

                                        or

                                        let a = +"0000002859148";
                                        
                                        let b = +"122008320";
                                        
                                        log(b>a);
                                        
                                        1. Keep In mind if you want to add the converted string to an existing number you have to use two + symbols…

                                          This combines the two numbers and returns a string

                                          let b = 1+("122008320");
                                          
                                          log(b);// result 1122008320 (Combines and returns a new string. (note the additional "1" at the beginning of result)
                                          

                                          this adds the two numbers

                                          let b = 1+ +("122008320");
                                          
                                          log(b);// result - 122008321. The numbers have been added together
                                          1. samuel henriques @samuel_henriques
                                              2021-03-19 16:58:35.208Z

                                              nice one,
                                              thank you @Chris_Shaw