GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Very basic file/document versioning

Hi all,

First post in the forum! Here's a script I've been using to keep track of old versions of files or documents, particularly big binary files that don't compress well like lossy image formats or the (already) compressed XML MS office formats. I've found this pretty handy for cases where I don't want to bother with dealing with git-lfs. The script copies selected files into a time-stamped subfolder and logs their names + a commit message into a TOML file that can be read by other programs. Cheers!

Script code for reference:

function OnClick(clickData) {
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection

    cmd.RunCommand("CreateFolder Versions");
    var mydate = DOpus.Create.Date().Format("D#yyyy-MM-dd");
    var mytime = DOpus.Create.Date().Format("T#hh-mm-ss");
    var dt = "D" + mydate + "-T" + mytime;
    // archive_file = "Version-" + dt + ".zip";
	archive_folder = "Version-" + dt;
    archive_path = "Versions\\" + archive_folder;

    var dlg = DOpus.Dlg;
    dlg.window = clickData.func.sourcetab;
    dlg.template = "Message"
    var message = dlg.Control("msgEdit").value
    DOpus.Output("Return code = " + dlg.result);

    if (dlg.result == 0) {

    var sourcepath = DOpus.FSUtil.Resolve(clickData.func.sourcetab.path);

    fso = new ActiveXObject("Scripting.FileSystemObject");
	var listing_path = sourcepath + "\\Versions\\Listing.toml"
    CreateFile = true;
    ForAppending = 8;
    CreateFile = true;
    f1 = fso.OpenTextFile(listing_path, ForAppending, CreateFile);

    f1.Write("[versions." + dt + "]\r");
    f1.Write("archive = \"" + archive_folder + "\"\r");

    if (message.length > 0) {   
        f1.Write("message = \"" + message + "\"\r");

    f1.Write("files = [\r");

	// --------------------------------------------------------
	if (clickData.func.sourcetab.selected.count > 0) {
		for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())	{
            var fpath = eSel.item().RealPath + ""; // force string type
            DOpus.Output("file: " + fpath);
            f1.Write("  \"" + fpath.replace(/\\/g,"/") + "\",\r");
    DOpus.Output(cmd.RunCommand("Copy UNATTENDED=yes CREATEFOLDER=" + archive_path + " HERE"));
	<resource name="Message" type="dialog">
		<dialog fontsize="8" height="58" lang="english" standard_buttons="ok,cancel" width="177">
			<control halign="left" height="8" name="static1" title="Commit Message" type="static" width="60" x="7" y="7" />
			<control halign="left" height="13" name="msgEdit" type="edit" width="168" x="5" y="22" />

Nice script!

I noticed it didn't work when in special folders like Desktop, and have edited the root post to fix that. It just needed a call to FSUtil.Resolve adding.

You could probably avoid the need for a dialog resource here as well, since simple request UIs (e.g. ones that only ask for a string) can be done just by using the dlg object. But I've left that as-is as there's nothing wrong with doing it this way either (other than it's more work!).

1 Like