Skip to main content
ChristianAsprenoGLOBE
Participant
July 21, 2021
Question

Indesign script to automatically insert document title metadata based on file name

  • July 21, 2021
  • 5 replies
  • 2267 views

Hi

I've been having problems with client changing file names when sending back amends and not knowing which job or version the file relates to. 

Is there a simple script which will insert the file name into the document title metadata field? Or it could go into the descriptin field if easier. That way even if they chage the file title I can still find the original job title

On save/export would be great if possible?

Thanks so much! 

This topic has been closed for replies.

5 replies

Legend
July 22, 2021

Hello @ChristianAsprenoGLOBE 

 

The below script will write the file name into the document title field every time the document is Saved or Save As.

Save this script here: Applications⁩ ▸ ⁨Adobe InDesign ▸ ⁨Scripts⁩ ▸ ⁨startup scripts⁩ folder and restart Indesign:

#targetengine "session"

function saveFileMetaData() {
    if(app.layoutWindows.length == 0) return;
    else if (!app.activeDocument.saved) {
        alert('Sorry, there was a problem and the document was not saved.');
        return;
        }
   doc = app.activeDocument; 
   var fileName = doc.name.replace(/.indd$/i, "");
   var myMetaData = doc.metadataPreferences;
   myMetaData.documentTitle = fileName;
}

app.addEventListener('afterSave', saveFileMetaData, false);
app.addEventListener('afterSaveAs', saveFileMetaData, false);

 

Regards,

Mike

rob day
Community Expert
Community Expert
July 22, 2021

Hi @Mike Bro, I think @ChristianAsprenoGLOBE wants the original file name saved to metadata—the current file name (doc.name) could be different.

Legend
July 22, 2021

Hi @rob day 

I see you point, but if the script is only installed on the OP's computer the document title meta data would be whatever they last Saved or Saved As before sending the file off to their client.

 

Regards,

Mike

rob day
Community Expert
Community Expert
July 21, 2021

Also, the downside of saving the original name as a keyword, is the client could edit the keywords after the name change. If that’s a potential problem the original name could be inserted into the RAW XMP data as a custom node:

 

 

 

rob day
Community Expert
Community Expert
July 21, 2021

This has actually come up before. You can try saving this script into your Scripts>startup scripts folder and restart ID:

 

 

 

var el = app.eventListeners.add("afterSave", onSaveKW );

/**
* Function to run after a Save
*  the event 
*/
function onSaveKW(evt){
    var kws = app.activeDocument.metadataPreferences.keywords;
    var kwp = "oname"
    var n = evt.parent.name
    if (!checkKeywords(kws, kwp)) {
        kws.unshift("oname:" + n)
        app.activeDocument.metadataPreferences.keywords = kws
    } 
}

/**
* Checks if a keyword starting with oname: exists 
*  the keyword array to check 
*  the prefix string 
*  false if keyword does not exist 
* 
*/
function checkKeywords(a, p){
    var b = false
    for (var i = 0; i < a.length; i++){
        if (a[i].split(":")[0] == p) {
            b=true
        } 
    }  
    return  b
}

 

 

 

The startup script listens for an after save event, and sets the doc name as a keyword, if it’s the first save the name gets added as oname:YourFileName.indd.

 

Here I created a new file and named it MyIDFile.indd and then did a save as with a different name and the file info keeps the original name:

 

brian_p_dts
Community Expert
Community Expert
July 21, 2021
Derek Cross
Community Expert
Community Expert
July 21, 2021

Have a look at: File > File Info, to see if that would be of any help.