If you change some little things, it would be faster. Have in mind the script will be running always on the background
function on_metadb_changed(handle_list, fromhook){
if (fromhook && bHasPlugin && !bFieldCheck) {SynchronizeRatingTags(handle_list);}
}
const bHasPlugin = utils.CheckComponent('foo_playcount'); // Call it once! Not on every function call. All these things should be constant during a foobar instance
const bFieldCheck = utils.ReadTextFile(`${fb.FoobarPath}\\LargeFieldsConfig.txt`).includes('fieldSpam=rating');
const sourceTfo = fb.TitleFormat('[%rating%]');
const targetTfo = fb.TitleFormat('$meta(rating)');
function SynchronizeRatingTags(handle_list){ // This part would be faster if you evalmtdb with the entire list at once, and then update all files
handle_list.Convert().filter(handle => sourceTfo.EvalWithMetadb(handle) !== targetTfo.EvalWithMetadb(handle)).forEach(handle => {
new FbMetadbHandleList(handle).UpdateFileInfoFromJSON(JSON.stringify([{['rating']: sourceTfo.EvalWithMetadb(handle)}]));
});
}
To be honest, I get a weird popup message saying it's not possible to update a doppelganger track in a directory that I don't even have on my pc, but I'm pretty sure it's not coming from the script, probably from something messed-up in my index-data files
You could check the files exist before updating the tags, pretty sure some weird bugs could appear if you don't do so.
Also check if the files are in the library, I don't think you want to change tags to other files.
let relative_paths = handle_list.GetLibraryRelativePaths();
if (fb.IsMetadbInMediaLibrary(handle)) {
...
}
Note the relative paths function outputs "" for files not on library, so it can be used as a "IsMetadbInMediaLibrary" check too, while having the path at the same time (if your use-case is only one library folder).
You can also use TF to get absolute paths along the rating, and split the string to get both
const sourceTfo = fb.TitleFormat('[%rating%]###%path%');
,,,
let stringArray= tfo.EvalWithMetadbs(handle_list);
let stringArray_length= stringArray.length;
for (let i = 0; i < stringArray_length; i++) {
let [rating, path] = stringArray[i].split('###');
//do your thing with ratings and path here
}
And obviously you can merge both TF at once and compare the 2 values
const sourceAndTargetTfo = fb.TitleFormat('[%rating%])###$meta(rating)###%path%');
...
let [rating, ratingtag, path] = stringArray[i].split('###');
Also you could use TF directly to check if [%rating%] equals $meta(rating) and return true.
const Tfo = fb.TitleFormat('$ifequal(%rating%,$meta(rating),%path%,)')
That would output an array of the files which need re-tagging, with empty values for those who don't. The you can filter it easily... and do your thing with those paths
let stringArray= tfo.EvalWithMetadbs(handle_list).filter(Boolean)