CopyMeta (Copy metadata between files)

This add-in creates the Script Command CopyMeta. It copies metadata from one file to one or more other files.

JScript
function OnInit(initData) {
    initData.name = 'CopyMeta';
    initData.version = '2022-09-03';
    initData.copyright = 'CopyMeta';
    initData.url = 'https://resource.dopus.com/t/script-command-copymeta-copy-metadata-between-files/42137';
    initData.desc = 'Copy metadata from the first selected file to other files';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'CopyMeta';
    cmd.method = 'OnCopyMeta';
    cmd.desc = 'CopyMeta';
    cmd.label = 'CopyMeta';
    cmd.template = 'createdate/s,lastmodifieddate/s,comment/s,rating/s,gps/s,tags/s,labels/s';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnCopyMeta(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var dtab = scriptCmdData.func.desttab;
    var dlg = scriptCmdData.func.Dlg();
    var args = scriptCmdData.func.args;

    cmd.deselect = false;

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

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

    var item = tab.selected(0);
    if (item.metadata == 'none') {
        DOpus.Output(item);
        DOpus.Output('... contains no metadata.');
        return;
    }
    
    var all = scriptCmdData.func.argsmap.empty;
    var tmp;
    var cmdLine = '';

    cmd.Clear();

    if (all || args.createdate) {
        tmp = item.create.Format('D#yyyy-MM-dd T#HH:mm:ss');
        cmdLine += ' "createdate:' + tmp + '"';
    }

    if (all || args.lastmodifieddate) {
        tmp = item.modify.Format('D#yyyy-MM-dd T#HH:mm:ss');
        cmdLine += ' "lastmodifieddate:' + tmp + '"';
    }

    if (all || args.comment) {
        tmp = item.metadata.other.usercomment;
        if (typeof tmp == 'undefined') tmp = '';
        cmdLine += ' "comment:' + tmp + '"';
    }

    if (all || args.rating) {
        tmp = item.metadata.other.rating;
        if (typeof tmp != 'number') tmp = 0;
        cmdLine += ' rating:' + tmp;
    }

    if (all || args.gps) {
        tmp = item.metadata.image.latitude;
        if (typeof tmp != 'number') tmp = 0;
        cmdLine += ' gpslatitude:' + tmp;

        tmp = item.metadata.image.longitude;
        if (typeof tmp != 'number') tmp = 0;
        cmdLine += ' gpslongitude:' + tmp;

        tmp = item.metadata.image.altitude;
        if (typeof tmp != 'number') tmp = 0;
        cmdLine += ' gpsaltitude:' + tmp;
    }

    if (all || args.tags) {
        tmp = '';
        if (item.metadata.tags.count > 0) {
            for (var e = new Enumerator(item.metadata.tags); !e.atEnd(); e.moveNext()) {
                tmp += e.item() + ';';
            }
            tmp = tmp.slice(0, -1);
        }
        cmdLine += ' "tags:' + tmp + '"';
    }

    if (cmdLine != '') {
        cmdLine = 'SetAttr META' + cmdLine;
        DOpus.Output(cmdLine);
        cmd.AddLine(cmdLine);
    }

    if (all || args.labels) {
        tmp = '';
        var itemLabels = item.Labels();
        if (itemLabels.count > 0) {
            for (var e = new Enumerator(itemLabels); !e.atEnd(); e.moveNext()) {
                tmp += e.item() + ',';
            }
            tmp = tmp.slice(0, -1);
        }
        var cmdLine = 'Properties SETLABEL="' + tmp + '"';
        DOpus.Output(cmdLine);
        cmd.AddLine(cmdLine);
    }

    if (dtab && dtab.selected.count > 0) {
        cmd.SetFiles(dtab.selected);
    } else {
        cmd.RemoveFile(item); // We leave the file untouched whose metadata we copy
    }

    if (cmd.filecount == 0) {
        DOpus.Output('');
        DOpus.Output('No copy targets selected.');
        return;
    }

    // Outcomment the following line, if the dialog gets in the way
    if (dlg.Request('Copy metadata from\n\n' + item.name + '\n\nto ' + cmd.filecount + ' files?') == 0) return;

    cmd.Run();

    DOpus.Output('');
    DOpus.Output('Metadata copied from:');
    DOpus.Output(item);
    DOpus.Output('');
    DOpus.Output('To:');
    for (var e = new Enumerator(cmd.files); !e.atEnd(); e.moveNext()) {
        DOpus.Output(e.item());
    }
}

How to install

Pasting this into Windows' "Save As" dialog will take you directly to the right destination:

%appdata%\GPSoftware\Directory Opus\Script AddIns
  • Create a button

How to use

CopyMeta will copy metadata from the first selected file in the source to the selected files in the destination. If there is no destination or no selected files in it, the metadata will be copied to the other selected files in the source.

Which metadata will be copied is determined by these switches, which should be self-explanatory:

CopyMeta CREATEDATE LASTMODIFIEDDATE COMMENT RATING GPS TAGS LABELS

The switch GPS copies gpslatitude, gpslongitude, and gpsaltitude.

The switches are optional. Providing no switch is equivalent to providing all.

The log panel will show the actual commands before they are executed and a dialog will ask for confirmation.

CopyMeta can be used in a context menu. It should be kept in mind, that the metadata will be taken from the first file of the selection, not from the file that was clicked.

Your favorite metadata is not on the list? Let me know, and it might appear in a future revision :slight_smile:


9 Likes

Thank you so much dear lxp. This Script is very useful to me. Please create one more script for find and replace metadata for the selected files. which should have

  1. a combo list for select metadata field name.
  2. a find what popup box for input text what should be find in selected metadata field
  3. a Replace Popup box for input text which should be replace
    That would be very very useful for me and like other user like me. Regards
1 Like