.unshift not working as expected
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 });


- Ryan DeRemer @Ryan_DeRemer
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?
Christian Scheuer @chrscheuer2022-05-09 21:22:26.851Z
Christian Scheuer @chrscheuer2022-05-09 21:23:33.604Z
Or, write via spread syntax:
const selTracks = sf.ui.proTools.selectedTrackNames; sf.ui.proTools.trackSelectByName({ deselectOthers: true, names: [ ...selTracks, 'NAME', ], });
Ryan DeRemer @Ryan_DeRemer
@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.
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] });
- In reply tochrscheuer⬆:
Ryan DeRemer @Ryan_DeRemer
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] });
Raphael Sepulveda @raphaelsepulveda2022-05-09 23:10:17.204Z
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);
Ryan DeRemer @Ryan_DeRemer
@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.
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 });