No internet connection
  1. Home
  2. How to

Duplicate Multiple Tracks Using A Script?

By Philip weinrobe @Philip_weinrobe
    2022-11-20 15:35:59.726Z

    Hi There Geniuses!
    I have this amazing script that y'all helped me write and it works perfectly for duplicating a single track, renaming the duplicate tracks to be numerically sequential, and bringing over the correct track attributes. Now I am hoping to figure out how to modify it for working with multiple tracks. For example, if i select the tracks "Gtr Amp" and "Gtr Room" and then run this script, currently it will make all the new tracks have the name and attributes from the first selected track. Can I make this go through and create the duplicate process for each of the selected tracks separately?

    And, can the process then put these new duplicates together in the same order that the original tracks were in?
    So if I created 3 duplicates of Gtr Amp and Gtr Room my track list would show

    Gtr Amp 1
    Gtr Room 1
    Gtr Amp 2
    Gtr Room 2
    Gtr Amp 3
    Gtr Room 3
    Gtr Amp 4
    Gtr Room 4

    I think this should be an easy edit of this script...i tried but failed! Any help appreciated :)

    var numberOfDups = Number(prompt('How many duplicates?', '1'));
    
    var originalTrackName = sf.ui.proTools.selectedTrackNames[0];
    sf.ui.proTools.trackDuplicateSelected({
        duplicateActivePlaylist: false,
        duplicateAlternatePlaylists: false,
        duplicateAutomation: false,
        duplicateGroupAssignments: true,
        duplicateInserts: true,
        duplicateSends: true,
        insertAfterLastSelectedTrack: true,
        numberOfDuplicates: numberOfDups
    });
    
    sf.ui.proTools.trackSelectByName({ names: [originalTrackName], deselectOthers: false });
    sf.ui.proTools.selectedTrack.trackBulkRenameNumerical();
    
    • 13 replies
    1. samuel henriques @samuel_henriques
        2022-11-21 12:09:21.400Z

        Hello Philip,
        try this:

        
        /**
        * @param {number} numberOfDups
        * @param {AxPtTrackHeader} track
        */
        function duplicate(numberOfDups, track) {
        
            const trackName = track.normalizedTrackName;
            //  var originalTrackName = sf.ui.proTools.selectedTrackNames[0];
            sf.ui.proTools.trackDuplicateSelected({
                duplicateActivePlaylist: false,
                duplicateAlternatePlaylists: false,
                duplicateAutomation: false,
                duplicateGroupAssignments: true,
                duplicateInserts: true,
                duplicateSends: true,
                insertAfterLastSelectedTrack: true,
                numberOfDuplicates: numberOfDups
            });
        
        
            sf.ui.proTools.trackSelectByName({ names: [trackName], deselectOthers: false });
            sf.ui.proTools.selectedTrack.trackBulkRenameNumerical();
        };
        
        function main() {
        
            sf.ui.proTools.appActivateMainWindow();
            sf.ui.proTools.invalidate();
        
            var numberOfDups = Number(prompt('How many duplicates?', '1'));
        
            const selectedTrackHeaders = sf.ui.proTools.selectedTrackHeaders;
        
            selectedTrackHeaders.forEach(track => {
        
                track.trackSelect();
                track.trackScrollToView();
        
                duplicate(numberOfDups, track)
        
            });
        
            //Select original tracks
            sf.ui.proTools.trackSelectByName({ names: selectedTrackHeaders.map(th => th.normalizedTrackName) });
        
        };
        
        main();
        
        1. PPhilip weinrobe @Philip_weinrobe
            2022-11-28 15:18:10.045Z

            @samuel_henriques
            this is INCREDIBLE. wow. thank you.
            one last little request (i didn't realize until this worked!)
            can the duplicated tracks be next to each other instead of next to their originals?

            for example --
            Gtr DI 1
            Gtr Amp 1

            ...if duplicated twice

            ..would appear in my track list as:
            Gtr DI 1
            Gtr Amp 1
            Gtr DI 2
            Gtr Amp 2
            Gtr DI 3
            Gtr Amp 3

            thank you!
            philip

            1. samuel henriques @samuel_henriques
                2022-11-28 16:41:03.027Z

                try this,

                
                function main() {
                
                    sf.ui.proTools.appActivateMainWindow();
                    sf.ui.proTools.invalidate();
                
                    var numberOfDups = Number(prompt('How many duplicates?', '1'));
                
                
                    let tracksToDup = sf.ui.proTools.selectedTrackNames;
                
                    sf.ui.proTools.trackDuplicateSelected({
                        duplicateActivePlaylist: false,
                        duplicateAlternatePlaylists: false,
                        duplicateAutomation: false,
                        duplicateGroupAssignments: true,
                        duplicateInserts: true,
                        duplicateSends: true,
                        insertAfterLastSelectedTrack: true,
                        numberOfDuplicates: numberOfDups
                    });
                
                
                
                    const dupTracks = sf.ui.proTools.invalidate().selectedTrackNames;
                
                    //Rename Tracks
                    tracksToDup.forEach(trackName => {
                
                        const trackDups = dupTracks.filter(tn => tn.startsWith(trackName))
                
                        sf.ui.proTools.trackSelectByName({ names: [trackName, ...trackDups] });
                        sf.ui.proTools.selectedTrack.trackBulkRenameNumerical();
                
                    });
                
                };
                
                main();
                
                1. PPhilip weinrobe @Philip_weinrobe
                    2022-12-01 20:54:38.550Z

                    dang, samuel. you a true genius.
                    thank you!

                    1. DDaniel Knobler @Daniel_Knobler
                        2022-12-16 04:41:22.076Z

                        Hello Sam,

                        This is an incredibly helpful script! Would it be possible to get a version that also creates groups for each new set of tracks?

                        Using Phil's example, each new set of duplicates would have its own group:
                        Gtr DI 2 and Gtr Amp 2 grouped together
                        Gtr DI 3 and Gtr Amp 3 grouped together.

                        Maybe it prompts for a group prefix name (i.e. I assign the group prefix Gtr and then it makes the group names "Gtr 2" and "Gtr 3")

                        Thanks so much!

                        1. samuel henriques @samuel_henriques
                            2022-12-16 10:10:00.176Z

                            here you go,

                            function main() {
                            
                                sf.ui.proTools.appActivateMainWindow();
                                sf.ui.proTools.invalidate();
                            
                                // ASk how many duplicates
                                var numberOfDups = Number(prompt('How many duplicates?', '1'));
                            
                            
                                for (let i = 0; i < numberOfDups; i++) {
                            
                                    let tracksToDup = sf.ui.proTools.selectedTrackNames;
                            
                                    const [inst, des, num] = tracksToDup[0].split(" ")
                            
                                    let trackNamesToNewGroup = []
                            
                                    sf.ui.proTools.trackDuplicateSelected({
                                        duplicateActivePlaylist: false,
                                        duplicateAlternatePlaylists: false,
                                        duplicateAutomation: false,
                                        duplicateGroupAssignments: false,
                                        duplicateInserts: true,
                                        duplicateSends: true,
                                        insertAfterLastSelectedTrack: true,
                                        numberOfDuplicates: 1
                                    });
                            
                            
                            
                                    const dupTracks = sf.ui.proTools.invalidate().selectedTrackNames;
                            
                            
                                    if (dupTracks.length > 1) {
                                        //Create Group
                                        const groupName = inst + " " + (num ? +num + 1 : 2)
                                        sf.ui.proTools.groupsCreate({ groupName: groupName })
                                    }
                            
                                    //Rename Tracks
                                    tracksToDup.forEach(trackName => {
                            
                                        const trackDups = dupTracks.filter(tn => tn.startsWith(trackName))
                            
                                        sf.ui.proTools.trackSelectByName({ names: [trackName, ...trackDups] });
                                        sf.ui.proTools.selectedTrack.trackBulkRenameNumerical();
                            
                            
                                        trackNamesToNewGroup.push(sf.ui.proTools.invalidate().selectedTrackNames.slice(-1)[0])
                                    });
                            
                                    sf.ui.proTools.trackSelectByName({ names: trackNamesToNewGroup });
                                };
                            };
                            
                            main();
                            
                            1. D2danielkassulke @danielkassulke
                                2022-12-18 06:01:38.385Z

                                Hi @samuel_henriques this is wonderful! Just thought I'd flag that this script runs into a slight problem when creating more than one duplicate. The duplicate group name isn't incrementing, so the script gets stuck naming the 2nd group at line 45.

                                1. samuel henriques @samuel_henriques
                                    2022-12-18 10:58:03.033Z

                                    Could you show me the track names you are using when it fails?
                                    I'm taking the info to create the group name from the track names to duplicate, so there might be an issue there.

                                    1. samuel henriques @samuel_henriques
                                        2022-12-18 11:00:26.014Z

                                        I can reproduce the line 45 error if I try to duplicate tracks where adding 1 to the name would create a track that exists, and the track group name as well. Let me thing Of a way around this.

                                        1. D2danielkassulke @danielkassulke
                                            2022-12-19 20:15:40.270Z2022-12-19 20:40:48.554Z

                                            Hi Samuel, your initial instinct regarding track names was correct. When testing, I just named an audio track "Drums" because it was the first thing that popped into my head for the test. I guess this is only an issue when working with stems or VIs.

                                            1. samuel henriques @samuel_henriques
                                                2022-12-19 20:25:59.406Z

                                                And if you Have a few dup and then make a new dup not from the last, it will fail as well. I'll try to work on a better version that "knows" better what the group and track name should be,

                                2. D
                                  In reply toPhilip_weinrobe:
                                  Daniel Knobler @Daniel_Knobler
                                    2022-12-16 14:23:29.415Z

                                    Fantastic! Thank you so much.

                                    1. PPhilip weinrobe @Philip_weinrobe
                                        2022-12-16 14:38:50.238Z

                                        the script grows even stronger.....

                                        lol.
                                        this is awesome.