Script: Create archive from selected records

I needed my account statements in a ZIP archive to send them to my tax accountant. So I came up with this (Java)script. It packs the selected records in a ZIP file whose name is chosen interactively. Only the bare file names are used in the ZIP, no (DT internal!) paths. Maybe it’s useful for someone else as well.

// Create an archive from the selected DT records

(() => {
const app = Application("DEVONthink 3");
app.includeStandardAdditions = true;

// curApp needed to use doShellScript
const curApp= Application.currentApplication();
curApp.includeStandardAdditions = true;

// Get the paths of the selected records
const paths = app.selectedRecords.path();

if (paths.length === 0 ) { //Adjust language
  app.displayAlert("Bitte mindestens einen Datensatz auswählen");
}

// Get target archive name
var zipPath = app.chooseFileName({
    withPrompt: "Archiv speichern unter:" // Adjust language
});

// Append .zip extension if necessary
if (! /\.zip$/.test(zipPath)) {
  zipPath += ".zip";
}
// Protect spaces and quotes in paths
const pathList = paths.map(p => { 
  return `'${p.replaceAll(/'/g, "'\\''")}'`; 
});

const zipCommand = `zip -j '${zipPath}' ${pathList.join(' ')}`;

curApp.doShellScript(zipCommand);

})()

The script contains two strings in German that users of other languages might want to adjust. The first tells the user to select at least one record (the script stops right after that message), the second one asks for the name/location of the ZIP archive.

1 Like

Very nice. I imagine someone will find use in this.