Author Topic: Attachments names in a metadata field  (Read 5523 times)

0 Members and 1 Guest are viewing this topic.

Pavel

  • Guest
Attachments names in a metadata field
« on: April 16, 2012, 01:20:17 AM »
I very like PDF–ShellTools. It is best affordable program for working with Attachments. – But here I need help – please, tell me how can I automatically load the names of Attachments (from the Attachments list) to the Details pane Items and InfoTip. Now I have created a Custom Field Attachments\Name: PDFCustom1\Type: Text and I´m manually writhing the names of Attachments to file(-s) via Properties\Details pane.

I have WIN 7 Ultimate 64-bit.

RTT

  • Administrator
  • *****
  • Posts: 907
Re: Attachments names in a metadata field
« Reply #1 on: April 16, 2012, 01:38:36 AM »
I suppose a script, with the help of the ListAttachments and SetMetadata command line interface functions, could do the job.

Something like this:
Code: [Select]
var WshShell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1,
    ForWriting = 2,
    ForAppending = 8;
var TristateUseDefault = -2,
    TristateTrue = -1,
    TristateFalse = 0;

function getPDFShellToolsPath() {
    var oEnv = WshShell.Environment("System");
    switch (oEnv("PROCESSOR_ARCHITECTURE").toLowerCase()) {
    case "amd64":
        return "\"%ProgramFiles(x86)%\\PDF-ShellTools\\PDFShellTools.exe\"";
    default:
        return "\"%ProgramFiles%\\PDF-ShellTools\\PDFShellTools.exe\"";
    }
}

function AttachmentsListToMetadataField(filename) {
    if (fso.GetExtensionName(filename) === 'pdf') {
        var FilePath = fso.GetParentFolderName(filename);
        var AttachmentsListFilename = FilePath + "\\Attachments.txt";
        if (fso.FileExists(AttachmentsListFilename)) {
            fso.DeleteFile(AttachmentsListFilename);
        }
        var ShellToolsPath = getPDFShellToolsPath();
        WshShell.Run(ShellToolsPath + " ListAttachments \"OutFilename=" + AttachmentsListFilename + "\" \"" + filename + "\"", 0, true);
        if (fso.FileExists(AttachmentsListFilename)) {
            var ts = fso.OpenTextFile(AttachmentsListFilename, ForReading, false, TristateFalse);

            var Attachments = new Array;
            while (!ts.AtEndOfStream) {
                Attachments.push(ts.ReadLine());
            }
            ts.Close();
            fso.DeleteFile(AttachmentsListFilename);
            WshShell.Run(ShellToolsPath + " SetMetadata -UTF8Encoded \"Metadata='Attachments=" + Attachments.join(';') + "'\" \"" + filename + "\"", 0, true);        }
    }
}

var objArgs = WScript.Arguments;
for (i=0; i<objArgs.length; i++){
AttachmentsListToMetadataField(objArgs(i));
}
Save this code to an AttachmentsToMetadata.js file and put it in the SendTo folder (type shell:sendto in the Windows Explorer address bar to easily locate, and open, this folder).
Now you just need to select the PDF files, right click them, and choose the Shell context menu "send to>AttachmentsToMetadata.js" item, to get these PDF files attachments names saved to a PDF custom metadata field, named Attachments.