No internet connection
  1. Home
  2. How to

bulk rename tracks with extensions

By Andreas Altmann @Andreas_Altmann
    2023-01-14 06:02:47.236Z

    Hey,
    This is the first time I'm trying to write a script/macro. I wouldn't even know if this falls under a macro or a script?

    I'm trying to bulk rename tracks with specific extensions on a new playlists.
    For example "guitar" -> new playlist -> instead of "guitar.01" I'd want "guitar.edit".

    So far this is what I think the order is:
    1.highlight tracks
    2.new playlist
    3.rename first track
    4.This needs to repeat for every track highlighted
    [Right arrow
    delete, delete
    write “edit” (copy and paste “edit”)
    enter]

    Once I know how to even start on one of these I think I can figure out other ones.
    Thank you so much!
    Any help is greatly appreciated.

    • 3 replies
    1. Chris Shaw @Chris_Shaw2023-01-14 19:17:43.515Z2023-01-15 06:29:15.049Z

      Generally speaking you shouldn't use keyboard presses in your scripts/macros. Instead, you should use UI and menu click commands whenever possible. This makes them more bullet proof.
      This should do it:
      ( if you're running PT 20.12 then this script will take advantage of the new PT SDK to rename the tracks - it's much faster and concise).

      function getProToolsVersion() {
          let proToolsVersion = sf.ui.proTools.appVersion;
          let splitVersion = proToolsVersion.split('.');
          let versionYear = splitVersion[0];
          let versionMonthString = splitVersion[1];
          let correctedVersionMonth = versionMonthString[0] != '1' ? "0" + versionMonthString.slice(-2) : versionMonthString;
          let proToolsVersionString = versionYear + '.' + correctedVersionMonth;
          let proToolsVersionAsNumber = Number(proToolsVersionString);
          return proToolsVersionAsNumber;
      }
      
      // Switch to PT and clear cache
      sf.ui.proTools.appActivateMainWindow();
      sf.ui.proTools.mainWindow.invalidate();
      
      //Check if PT version is SDK Compatible
      const isPTSDKCompatible = (getProToolsVersion() >= 21.12);
      
      // Get selected Tracks
      const selectedTracks = sf.ui.proTools.selectedTrackNames;
      
      //Scroll to first selected track and reselect selectedTracks
      sf.ui.proTools.trackSelectByName({ names: [selectedTracks[0]] });
      sf.ui.proTools.selectedTrack.trackScrollToView();
      sf.ui.proTools.trackSelectByName({ names: selectedTracks });
      
      // Create new playlist for all selected tracks
      const playlistSelector = sf.ui.proTools.selectedTrack.popupButtons.whoseTitle.is("Playlist selector").first;
      
      playlistSelector.popupMenuSelect({
          menuPath: ["New..."],
          isShift: true,
          isOption: true
      });
      
      // If only one track is selected, wait for confirmation dialog and click ok
      if (selectedTracks.length == 1){
          const confirmationDlg = sf.ui.proTools.confirmationDialog;
          confirmationDlg.elementWaitFor();
          confirmationDlg.buttons.whoseTitle.is("OK").first.elementClick();
      }
      
      // If PT  version is not SDK compatible use this routine to bulk rename tracks. Else use new PT SDK
      if (!isPTSDKCompatible) {
          // Open Batch rename window
          sf.ui.proTools.selectedTrack.titleButton.popupMenuSelect({
              isRightClick: true,
              menuPath: ["Batch Rename..."],
          });
      
          // Make reference to Batch Rename window
          const batchRenameWindow = sf.ui.proTools.windows.whoseTitle.is("Batch Track Rename").first;
      
          // Wait for batch rename window to open
          batchRenameWindow.elementWaitFor();
      
          // Disable all checkboxes
          const batchCheckBoxes = batchRenameWindow.checkBoxes.map(cb => cb.checkboxSet({
              targetValue: "Disable"
          }));
      
          // Define checkboxes to set
          const batchCheckBoxesToSet = {
              "Replace": "Enable",
              "Clear Existing Name": "Disable",
              "Regular Expressions": "Enable"
          }
      
          //Set checkboxes 
          for (let key in batchCheckBoxesToSet) {
              batchRenameWindow.checkBoxes.whoseTitle.is(key).first.checkboxSet({ targetValue: batchCheckBoxesToSet[key] })
          };
      
          //Set Regular Expression to define text to be replaced (everything from the last "." onward)
          batchRenameWindow.textFields.first.elementSetTextFieldWithAreaValue({
              value: "\.[^.]+$",
          });
      
          // Set replacement text
          batchRenameWindow.textFields.allItems[1].elementSetTextFieldWithAreaValue({
              value: ".edit",
          });
      
          // click "OK" close batch rename window to batch rename selected tracks
          batchRenameWindow.buttons.whoseTitle.is("OK").first.elementClick();
      
          //Wait for batch rename window to close
          batchRenameWindow.elementWaitFor({ waitForNoElement: true });
      
      } else {
          // We're using  21.12 or later - use new SDK commands to rename tracks
          //   Get new track / playlist names
          const newPlaylistNames = sf.ui.proTools.selectedTrackNames;
      
          //Rename tracks
          newPlaylistNames.forEach(track => {
              sf.app.proTools.renameTrack({
                  oldName: track,
                  newName: track.replace(/\.[^.]+$/, ".edit"),
                  onError: "Continue"
              })
          });
      }
      
      1. A
        In reply toAndreas_Altmann:
        Andreas Altmann @Andreas_Altmann
          2023-01-24 06:34:46.731Z

          Hi Chris,
          I obviously must be doing something wrong. I copy and pasted what you wrote and put it in as a new script.

          It starts a new playlist of all highlighted tracks and then it says TypeError " Cannot read property of "renametrack" "undefined

          I run Pro Tools Ultimate 2022.4.0

          I tried modifying your code...it doesn't do anything then.
          Keep in mind, I have -100 code skills,..

          Your help is greatly appreciated.

          Thanks,
          Andy

          1. AAndreas Altmann @Andreas_Altmann
              2023-02-02 05:23:13.595Z

              Hi Chris,
              I'm finally getting the hang of this it seems. My novice brain couldn't get your code to work, so I researched and came up with something that included making presets in the Batch Rename window.
              This script copies clips/regions to a new playlist and uses one of my batch rename preset to rename .01 to "edit".
              Hope this helps other folks.

              //Make sure Pro Tools Edit Window is active
              sf.ui.proTools.appActivateMainWindow();

              //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", });

              /** @param {string} presetMenuPath */
              function batchTrackRenameWithPreset(presetMenuPath) {
              // Identify Batch Track Rename window
              const batchTrackRenameWin = sf.ui.proTools.windows.whoseTitle.is("Batch Track Rename").first;

              // Launch Batch Track Rename
              sf.keyboard.press({ keys: "alt+shift+r" });
              
              // Wait for Batch Track Rename to appear
              batchTrackRenameWin.elementWaitFor();
              
              // Select preset
              batchTrackRenameWin.children.whoseRole.is("AXMenuButton").whoseTitle.is("Librarian menu").first.popupMenuSelect({
                  menuPath: [presetMenuPath],
              });
              
              // Click "OK" and wait for window to close
              batchTrackRenameWin.buttons.whoseTitle.is("OK").first.elementClick();
              batchTrackRenameWin.elementWaitFor({ waitForNoElement: true });
              

              }

              batchTrackRenameWithPreset('Edit');