No internet connection
  1. Home
  2. How to

Create Tracks & Add Comments from CSV File

By Harrison White @Harrison_White
    2023-01-17 15:01:56.580Z

    Hi!

    I have a simple CSV/Excel file containing a table with track names (column A) and track comments (column B). The table has a header row with 'name' in A1 and comment in B1. Using other answers found on this forum, I have managed to find a script that creates the tracks and names them correctly, but I cannot find a way to add in the comments as each track is created. The comments differ for each track - ie each row of the CSV has a unique track name, and associated comment.

    Any help would be much appreciated!

    Solved in post #7, click to view
    • 7 replies
    1. If you're able to share the script here it would be easier to add the code you need

      1. HHarrison White @Harrison_White
          2023-01-18 07:30:11.887Z

          Hi Chris, thanks for the quick response! Here's the code for the 'create & name track' bit. I'm all for simplifying it if it can be done. Cheers

          function createTrack(trackInfo) { const trackName = trackInfo.name
          const trackFormat = trackInfo.format
          
          sf.ui.proTools.menuClick({ menuPath: ["Track", "New..."] });
          
          const newTrackWin = sf.ui.proTools.windows.whoseTitle.is("New Tracks").first;
          newTrackWin.elementWaitFor({}, "New Track Win didn'\t open.");
          
          const [format, type, timeBase] = newTrackWin.popupButtons.map(x => x)
          
          if (format.title.invalidate().value !== trackFormat) { format.popupMenuSelect({ menuPath: [trackFormat] }); }
          if (type.title.invalidate().value !== "Audio Track") { type.popupMenuSelect({ menuPath: ["Audio Track"] }); }
          if (timeBase.title.invalidate().value != "Samples") { timeBase.popupMenuSelect({ menuPath: ["Samples"] }); }
          newTrackWin.textFields.whoseTitle.is("Track Name").first.elementSetTextFieldWithAreaValue({ value: trackName })
          
          newTrackWin.buttons.whoseTitle.is("Create").first.elementClick(); };function readFile(path) {
          let trackObj = []
          let [header, ...lines] = sf.file.readLines({ path: path }).lines
          lines.map(line => {
              let cells = line.split(",");
              trackObj.push({
                 format: cells[0].split("").map((char, i) => i === 0 ? char.toUpperCase() : char).join(""),
                  name: cells[1],
              });
          });
          return trackObj}; function main(path) {
          sf.ui.proTools.appActivateMainWindow();
          sf.ui.proTools.invalidate();
          
          let createdTrackNames = []
          const trackList = readFile(path);
          
          trackList.forEach(trackInfo => {
          
              createTrack(trackInfo);
          
              createdTrackNames.push(sf.ui.proTools.selectedTrackNames[0])
          });
          
          sf.ui.proTools.trackSelectByName({ names: createdTrackNames })}; const filePath = "XXXXXXXXXXXXXXXXX"; main(filePath);
          
          1. Does your CSV include a cell/entry for track format? Using a 2 field CSV with just a track name and a comment causes the script to throw an error because no format (Mono, Stereo) is provided.

            1. Once we get this sorted out it'll be very easy to add the comments to the track

        • H
          In reply toHarrison_White:
          Harrison White @Harrison_White
            2023-01-18 16:08:58.666Z

            Apologies - yes it does.

            Column A: format (all tracks to be mono)
            Column B: name
            Column C: comment

            Thanks

            1. In reply toHarrison_White:

              This should do it - just change the file path:

              function createTrack(trackInfo) {
                  const trackName = trackInfo.name
                  const trackFormat = trackInfo.format
              
                  sf.ui.proTools.menuClick({ menuPath: ["Track", "New..."] });
              
                  const newTrackWin = sf.ui.proTools.windows.whoseTitle.is("New Tracks").first;
                  newTrackWin.elementWaitFor({}, "New Track Win didn'\t open.");
              
                  newTrackWin.elementWaitFor()
              
                  const [format, type, timeBase] = newTrackWin.popupButtons.map(x => x)
                  if (format.title.invalidate().value !== trackFormat) { format.popupMenuSelect({ menuPath: [trackFormat] }); }
                  if (type.title.invalidate().value !== "Audio Track") { type.popupMenuSelect({ menuPath: ["Audio Track"] }); }
                  if (timeBase.title.invalidate().value != "Samples") { timeBase.popupMenuSelect({ menuPath: ["Samples"] }); }
                  newTrackWin.textFields.whoseTitle.is("Track Name").first.elementSetTextFieldWithAreaValue({ value: trackName })
              
                  newTrackWin.buttons.whoseTitle.is("Create").first.elementClick();
              };
              
              function readFile(path) {
                  let trackObj = []
                  let [header, ...lines] = sf.file.readLines({ path: path }).lines
                  lines.map(line => {
                      let cells = line.split(",");
                      trackObj.push({
                          format: cells[0].split("").map((char, i) => i === 0 ? char.toUpperCase() : char).join(""),
                          name: cells[1],
                          comments: cells[2]
                      });
                  });
                  return trackObj
              };
              
              /**
              * @param {any[]} trackList
              */
              function setComments(trackList) { //from Samuel Henriques
              
                  //Reselect new tracks
                  sf.ui.proTools.appActivateMainWindow()
                  sf.ui.proTools.invalidate()
              
                  trackList.forEach(track => {
                      sf.ui.proTools.trackSelectByName({
                          names: [track.name],
                          deselectOthers: false
                      })
                  })
                  //Get selected Tracks
                  const selectedTracks = sf.ui.proTools.selectedTrackHeaders;
              
                  //Scroll to view first track
                  selectedTracks[0].trackScrollToView()
              
                  // Open rename window
                  selectedTracks[0].popupButtons.first.mouseClickElement({ clickCount: 2 })
              
                  // Wait for rename window
                  sf.ui.proTools.windows.whoseTitle.is(selectedTracks[0].popupButtons.first.value.invalidate().value).first.elementWaitFor();
              
                  //Get the elements of text fields and buttons 
                  const [trackName, comments] = sf.ui.proTools.focusedWindow.invalidate().textFields.map(x => x)
                  const [ok, cncl, pr, next] = sf.ui.proTools.focusedWindow.buttons.map(x => x)
              
                  //loop through selected tracks and set comments
                  selectedTracks.forEach(track => {
                      // search trackList array to get corresponding comment of current track
                      let commentToSet = trackList.find(obj => obj.name === track.normalizedTrackName);
              
                      //Set comments in Track Rename window
                      comments.elementSetTextFieldWithAreaValue({
                          value: commentToSet.comments,
                      });
                      sf.wait({ intervalMs: 5 })
                      next.elementClick()
                  })
                  // Close rename window
                  ok.elementClick()
              }
              
              function main(path) {
                  sf.ui.proTools.appActivateMainWindow();
                  sf.ui.proTools.invalidate();
              
                  let createdTrackNames = []
                  const trackList = readFile(path);
              
                  trackList.forEach(trackInfo => {
                      createTrack(trackInfo);
                      createdTrackNames.push(sf.ui.proTools.selectedTrackNames[0])
                  });
                  sf.ui.proTools.mainWindow.invalidate()
              
                  setComments(trackList)
              
                  sf.ui.proTools.trackSelectByName({ names: createdTrackNames })
              };
              
              //////////////
              // M A I N ///
              //////////////
              
              const filePath = "/Users/cshaw340/Desktop/test csv 2.csv"
              main(filePath)
              
              ReplySolution
              1. HHarrison White @Harrison_White
                  2023-01-19 15:04:05.839Z

                  Spot on! Thanks, Chris - works great!