Sync ratings between files

This script synchronizes the ratings of the selected items (files and folders) in the source with those in the destination that have the same name. If two files have different ratings, the larger rating will be copied to the item with the lower rating.

The script will work in collections, but not in Flatview.

// https://resource.dopus.com/t/sync-ratings-between-files/42802

// 2022-11-14

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dtab = clickData.func.desttab;
    var fsu = DOpus.FSUtil();

    cmd.deselect = false;

    if (!dtab) return;

    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.ClearOutput();

    DOpus.Output('Enumerating...\n');

    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {
        var srcItem = e.item();
        if (srcItem.metadata == 'none') continue;

        var dstItem = fsu.GetItem(fsu.Resolve(dtab.path + '\\' + srcItem.name));
        if (!fsu.Exists(dstItem)) continue;
        if (dstItem.metadata == 'none') continue;

        var srcRating = srcItem.metadata.other.rating;
        if (typeof srcRating == 'undefined') srcRating = 0;

        var dstRating = dstItem.metadata.other.rating;
        if (typeof dstRating == 'undefined') dstRating = 0;

        if (srcRating == dstRating) continue;

        cmd.ClearFiles();

        if (srcRating > dstRating) {
            newRating = srcRating;
            cmd.AddFile(dstItem);
            DOpus.Output(dstItem);
        } else {
            newRating = dstRating;
            cmd.AddFile(srcItem);
            DOpus.Output(srcItem);
        }

        var cmdLine = ('SetAttr META rating:' + newRating);
        DOpus.Output(cmdLine);
        DOpus.Output('');
        cmd.RunCommand(cmdLine);
    }

    DOpus.Output('\n... done.');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>SyncRatings</label>
	<icon1>#properties</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/sync-ratings-between-files/42802</instruction>
		<instruction />
		<instruction>// 2022-11-14</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var dtab = clickData.func.desttab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction />
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (!dtab) return;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;Enumerating...\n&apos;);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var srcItem = e.item();</instruction>
		<instruction>        if (srcItem.metadata == &apos;none&apos;) continue;</instruction>
		<instruction />
		<instruction>        var dstItem = fsu.GetItem(fsu.Resolve(dtab.path + &apos;\\&apos; + srcItem.name));</instruction>
		<instruction>        if (!fsu.Exists(dstItem)) continue;</instruction>
		<instruction>        if (dstItem.metadata == &apos;none&apos;) continue;</instruction>
		<instruction />
		<instruction>        var srcRating = srcItem.metadata.other.rating;</instruction>
		<instruction>        if (typeof srcRating == &apos;undefined&apos;) srcRating = 0;</instruction>
		<instruction />
		<instruction>        var dstRating = dstItem.metadata.other.rating;</instruction>
		<instruction>        if (typeof dstRating == &apos;undefined&apos;) dstRating = 0;</instruction>
		<instruction />
		<instruction>        if (srcRating == dstRating) continue;</instruction>
		<instruction />
		<instruction>        cmd.ClearFiles();</instruction>
		<instruction />
		<instruction>        if (srcRating &gt; dstRating) {</instruction>
		<instruction>            newRating = srcRating;</instruction>
		<instruction>            cmd.AddFile(dstItem);</instruction>
		<instruction>            DOpus.Output(dstItem);</instruction>
		<instruction>        } else {</instruction>
		<instruction>            newRating = dstRating;</instruction>
		<instruction>            cmd.AddFile(srcItem);</instruction>
		<instruction>            DOpus.Output(srcItem);</instruction>
		<instruction>        }</instruction>
		<instruction />
		<instruction>        var cmdLine = (&apos;SetAttr META rating:&apos; + newRating);</instruction>
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        DOpus.Output(&apos;&apos;);</instruction>
		<instruction>        cmd.RunCommand(cmdLine);</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n... done.&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>


2 Likes