Script Description
This script will search for text based on paragraph styles. It will then fill in the appropriate XMP fields with the found text. To use this script you’ll need to have paragraph styles named: title, author, & descriptions. An example InDesign document containing these paragraph styles is included in the capturexmp.zip download.
Getting Started
To get started this script sets the scripting object model to that used by CS2. I wrote this using CS2 so to get this work in CS3 this line is needed. Next, I create a function that searches through found text and replaces characters using parallel arrays. In this example I just replace the paragraph markers and new line markers with nothing. Just so those characters do not appear in XMP panel.
//Set to 4.0/CS2 scripting object model app.scriptPreferences.version = 4.0;
myDocument = app.activeDocument; //work on active document
//===== Remove/Replace certain characters such as paragraph markers =====\\
function replaceByCode(myMetaIn, myTotalCharacters)
{
myCharCodeList = new Array(
"13", // Paragraph Marker
"10", // Soft Return
);
myCharReplacementList = new Array(
"", // Remove paragraph markers
"", // Remove soft returns
);
var myTotalSpecialCharacters = myCharCodeList.length;
var myMetaOut = myMetaIn;
for (z = myTotalSpecialCharacters; z >= 0; z--)
{
for (y = myTotalCharacters; y >= 0; y--)
{
myCharacterCode = myMetaIn.charCodeAt(y) + "";
if (myCharacterCode == myCharCodeList[z])
{
myCharacter = myMetaIn.charAt(y);
myMetaOut = myMetaOut.replace(myCharacter, myCharReplacementList[z]);
}
}
}
return (myMetaOut)
}
Searching for text
Here I search for the text. I find the text based on what paragraph styles they use. In this example I search for text style with "title", "description", and "keywords" paragraph styles. The keywords should be in one paragraph separated by a comma. For each of the strings that I find, I use the replaceByCode function to remove extra paragraph markers.
/===== Search for text with the paragraph style "title" =====\\
var myTitle = " ";
try{
app.findPreferences = null;
app.changePreferences = null;
var titleSearch = myDocument.search(undefined,false,false,undefined, {appliedParagraphStyle:’title’});
for (i = 0; i < titleSearch.length; i++)
{
myTitleParagraph = titleSearch[i];
}
myTitle = myTitleParagraph.contents;
myTitleTotalCharacters = myTitle.length;
myTitle = replaceByCode(myTitle, myTitleTotalCharacters);
}
catch(e){}
//===== Search for text with the paragraph style "description" =====\\
var myDescription = " ";
try{
app.findPreferences = null;
app.changePreferences = null;
var descriptionSearch = myDocument.search(undefined,false,false,undefined, {appliedParagraphStyle:’description’});
for (i = 0; i < descriptionSearch.length; i++)
{
myDescriptionParagraph = descriptionSearch[i];
}
myDescription = myDescriptionParagraph.contents;
}
catch(e){}
//===== Search for text with the paragraph style "keywords" =====\\
var myKeywords = " ";
try{
app.findPreferences = null;
app.changePreferences = null;
var keywordsSearch = myDocument.search(undefined,false,false,undefined, {appliedParagraphStyle:’keywords’});
for (i = 0; i < keywordsSearch.length; i++)
{
myKeywordsParagraph = keywordsSearch[i];
}
myKeywords = myKeywordsParagraph.contents;
myKeywords = myKeywords.split(","); // Create an array from the string. Keywords should be seperated by a comma.
}
catch(e){}
Assigning the found text to XMP fields
The last step is to apply the text I found earlier to fields within the XMP panel. Since I’m using generic fields I can assign them using the default metadataPreferences object. To see the results of this script, run it on the provided example InDesign document, then click on "File" and "File Info" to see the filled in fields.
//===== Assign the text found from searching to XMP fields =====\\
myDocument.metadataPreferences.documentTitle = myTitle;
myDocument.metadataPreferences.description = myDescription;
myDocument.metadataPreferences.keywords = myKeywords;

