Skip to main content
Known Participant
October 28, 2013
Answered

Scripts to insert metadata automatically to document with paragraph style

  • October 28, 2013
  • 1 reply
  • 4330 views

Hello,

I am trying to find a way to automatically insert  metadata into my document through paragraph style in indesign cs5.5.

This will be myDocument title, the Author name and myDocument description.

Is there anyone who can help me with this project?

Thanks.

This topic has been closed for replies.
Correct answer Sajeev Sridharan

Place the script in the following location: C:\Program Files\Adobe\Adobe InDesign CS6\Scripts\startup scripts

If you close the document the script will automatically put the metadata in your indesign file (use ctrl+w or menu -> file -> close option to close the file)

Code:

#targetengine session

//if you want to test the script for your active document, comment the below line else copy the script in your startup folder

app.menuActions.itemByName("Close").eventListeners.add("beforeInvoke", function()

{

var myDoc = app.activeDocument;

var docTitleText="";

var docDescripText="";

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = '.+';

found = app.activeDocument.findGrep (true);

for (j = found.length-1; j >=0 ; j--)

{

    var foundText = found.contents;

   

    //You can define multiple Paragraph style for Document Title

   

    if(found.texts[0].appliedParagraphStyle.name=="docTitle1")

    {

        docTitleText=found.contents;

    }

    else if(found.texts[0].appliedParagraphStyle.name=="docTitle2")

    {

        docTitleText=docTitleText + " " + found.contents;

    }

    else if(found.texts[0].appliedParagraphStyle.name=="docTitle3")

    {

        docTitleText=docTitleText + " " + found.contents;

    }

    //You can define multiple Paragraph style for Document Description

    else if(found.texts[0].appliedParagraphStyle.name=="docDescription1")

    {

        docDescripText=found.contents;

    }

    else if(found.texts[0].appliedParagraphStyle.name=="docDescription2")

    {

        docDescripText=docDescripText + " " + found.contents;

    }

    else if(found.texts[0].appliedParagraphStyle.name=="docDescription3")

    {

        docDescripText=docDescripText + " " + found.contents;

    }

}

with (myDoc.metadataPreferences){

    author = "My Name";

    documentTitle = docTitleText;

    description = docDescripText;

    }

});

Vandy

1 reply

csm_phil
Legend
October 28, 2013

Hi abmiss1,

I have a doubt with your above request, what i understood!

If the the paragraph style is "myDocument title" means the contents insert to : XMP Document title.

If the the paragraph style is "Author Name" means the contents insert to : XMP Author name.

If the the paragraph style is "myDocument description" means the contents insert to : XMP Description.

Please find the below JS cod its simply created the XMP meta information.

var myDocument = app.activeDocument;

with (myDocument.metadataPreferences){

    author = "Adobe";

    copyrightInfoURL = "http://www.adobe.com";

    copyrightNotice = "This document is copyrighted.";

    copyrightStatus = CopyrightStatus.yes;

    description = "Example of xmp metadata scripting in InDesign CS";

    documentTitle = "XMP Example";

    jobName = "XMP_Example_2003";

    keywords = ["animal", "mineral", "vegetable"];

    }

Otherwise please clearly expalin what is your requirement.

thx,

csm_phil

Legend
October 28, 2013

Try this,

var myDoc = app.activeDocument;

var authorText, docDescripText;

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = '.+';

found = app.activeDocument.findGrep (true);

for (j = 0; j < found.length; j++){

     var foundText = found.contents;

     if(found.texts[0].appliedParagraphStyle.name=="authorName")//author paragraph style

     {

         authorText=found.contents;

     }

     else if(found.texts[0].appliedParagraphStyle.name=="docDescription")//description paragraph style

     {

         docDescripText=found.contents;

     }

}

with (myDoc.metadataPreferences){

    author = authorText;

    documentTitle = myDoc.name.split(".indd")[0];

    description = docDescripText;

    }

Vandy

abmiss1Author
Known Participant
October 28, 2013

Thank you very much Vandy. The script work good. The only thing I want to ask you again is ( maybe I wasn't explicit on my original post ): The author name will always be the same but the Document Title and the Document Description will varry, this is why I wanted to do it with paragraph style. Also I was wondering if I can apply like 2 or 3 styles to the Document descrition at the same time. So in summary (author name="My Name", Document Title = "Par Style1" "Par Style2"  And my Document Description will be also define with multiple paragraph styles.

I also need help to make the srypt run automatically as I open any document or close it.

Thank so much for your help.