No internet connection
  1. Home
  2. How to

Reselect Visible Audio Tracks from current track selection

By Forrester Savell @Forrester_Savell
    2023-03-02 08:43:42.426Z

    Can anyone help me modify this code so that it only selects visible Audio tracks? Works great currently to select only Audio Tracks, but includes any hidden Audio Tracks, which throws an error in my script.

        let selectedAudioTracks = sf.ui.proTools.invalidate().trackGetSelectedTracks().trackHeaders.filter
            (t => t.title.value.includes("Audio Track")).map(t => t.normalizedTrackName);
    
        sf.ui.proTools.trackSelectByName({names: selectedAudioTracks, deselectOthers: true})
    

    I've found plenty of code for selecting visible tracks, but can't seem to integrate it with pre-selected audio tracks.

    Thanks for you help.

    Forrester

    Solved in post #2, click to view
    • 5 replies
    1. Chris Shaw @Chris_Shaw2023-03-02 22:23:44.553Z2023-03-02 22:59:37.165Z

      This should do it:

      // Get all selected track headers
      let allSelectedTrackHeaders = sf.ui.proTools.invalidate().trackGetSelectedTracks().trackHeaders;
      
      //Filter to get only visible audio tracks
      let allVisibleSelectedAudioTrackNames = allSelectedTrackHeaders.filter(t => t != null && t.title.value.endsWith("- Audio Track ")).map(t => t.normalizedTrackName);
      
      //Select only previously selected visible audio tracks
      sf.ui.proTools.trackSelectByName({ names: allVisibleSelectedAudioTrackNames, deselectOthers: true });
      
      Reply1 LikeSolution
      1. FForrester Savell @Forrester_Savell
          2023-03-02 22:33:28.335Z

          Thanks so much @Chris_Shaw

          A follow up noob question - what's the difference between trackHeaders and trackNames? I've seen them both used to select tracks?

          1. Chris Shaw @Chris_Shaw2023-03-02 22:59:19.235Z2023-03-02 23:08:01.371Z

            A Track Header contains all of the UI / Accessibility information of a selected track (buttons, labels, track type, dimensions and position of each track element, etc). Track names are just that - track names.

            The track header information generally looks something like this:

            "{\"title\":\"Leslie EG 57 - Audio Track \",\"role\":\"AXGroup\",\"fullRole\":\"AXGroup\"}"
            

            As you can see the title not only contains the track name but also the track type.

            Track headers can only be obtained from visible tracks which is why your original script would throw an error. If you try to grab the header of a hidden track the command returns null.
            The first command returns an array of track headers for the selected tracks (which will include null for every hidden track) and puts that array in allSelectedTrackHeaders
            The second command looks at every entry in allSelectedTrackHeaders and returns only those headers that aren't null and whose title ends with "- Audio Track " then takes those entries and returns the normalized track name (the track title without the track type) and stores the results in allVisibleSelectedAudioTrackName.

          2. In reply toChris_Shaw:

            re-edited the code above to make it slightly faster

            1. (three steps instead of four).