Copy link to clipboard
Copied
I want to adjust this script so it saves the INDD filename to the PDF 'Subject' (or keywords, copyright... anything really), not title. What needs to change? The reason is that our filenames are often edited prior to being hosted online, losing our file references, and the Title is edited so it displays as required when selected online. I want to retain our original filename (from the export) so we can trace it at a later date. Ideally, this would be for INDD and AI.
The script was posted by JamesHaney here: https://community.adobe.com/t5/indesign/indesign-filename-into-the-meta-data-automatically/td-p/7020...
–––––––––––––––––––––––––––––––––––––––––
#targetengine 'session'
var myEventListener = app.addEventListener("beforeSave", function (myEvent) {
var doc = app.documents[0];
var docTitle = (doc.name);
var myDocXMP = app.activeDocument.metadataPreferences;
var destNamespace = "http://ns.adobe.com/xap/1.0/";
var destNodeName = "InDesignFileName";
var nodeValue = docTitle;
myDocXMP.setProperty(destNamespace, destNodeName, nodeValue);
}
).name = "storeTitleXMP";
The text editor has to save the code as plain text. Also, make sure you quit InDesign before putting the script in your startup folder, maybe even restart your computer.
Plain text editors can be tricky—here are compiled scripts.
https://shared-assets.adobe.com/link/27806554-f525-44e5-7a20-fcd41d09ff81
I also added an alternate that uses the Keyword array. The original name is the first keyword with a "oname:" prefix:
Copy link to clipboard
Copied
Hi,
The easiest method would just be to change destNodeName to whatever value you would like.
for example - "ANewNodeName"
or add it as a new node
var myEventListener = app.addEventListener("beforeSave", function (myEvent) {
var doc = app.documents[0];
var docTitle = (doc.name);
var myDocXMP = app.activeDocument.metadataPreferences;
var destNamespace = "http://ns.adobe.com/xap/1.0/";
var destNodeName = "InDesignFileName";
var destNodeName2 = "OriginalInDesignFileName";
var nodeValue = docTitle;
myDocXMP.setProperty(destNamespace, destNodeName, nodeValue);
myDocXMP.setProperty(destNamespace, destNodeName2, nodeValue);
}
).name = "storeTitleXMP";
Malcolm
Copy link to clipboard
Copied
Hey Malcom, thanks for the reply!
I've pasted the below into the script, made a new document called 'ONE', exported a PDF (called ONE), then renamed the PDF via finder to 'TWO' and edited the title in the properties to 'THREE'. I'm then looking for the name 'ONE' somewhere else in the metadata and can't see it.
Really just playing by ear as I don't script generally, so apologies for the nonsensical message 😄
Copy link to clipboard
Copied
Hi,
Can you upload the document?
Malcolm
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi,
Using the document you provided and the script above I was able to creat this.
Attached are screen shots that show the metadata written in InDesign ( FromInDesign.pdf)
And the resultant PDF file (inAcrobat.pdf)
I have also attached the created PDF ( One_01.pdf)
Is this not where you are hoping for the data to be written?
Malcolm
Copy link to clipboard
Copied
Hi Malcolm, I think the problem is you only want to set the original file name node and property once, so it’s also possible to use the getProperty method to check if the original name has already been set in the Advanced XMP. So maybe something like this:
//startup script
#targetengine "session"
var el = app.eventListeners.add("beforeExport", metaExport);
/**
* Function to run before an Export
* the event
*/
function metaExport(evt){
var md = app.activeDocument.metadataPreferences;
var ns = "http://ns.adobe.com/xap/1.0/"
//check if an OriginalFileName node exists, if it doesn’t use the current file name
var ofn = md.getProperty(ns, "OriginalFileName")
if (ofn == "") {
md.setProperty(ns, "OriginalFileName", evt.parent.name);
}
//set a current file name node
md.setProperty(ns, "CurrentFileName", evt.parent.name)
}
Here the original file name was YELLOW.indd. I changed the name in the Finder, opened the file and Exported it again:
Copy link to clipboard
Copied
the INDD filename to the PDF 'Subject' (or keywords, copyright... anything really), not title
The script is executing before every save event, I wonder if you want it to be before an export? The metadata in InDesign’s description field shows in the PDF’s Subject field. This checks if the Description field is empty, and if it is inserts some text with the document’s name:
//startup script
#targetengine "session"
var el = app.eventListeners.add("beforeExport", onExport );
/**
* Function to run before an Export
* the event
*/
function onExport(evt){
var md = app.activeDocument.metadataPreferences.description
if (md == "") {
md = "Original InDesign Document Name: " + evt.parent.name
}
}
Copy link to clipboard
Copied
Hey Rob,
That looks like it'd be perfect but when I paste that into a text file and save with a jsx extension, then put it in my startup scripts folder, it doesn't see to write anything to the subject field as it has in your screenshot. The steps I take are:
Open indesign
New file
Save as 'blue.indd'
Export PDF as blue.pdf
tap on that PDF and rename 'red'
The properties of that pdf now show the new file, old title (which we'll have to rename) and nothing else. Any ideas what I'm doing wrong?
Copy link to clipboard
Copied
The text editor has to save the code as plain text. Also, make sure you quit InDesign before putting the script in your startup folder, maybe even restart your computer.
Plain text editors can be tricky—here are compiled scripts.
https://shared-assets.adobe.com/link/27806554-f525-44e5-7a20-fcd41d09ff81
I also added an alternate that uses the Keyword array. The original name is the first keyword with a "oname:" prefix:
Copy link to clipboard
Copied
Amazing! Thanks so much, it's really saved a lot of headaches and will continue to do so! 😄
Thanks again!
Copy link to clipboard
Copied
I tried the script, but the original name remains, if I rename the INDD file, I will duplicate it. So the script doesn't run every time I export to PDF? This creates chaos in the descriptions.
Copy link to clipboard
Copied
Sorry, not sure what you mean?
The script runs when I export a PDF. Whatever the INDD name is at the point of that export, is written into the keywords field of the PDF properties.
Renaming the INDD thereafter makes no difference and the PDFs keep the name on export of the original INDD name. Is that what you mean? You want the script to update the name in subject along with whatever the INDDs new name is?
Copy link to clipboard
Copied
Yes, the problem is to keep the original name, which will be stored in INDD. In JSX, I used "try" instead of "if". The file name is overwritten with each export.
Copy link to clipboard
Copied
The original file name only needs to be written once, so you have to keep the if statement, which checks if the Descrption field is empty. If you use the Description field for other data it’s not going to work. The BeforeExportXMP.jsx make a custom metadata field named OriginalFileName:
Copy link to clipboard
Copied
Here’s the code for the XMP version:
//startup script
#targetengine "session"
var el = app.eventListeners.add("beforeExport", metaExport);
/**
* Function to run before an Export
* @Param the event
*/
function metaExport(evt){
var md = app.activeDocument.metadataPreferences;
var ns = "http://ns.adobe.com/xap/1.0/"
//check if an OriginalFileName node exists, if it doesn’t use the current file name
var ofn = md.getProperty(ns, "OriginalFileName")
if (ofn == "") {
md.setProperty(ns, "OriginalFileName", evt.parent.name);
}
//set a current file name node
md.setProperty(ns, "CurrentFileName", evt.parent.name)
}
Copy link to clipboard
Copied
The script’s function is triggered by a before export listener, so it’s intended use is as a startup script. It should be copied into the InDesign application folder’s startup scripts folder and the listener will run every time ID starts up. I posted 3 versions so you can choose where the original name gets copied—the Description, or Keyword fields, or the advanced XMP.
Copy link to clipboard
Copied
Also, if you use the Description field for other purposes, run either the BeforeExportXMP.jsx or BeforeExportKW.jsx version.