Adobe 65009333 Scripting Guide - Page 85

Applying styles to XML elements

Page 85 highlights

XML Adding XML elements to a story 85 var myDocument = app.documents.item(0); //Create a style to tag mapping. myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("heading 1"), myDocument.xmlTags.item("heading_1")); myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("heading 2"), myDocument.xmlTags.item("heading_2")); myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("para 1"), myDocument.xmlTags.item("para_1")); myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("body text"), myDocument.xmlTags.item("body_text")); //Apply the style to tag mapping. myDocument.mapStylesToXMLTags(); Another approach is simply to have your script create a new XML tag for each paragraph or character style in the document, and then apply the style to tag mapping, as shown in the following script fragment (from the MapAllStylesToTags tutorial script): var myDocument = app.documents.item(0); //Create tags that match the style names in the document, //creating an XMLExportMap for each tag/style pair. for(var myCounter = 0; myCounter

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

XML
Adding XML elements to a story
85
var myDocument = app.documents.item(0);
//Create a style to tag mapping.
myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("heading 1"),
myDocument.xmlTags.item("heading_1"));
myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("heading 2"),
myDocument.xmlTags.item("heading_2"));
myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("para 1"),
myDocument.xmlTags.item("para_1"));
myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item("body text"),
myDocument.xmlTags.item("body_text"));
//Apply the style to tag mapping.
myDocument.mapStylesToXMLTags();
Another approach is simply to have your script create a new XML tag for each paragraph or character style
in the document, and then apply the style to tag mapping, as shown in the following script fragment (from
the MapAllStylesToTags tutorial script):
var myDocument = app.documents.item(0);
//Create tags that match the style names in the document,
//creating an XMLExportMap for each tag/style pair.
for(var myCounter = 0; myCounter<myDocument.paragraphStyles.length; myCounter++){
var myParagraphStyle = myDocument.paragraphStyles.item(myCounter);
var myParagraphStyleName = myParagraphStyle.name;
var myXMLTagName = myParagraphStyleName.replace(/\ /gi, "_")
myXMLTagName = myXMLTagName.replace(/\[/gi, "")
myXMLTagName = myXMLTagName.replace(/\]/gi, "")
var myXMLTag = myDocument.xmlTags.add(myXMLTagName);
myDocument.xmlExportMaps.add(myParagraphStyle, myXMLTag);
}
//Apply the tag to style mapping.
myDocument.mapStylesToXMLTags();
Applying styles to XML elements
In addition to using tag-to-style and style-to-tag mappings or applying styles to the text and page items
associated with XML elements, you also can apply styles to XML elements directly. The following script
fragment shows how to use three methods:
applyParagraphStyle
,
applyCharacterStyle
, and. (For the
complete script, see ApplyStylesToXMLElements.)
var myDocument = app.documents.add();
myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
//Create a series of XML tags.
var myHeading1XMLTag = myDocument.xmlTags.add("heading_1");
var myHeading2XMLTag = myDocument.xmlTags.add("heading_2");
var myPara1XMLTag = myDocument.xmlTags.add("para_1");
var myBodyTextXMLTag = myDocument.xmlTags.add("body_text");
//Create a series of paragraph styles.
var myHeading1Style = myDocument.paragraphStyles.add();
myHeading1Style.name = "heading 1";
myHeading1Style.pointSize = 24;
var myHeading2Style = myDocument.paragraphStyles.add();
myHeading2Style.name = "heading 2";
myHeading2Style.pointSize = 14;
myHeading2Style.spaceBefore = 12;
var myPara1Style = myDocument.paragraphStyles.add();
myPara1Style.name = "para 1";
myPara1Style.pointSize = 12;