No internet connection
  1. Home
  2. How to

.unshift not working as expected

By Ryan DeRemer @Ryan_DeRemer
    2022-05-09 20:07:30.567Z

    Hey SF geniuses! So I have a bit of code that I'm vexed by. the goal is to establish the selected tracks as variable, do a bunch of stuff (which includes creating a new folder), then reselect the tracks AND the new folder. The basic action using .unshift is below. What I expect it to do is select the tracks and new folder. The result I'm getting is it selects the new folder, skips the first selected track, and selects the rest of the tracks. Am I missing something? Screenshots below as well.

    const selTracks = sf.ui.proTools.selectedTrackNames;
    
    selTracks.unshift('NAME');
    sf.ui.proTools.trackSelectByName({ deselectOthers: true, names: selTracks });
    
    • 10 replies
    1. Ryan DeRemer @Ryan_DeRemer
        2022-05-09 20:10:22.237Z

        The log also confirms the folder track is replacing the first selected track in the array. I thought .unshift just adds to the beginning of the array?

          1. Or, write via spread syntax:

            const selTracks = sf.ui.proTools.selectedTrackNames;
            
            sf.ui.proTools.trackSelectByName({
                deselectOthers: true,
                names: [
                    ...selTracks,
                    'NAME',
                ],
            });
            
            1. Ryan DeRemer @Ryan_DeRemer
                2022-05-09 21:39:07.718Z

                @chrscheuer Thanks for the reply! I'll try the spread syntax. I tried .push and it just didn't select the extra track. My theory is the array order has to match the track order in PT. I could be wrong but that's the most obvious explanation to me.

                1. Kitch Membery @Kitch2022-05-09 22:16:35.811Z

                  Hi @Ryan_DeRemer,

                  +1 on what Christian suggested (The ES6 Spread Syntax is very handy!). One other thing though... because you have added a track you "may" have to invalidate the Pro Tools window, so it knows where the tracks are newly placed.

                  const selTrackNames = sf.ui.proTools.selectedTrackNames;
                  
                  //Add Folder & do other stuff
                  
                  const newFolderName = "NAME";
                  
                  sf.ui.proTools.mainWindow.invalidate();
                  
                  sf.ui.proTools.trackSelectByName({
                      deselectOthers: true,
                      names: [newFolderName, ...selTrackNames]
                  });
                  
                2. In reply tochrscheuer:
                  Ryan DeRemer @Ryan_DeRemer
                    2022-05-09 22:44:41.710Z

                    Hey @Kitch @chrscheuer. The spread syntax is giving me a syntax error (TypeError: Cannot convert a Symbol value to a string). Any ideas? Nothing's underlined in the editor this error was in the log when I ran it. The script below is the definitions and the final action.

                    const selTracks = sf.ui.proTools.selectedTrackNames;
                    
                    const folderTitle = sf.interaction.displayDialog({
                        defaultAnswer: '',
                        buttons: ['OK', 'Cancel'],
                        defaultButton: 'OK',
                        cancelButton: 'Cancel',
                        title: 'Group, VCA, Route to New Folder',
                        prompt: 'Enter a name for the new folder'
                    }).text;
                    
                    sf.ui.proTools.trackSelectByName({ deselectOthers: true, names: [folderTitle, ...selTracks] });
                    
                    1. Something is a bit funky with the array that selectedTrackNames is returning. Doesn't seem to like the usual array methods. But if you do the following it should work once again—including using .unshift():

                      const selTracks = Array.from(sf.ui.proTools.selectedTrackNames);
                      
                      1. Ryan DeRemer @Ryan_DeRemer
                          2022-05-09 23:21:17.511Z

                          @raphaelsepulveda @Kitch Yes both worked! :) That is weird though. I just updated to PT 2022.4 a couple days ago, is it maybe something to do with the new version? I didn't run into this in 2021.6.

                        • In reply toRyan_DeRemer:
                          Kitch Membery @Kitch2022-05-09 23:12:39.813Z

                          One Sec... :-)

                          1. Kitch Membery @Kitch2022-05-09 23:15:25.849Z

                            As usual, @raphaelsepulveda beat me to it... But here is another way... Just map the Selected Track names to make it an array :-)

                            sf.ui.proTools.appActivateMainWindow();
                            
                            const selTrackNames = sf.ui.proTools.selectedTrackNames.map(tn => tn);
                            
                            const folderTitle = sf.interaction.displayDialog({
                                defaultAnswer: '',
                                buttons: ['OK', 'Cancel'],
                                defaultButton: 'OK',
                                cancelButton: 'Cancel',
                                title: 'Group, VCA, Route to New Folder',
                                prompt: 'Enter a name for the new folder'
                            }).text;
                            
                            const tracksToSelect = [folderTitle, ...selTrackNames];
                            
                            sf.ui.proTools.trackSelectByName({
                                deselectOthers: true,
                                names: tracksToSelect
                            });