No internet connection
  1. Home
  2. How to

Incrementing a new or Duplicated Playlist with numbers

By Andrew Downes @Andrew_Downes
    2020-10-21 23:12:45.042Z

    Inspired by the "Incrementing a Duplicated Track Name" thread, I am wodering if we can apply this to Playlists.
    I have manged to bodge together a start by using some of Christian's code from the duplicate track rename post, and some from Andrew Scheps incrementing a playlist.

    
    //Get the current track/playlist name
    var oldPlaylistName = sf.ui.proTools.selectedTrackNames[0];
    
    sf.ui.proTools.trackSelectByName({ names: [oldPlaylistName], deselectOthers: false });
    sf.ui.proTools.selectedTrack.trackBulkRenameNumerical();
    
    //Duplicate selected track playlist
    sf.ui.proTools.selectedTrack.popupButtons.whoseTitle.is('Playlist selector').first.popupMenuSelect({
        menuPath: ["New..."],
    });
    
    //Wait for Duplicate Playlist window to appear
    sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Appear",});
    
    //Click OK in Duplicate Playlist window
    sf.ui.proTools.confirmationDialog.buttons.whoseTitle.is('OK').first.elementClick();
    
    //Wait for Duplicate Playlist window to disappear
    sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Disappear",});
    
    //Get and Increment Last Number
    function getAndIncrementLastNumber(str) { 
        return str.replace(/\d+$/, function (s) {
            return ++s;
        });
    }
    
    //Variable for new clip name
    var newPlaylistName = getAndIncrementLastNumber(oldPlaylistName);
    
    //Rename Track with Old Name + Incrimented value
    sf.ui.proTools.selectedTrack.trackRename({
        newName: newPlaylistName,
    });
    

    Where I am getting stuck, it seems, is after renaming the original playlist with a number, e.g "Guitar" goes to "Guitar 1". the new playlist is just called "Guitar" again. I'd like it to go to "Guitar 2" and so on.
    None of this is a problem if a number already exsists in the trac name.
    Thank you

    • 4 replies
    1. A
      Andrew Downes @Andrew_Downes
        2020-10-23 03:15:30.887Z

        Hi All
        I think I have managed to work it out for myself.
        The following script will,
        Add a number to the track name if there isn't one (1)
        Make a Duplicate of the playlist
        Increment that number to 2, and so on
        If there is a number already, it will ignore the first bit.
        If you remove the word "Duplicate" from line 14 and add "New", it will create a new playlist.

        I'm still working on getting it to work over multiple tracks.

        sf.ui.proTools.appActivateMainWindow();
        
        if (!sf.ui.proTools.selectedTrack.normalizedTrackName.match(/\d+$/)) {
            sf.ui.proTools.selectedTrack.trackRename({
                renameFunction: n => n + ' 1',
            });
        }
        
        //Get the current track/playlist name
        var oldPlaylistName = sf.ui.proTools.selectedTrackNames[0];
        
        //Duplicate selected track playlist
        sf.ui.proTools.selectedTrack.popupButtons.whoseTitle.is('Playlist selector').first.popupMenuSelect({
            menuPath: ["Duplicate..."],
        });
        
        //Wait for Duplicate Playlist window to appear
        sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Appear",});
        
        //Click OK in Duplicate Playlist window
        sf.ui.proTools.confirmationDialog.buttons.whoseTitle.is('OK').first.elementClick();
        
        //Wait for Duplicate Playlist window to disappear
        sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Disappear",});
        
        //Get and Increment Last Number
        function getAndIncrementLastNumber(str) { 
            return str.replace(/\d+$/, function (s) {
                return ++s;
            });
        }
        
        //Variable for new clip name
        var newPlaylistName = getAndIncrementLastNumber(oldPlaylistName);
        
        //Rename Track with Old Name + Incrimented value
        sf.ui.proTools.selectedTrack.trackRename({
            newName: newPlaylistName,
        });
        

        Thanks

        1. Andrew - thanks so much for sharing this!

          1. You can use this code to do a loop through all selected tracks:

            
            function doForAllSelectedTracks(action) {
                var originallySelectedTrackNames = sf.ui.proTools.selectedTrackNames;
            
                try {
                    sf.ui.proTools.selectedTrackHeaders.forEach(track => {
                        track.trackSelect();
                        action(track);
                    });
                }
                finally {
                    sf.ui.proTools.trackSelectByName({ names: originallySelectedTrackNames });
                }
            }
            
            function trackFunc() {
            
                //Insert your code here
            
            }
            
            doForAllSelectedTracks(trackFunc);
            
        2. C
          In reply toAndrew_Downes:
          Christian Best @Christian_Best
            2020-10-23 17:50:23.046Z

            I just tried this and it does relabel all selected tracks,,,,eg. Audio 1 to Audio 2
            Is there a way of selecting the tracks and then Duplicating them then running this on the on the duplicates?

            this is the version with both scripts added together

            function doForAllSelectedTracks(action) {
                var originallySelectedTrackNames = sf.ui.proTools.selectedTrackNames;
            
                try {
                    sf.ui.proTools.selectedTrackHeaders.forEach(track => {
                        track.trackSelect();
                        action(track);
                    });
                }
                finally {
                    sf.ui.proTools.trackSelectByName({ names: originallySelectedTrackNames });
                }
            }
            
            function trackFunc() {
            
                sf.ui.proTools.appActivateMainWindow();
            
            if (!sf.ui.proTools.selectedTrack.normalizedTrackName.match(/\d+$/)) {
                sf.ui.proTools.selectedTrack.trackRename({
                    renameFunction: n => n + ' 1',
                });
            }
            
            //Get the current track/playlist name
            var oldPlaylistName = sf.ui.proTools.selectedTrackNames[0];
            
            //Duplicate selected track playlist
            sf.ui.proTools.selectedTrack.popupButtons.whoseTitle.is('Playlist selector').first.popupMenuSelect({
                menuPath: ["Duplicate..."],
            });
            
            //Wait for Duplicate Playlist window to appear
            sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Appear",});
            
            //Click OK in Duplicate Playlist window
            sf.ui.proTools.confirmationDialog.buttons.whoseTitle.is('OK').first.elementClick();
            
            //Wait for Duplicate Playlist window to disappear
            sf.ui.proTools.confirmationDialog.elementWaitFor({waitType: "Disappear",});
            
            //Get and Increment Last Number
            function getAndIncrementLastNumber(str) { 
                return str.replace(/\d+$/, function (s) {
                    return ++s;
                });
            }
            
            //Variable for new clip name
            var newPlaylistName = getAndIncrementLastNumber(oldPlaylistName);
            
            //Rename Track with Old Name + Incrimented value
            sf.ui.proTools.selectedTrack.trackRename({
                newName: newPlaylistName,
            });
            
            }
            
            doForAllSelectedTracks(trackFunc);
            ```