Title
recalling a preset based on track comment to multiple tracks
What do you expect to happen when you run the script/macro?
This will form part of a larger script I am already using for setting up sessions.
This function will check each track for a comment containing TP: then recall a corrisponding track preset to that track and any others with the same comment.
The first for loop builds an array of all the track presets that need to be recalled.
the second loop (combined with the second nested loop) is currently just trying to select all the tracks with the same comment (Ill add the bit to recall the track preset later)
Are you seeing an error?
What happens when you run this script?
Currently nothing. it wont run I think ive broken something when trying to fix it.
Previosly it was not selecting the correct tracks it would select the same tracks for each preset reguardless of comment
How were you running this script?
I clicked the "Run Script" or "Run Macro" button in SoundFlow
How important is this issue to you?
3
Details
{ "inputExpected": "This will form part of a larger script I am already using for setting up sessions. \n\nThis function will check each track for a comment containing TP: then recall a corrisponding track preset to that track and any others with the same comment. \n\nThe first for loop builds an array of all the track presets that need to be recalled. \n\nthe second loop (combined with the second nested loop) is currently just trying to select all the tracks with the same comment (Ill add the bit to recall the track preset later) ", "inputIsError": false, "inputWhatHappens": "Currently nothing. it wont run I think ive broken something when trying to fix it. \n\nPreviosly it was not selecting the correct tracks it would select the same tracks for each preset reguardless of comment ", "inputHowRun": { "key": "-MpfwYA4I6GGlXgvp5j1", "title": "I clicked the \"Run Script\" or \"Run Macro\" button in SoundFlow" }, "inputImportance": 3, "inputTitle": "recalling a preset based on track comment to multiple tracks " }
Source
sf.ui.proTools.appActivateMainWindow();
sf.ui.proTools.windows.invalidate();
function recallTrackPresetByComment() {
// Make sure comments are visible
if (!sf.ui.proTools.getMenuItem("View", "Edit Window Views", "Comments").isMenuChecked) {
sf.ui.proTools.menuClick({
menuPath: ["View", "Edit Window Views", "Comments"],
});
}
const visibleTracks = sf.ui.proTools.trackGetVisibleTracks().names
var trackPresetsToRecall = [];
for (let i = 0; i < visibleTracks.length; i++) {
var trackcomment = sf.ui.proTools.trackGetByName({ name: visibleTracks[i] }).track.textFields.first.value.value
if (trackcomment != "" && trackPresetsToRecall.includes(trackcomment) == false && trackcomment.startsWith("TP:")) {
trackPresetsToRecall.push(trackcomment);
}
}
for (let i = 0; i < trackPresetsToRecall.length; i++) {
sf.ui.proTools.trackDeselectAll();
for (let j = 0; j < visibleTracks.length; j++) {
var trackcomment = sf.ui.proTools.trackGetByName({ name: visibleTracks[j] }).track.textFields.first.value.value
if (trackcomment == trackPresetsToRecall[i]){
sf.ui.proTools.trackSelectByName(({names:[visibleTracks[j]], deselectOthers:false}))
log(visibleTracks[i])
}
}
}
}
recallTrackPresetByComment();
Links
User UID: ygOIcvnHDlfKp28uvAXMIpsOUTx2
Feedback Key: sffeedback:ygOIcvnHDlfKp28uvAXMIpsOUTx2:-NnJmFMsOu1vQzx28Ewf
Feedback ZIP: 2FD/nCfXS/osWDi2VfqqR88N06a4SP4PQipximISHNWDlJbVu6oAFq3QaDgR+gNSlLRaGJFElLNTaUiYG1NBw+1B+6Mt3dZYdV5dQ12iNuW21Zy1H96al6MUa5Jj9tqFlZXoF8xtciZjehNd16tKcC97dT603PQ8rSeB3wr73vwktlEbj6zLrQouxCuBCfLO+2lHnyEdWmhD+sNIBtAYkFu1lzIfq3nBEfiWQI7saHSPHNLuLQ5/xwfpyo6SrfFO1J340vlwEXKZz0BLBpwbQkK9bCES3GgUlVbAr1Ajt4bXmMs69jRLCCOotFugK/TE7JnPZ+SKrXoOtx8eB43nEuDfLV+WtcGTsB6GWOQI6Jo=
- In reply toJack_Green⬆:Raphael Sepulveda @raphaelsepulveda2024-01-09 01:14:02.124Z
@Jack_Green, give this a shot.
It first gathers a list of all the tracks that have comments starting with "TP:" and categorizes them by comment, which I assume is something like "TP: GTR". It then goes through all the track preset categories created earlier and selects the tracks that have it in the comments, ready to do something with them. I left it open for you to add the recall track preset script of your choice.
function recallTrackPresetByComment() { sf.ui.proTools.appActivateMainWindow(); sf.ui.proTools.windows.invalidate(); // Make sure comments are visible if (!sf.ui.proTools.getMenuItem("View", "Edit Window Views", "Comments").isMenuChecked) { sf.ui.proTools.menuClick({ menuPath: ["View", "Edit Window Views", "Comments"], }); } // Create a map of tracks that contain "TP:". // Keys are track preset comments, values are string arrays containing track names const trackPresetsToRecall = sf.ui.proTools.visibleTrackHeaders.reduce((acc, track) => { const trackComment = track.textFields.first.value.value; if (trackComment && trackComment.startsWith("TP:")) { if (!acc[trackComment]) acc[trackComment] = []; acc[trackComment].push(track.normalizedTrackName); } return acc; }, {}); // Select tracks that contain the same Track Preset comments and apply Track Preset for (const trackPreset in trackPresetsToRecall) { const trackNames = trackPresetsToRecall[trackPreset]; sf.ui.proTools.trackSelectByName({ names: trackNames }); // Insert code to Recall Track Preset here // Use trackPreset to have access to the track comment log({ trackPreset, trackNames }); } } recallTrackPresetByComment();
Jack Green @Jack_Green
Thanks that worked perfectly ! :)
- In reply toJack_Green⬆:Jack Green @Jack_Green
@raphaelsepulveda could you please explain how this code filters the array to contain each element only once? if you don't mind of course :)
if (trackComment && trackComment.startsWith("TP:")) { if (!acc[trackComment]) acc[trackComment] = []; acc[trackComment].push(track.normalizedTrackName); }
Im looking todo something similar in another script pushing things into an array but only if they do not already exist in the array
Raphael Sepulveda @raphaelsepulveda2024-02-02 19:33:50.519Z
Hey @Jack_Green, sure thing.
What we're doing here is populating an object with properties (not an array).
First, we're checking if the object contains a property with a key that matches the track comment of the track in question. If there isn't one, it'll create it and set its value to an empty array. 👇🏼
if (!acc[trackComment]) acc[trackComment] = [];
Now that we've guaranteed that there is an array to put things into, we can push the track's name into it. 👇🏼
acc[trackComment].push(track.normalizedTrackName);
So basically we're just checking if it exists or not and then proceeding from there.
Im looking todo something similar in another script pushing things into an array but only if they do not already exist in the array
I think the following two examples would be more helpful for you:
- Say you have an array that has duplicated values (
arr1
). You can useforEach
to check if a value already exists in another array (arr2
) and if it doesn't, push it in:
const arr1 = ["a", "b", "b", "c", "d"]; const arr2 = []; arr1.forEach(letter => { if (!arr2.includes(letter)) { arr2.push(letter) } }); log(arr2);
- Or you can use this technique to remove any duplicates from an array:
const arr1 = ["a", "b", "b", "c", "d"]; const arr2 = [...new Set(arr1)]; log(arr2);
Hopefully, that answers your question!
- Say you have an array that has duplicated values (