Adobe 65009333 - InCopy CS4 - PC Manual page 84

Scripting guide: javascript
Hide thumbs Also See for 65009333 - InCopy CS4 - PC:
Table of Contents

Advertisement

XML
Adding XML elements to a story 84
var myXMLElement = myDocument.xmlElements.item(0).xmlElements.item(0);
//By inserting the return character after the XML element, the character
//becomes part of the content of the parent XML element, not of the element itself.
myXMLElement.insertTextAsContent("\r", XMLElementPosition.afterElement);
myXMLElement = myDocument.xmlElements.item(0).xmlElements.item(1);
myXMLElement.insertTextAsContent("Static text: ", XMLElementPosition.beforeElement);
myXMLElement.insertTextAsContent("\r", XMLElementPosition.afterElement);
//To add text inside the element, set the location option to beginning or end.
myXMLElement = myDocument.xmlElements.item(0).xmlElements.item(2);
myXMLElement.insertTextAsContent("Text at the start of the element: ",
XMLElementPosition.elementStart);
myXMLElement.insertTextAsContent(" Text at the end of the element.",
XMLElementPosition.elementEnd);
myXMLElement.insertTextAsContent("\r", XMLElementPosition.afterElement);
//Add static text outside the element.
myXMLElement = myDocument.xmlElements.item(0).xmlElements.item(3);
myXMLElement.insertTextAsContent("Text before the element: ",
XMLElementPosition.beforeElement);
myXMLElement.insertTextAsContent(" Text after the element.",
XMLElementPosition.afterElement);
//To insert text inside the text of an element, work with the text
//objects contained by the element.
myXMLElement.words.item(2).insertionPoints.item(0).contents = "(the third word of) ";
Mapping tags to styles
One of the quickest ways to apply formatting to XML text elements is to use
, also known
xmlImportMaps
as tag-to-style-mappings. When you do this, you can associate a specific XML tag with a paragraph or
character style. When you use the
method of the document, InCopy applies the style to
mapTagsToStyles
the text, as shown in the following script fragment (from the MapTagsToStyles tutorial script):
var myDocument = app.documents.item(0);
//Create a tag to style mapping.
myDocument.xmlImportMaps.add(myDocument.xmlTags.item("heading_1"),
myDocument.paragraphStyles.item("heading 1"));
myDocument.xmlImportMaps.add(myDocument.xmlTags.item("heading_2"),
myDocument.paragraphStyles.item("heading 2"));
myDocument.xmlImportMaps.add(myDocument.xmlTags.item("para_1"),
myDocument.paragraphStyles.item("para 1"));
myDocument.xmlImportMaps.add(myDocument.xmlTags.item("body_text"),
myDocument.paragraphStyles.item("body text"));
//Apply the tag to style mapping.
myDocument.mapXMLTagsToStyles();
//Place the XML element in the layout to see the result.
myDocument.stories.item(0).placeXML(myDocument.xmlElements.item(0));
Mapping styles to tags
When you have formatted text that is not associated with any XML elements, and you want to move that
text into an XML structure, use style-to-tag mapping, which associates paragraph and character styles with
XML tags. To do this, use
objects to create the links between XML tags and styles, then use
xmlExportMaps
the
method to create the corresponding XML elements, as shown in the following
mapStylesToTags
script fragment (from the MapStylesToTags tutorial script):

Advertisement

Table of Contents
loading

This manual is also suitable for:

Incopy cs4

Table of Contents