• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

script to write to PDF metadata upon export

Explorer ,
Feb 11, 2021 Feb 11, 2021

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";

TOPICS
Import and export , Scripting

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 11, 2021 Feb 11, 2021

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:

 

Screen Shot 1.png

 

Votes

Translate

Translate
Community Expert ,
Feb 11, 2021 Feb 11, 2021

Copy link to clipboard

Copied

Hi,

 

The easiest method would just be to change destNodeName to whatever value you would like.

 

for example - "ANewNodeName"

BarlaeDC_0-1613037436531.png

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2021 Feb 11, 2021

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 😄

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2021 Feb 11, 2021

Copy link to clipboard

Copied

Hi,

 

Can you upload the document?

 

Malcolm

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2021 Feb 11, 2021

Copy link to clipboard

Copied

Here - so the original filename (ONE in this example) needs to appear in the properties or metadata for the renamed PDF. 😕

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2021 Feb 11, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2021 Feb 11, 2021

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:

 

 

Screen Shot 2.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2021 Feb 11, 2021

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
    } 
}

 

 

 

 

Screen Shot 2.png

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2021 Feb 11, 2021

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?

Screenshot 2021-02-11 at 15.45.10.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2021 Feb 11, 2021

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:

 

Screen Shot 1.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2021 Feb 12, 2021

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 17, 2021 Feb 17, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 18, 2021 Feb 18, 2021

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 18, 2021 Feb 18, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

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:

Screen Shot 25.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

LATEST

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)
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Also, if you use the Description field for other purposes, run either the BeforeExportXMP.jsx or BeforeExportKW.jsx version.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines