List Tags and Labels

This script scans the selection for tags and labels. It lists the tags and labels it has found in the log panel and creates two dated text files with them in the source. Folders get scanned recursively. Reading metadata is time-consuming, you will need a bit of patience when scanning many files, especially media files.

// https://resource.dopus.com/t/list-tags-and-labels/41360

// 2022-05-31

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var tagSet = DOpus.Create().StringSet();
    var labelSet = DOpus.Create().StringSet();
    var currDate = DOpus.Create().Date().Format('D#yyyyMMdd-T#HHmmss');
    cmd.deselect = false;

    function GetTagsFromItem(item) {
        var itemTags = item.metadata.tags;
        if (typeof itemTags == 'undefined') return;
        if (itemTags.count == 0) return;
        for (var e = new Enumerator(itemTags); !e.atEnd(); e.moveNext()) {
            tagSet.insert(e.item());
        }
    }

    function GetLabelsFromItem(item) {
        var itemLabels = item.Labels();
        if (typeof itemLabels == 'undefined') return;
        if (itemLabels.count == 0) return;
        for (var e = new Enumerator(itemLabels); !e.atEnd(); e.moveNext()) {
            labelSet.insert(e.item());
        }
    }

    if (tab.selected.count == 0) return;

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

    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (item.is_dir) {
            var folderEnum = fsu.ReadDir(item, 'r')
            while (!folderEnum.complete) {
                var folderItem = folderEnum.Next();
                if (folderItem.is_dir) continue;
                GetTagsFromItem(folderItem);
                GetLabelsFromItem(folderItem);
            }
        } else {
            GetTagsFromItem(item);
            GetLabelsFromItem(item);
        }
    }

    DOpus.Output('\n... done!');

    var tagsFile = fso.CreateTextFile(tab.path + '\\Tags-' + currDate + '.txt');

    DOpus.Output('\n\n*** Tags:\n');
    if (tagSet.empty) {
        DOpus.Output('<<<none>>>');
        tagsFile.WriteLine('<<<none>>>');
    } else {
        for (var e = new Enumerator(tagSet); !e.atEnd(); e.moveNext()) {
            DOpus.Output(e.item());
            tagsFile.WriteLine(e.item());
        }
    }
    tagsFile.Close();

    var labelsFile = fso.CreateTextFile(tab.path + '\\Labels-' + currDate + '.txt');

    DOpus.Output('\n\n*** Labels:\n');
    if (labelSet.empty) {
        DOpus.Output('<<<none>>>');
        labelsFile.WriteLine('<<<none>>>');
    } else {
        for (var e = new Enumerator(labelSet); !e.atEnd(); e.moveNext()) {
            DOpus.Output(e.item());
            labelsFile.WriteLine(e.item());
        }
    }
    labelsFile.Close();
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>List Tags and Labels</label>
	<tip>Scan selection for tags and labels</tip>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/list-tags-and-labels/41360</instruction>
		<instruction />
		<instruction>// 2022-05-31</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    var fso = new ActiveXObject(&apos;Scripting.FileSystemObject&apos;);</instruction>
		<instruction>    var tagSet = DOpus.Create().StringSet();</instruction>
		<instruction>    var labelSet = DOpus.Create().StringSet();</instruction>
		<instruction>    var currDate = DOpus.Create().Date().Format(&apos;D#yyyyMMdd-T#HHmmss&apos;);</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    function GetTagsFromItem(item) {</instruction>
		<instruction>        var itemTags = item.metadata.tags;</instruction>
		<instruction>        if (typeof itemTags == &apos;undefined&apos;) return;</instruction>
		<instruction>        if (itemTags.count == 0) return;</instruction>
		<instruction>        for (var e = new Enumerator(itemTags); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>            tagSet.insert(e.item());</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    function GetLabelsFromItem(item) {</instruction>
		<instruction>        var itemLabels = item.Labels();</instruction>
		<instruction>        if (typeof itemLabels == &apos;undefined&apos;) return;</instruction>
		<instruction>        if (itemLabels.count == 0) return;</instruction>
		<instruction>        for (var e = new Enumerator(itemLabels); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>            labelSet.insert(e.item());</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    if (tab.selected.count == 0) return;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction>    DOpus.Output(&apos;Enumerating...&apos;);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        if (item.is_dir) {</instruction>
		<instruction>            var folderEnum = fsu.ReadDir(item, &apos;r&apos;)</instruction>
		<instruction>            while (!folderEnum.complete) {</instruction>
		<instruction>                var folderItem = folderEnum.Next();</instruction>
		<instruction>                if (folderItem.is_dir) continue;</instruction>
		<instruction>                GetTagsFromItem(folderItem);</instruction>
		<instruction>                GetLabelsFromItem(folderItem);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else {</instruction>
		<instruction>            GetTagsFromItem(item);</instruction>
		<instruction>            GetLabelsFromItem(item);</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n... done!&apos;);</instruction>
		<instruction />
		<instruction>    var tagsFile = fso.CreateTextFile(tab.path + &apos;\\Tags-&apos; + currDate + &apos;.txt&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n\n*** Tags:\n&apos;);</instruction>
		<instruction>    if (tagSet.empty) {</instruction>
		<instruction>        DOpus.Output(&apos;&lt;&lt;&lt;none&gt;&gt;&gt;&apos;);</instruction>
		<instruction>        tagsFile.WriteLine(&apos;&lt;&lt;&lt;none&gt;&gt;&gt;&apos;);</instruction>
		<instruction>    } else {</instruction>
		<instruction>        for (var e = new Enumerator(tagSet); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>            DOpus.Output(e.item());</instruction>
		<instruction>            tagsFile.WriteLine(e.item());</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction>    tagsFile.Close();</instruction>
		<instruction />
		<instruction>    var labelsFile = fso.CreateTextFile(tab.path + &apos;\\Labels-&apos; + currDate + &apos;.txt&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n\n*** Labels:\n&apos;);</instruction>
		<instruction>    if (labelSet.empty) {</instruction>
		<instruction>        DOpus.Output(&apos;&lt;&lt;&lt;none&gt;&gt;&gt;&apos;);</instruction>
		<instruction>        labelsFile.WriteLine(&apos;&lt;&lt;&lt;none&gt;&gt;&gt;&apos;);</instruction>
		<instruction>    } else {</instruction>
		<instruction>        for (var e = new Enumerator(labelSet); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>            DOpus.Output(e.item());</instruction>
		<instruction>            labelsFile.WriteLine(e.item());</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction>    labelsFile.Close();</instruction>
		<instruction>}</instruction>
	</function>
</button>

5 Likes

Thanks for this! Can you point me to a method for having it match the filenames to the labels/tags output? I ran it through a large directory and while it enumerates the tags, I don't know which belongs to which file.

The idea of the script is to be a help in situations like this:

https://resource.dopus.com/t/working-with-tags-tag-list-of-all-tags-available/41355/1

Once you have the tags, you can easily FIND files that carry them:

Additionally, you could modify the script to automatically start a search with the tags it finds.

Tags are also visible as a column and in the metadata pane.

1 Like

Thanks!

If you want an offline list of files and their tags, you can find all files with * (use wildcards) as a tag, then turn on the column which shows them, and use Tools > Print / Export Folder Listing to save it to a text file.

(The CSV or TSV formats are best, as then you don't have to worry about column widths, and you can import them into Excel or Google Sheets. Make sure the Export dialog is set up to include the columns you need as well. Full Path is probably one you would want.)

2 Likes