InDesign Paragraph Styles to XML tags for Groups

Hi,
Some folks have pointed out that the script for turning InDesign Paragraph Styles into XML tags won’t work with style groups. This version of the of the script should work with grouped and ungrouped styles.


(*InDesign Paragraph Styles to XML Tags

This script will create XML tags based on paragraph style names in InDesign CS3. First we change any character that is illegal according to xml tag naming specs to an underscore. XML tags can start with an underscore, so any illegal character that is first in a paragraph style name will be OK. We also change all paragraph style names in the document to legal XML tag names so that mapping styles to tags or tags to styles when importing XML will be direct.

Copyright 2009, 2010, 2011 Gregory J. Ledger

Feel free to use, modify, and redistribute this code. However, please always post a link to my website, "www.macproductionartist.wordpress.com" when reusing this code.*)

global mylist
global myNewListItems
global legalChar
(*XML legal characters could also include ideograms, but that would make the following list enormous. If so inclined, please adjust the legalChar variable to include ideograms if you desire.*)
set legalChar to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_"}
set mynewlist to {}
global illegalNumbers
set illegalNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set fileType to {"IDd5"}
repeat
set myFile to choose file
set myFileInfo to info for myFile
if file type of myFileInfo is not in fileType then
display alert "Please choose an InDesign CS3 document!"
else
exit repeat
end if
end repeat
tell application "Adobe InDesign CS3"
activate
open myFile
tell active document
set PStyles to every item of all paragraph styles
set PStyleNames to name of every item of all paragraph styles
repeat 2 times
set PStyles to rest of PStyles --don't need first two default paragraph styles
set PStyleNames to rest of PStyleNames --don't need first two default paragraph stylenames
end repeat
end tell
my doReplace(PStyleNames)
--change the paragraph stylename
tell active document
try
repeat with i from 1 to count of PStyles
set pStyle2change to item i of PStyles
set name of pStyle2change to item i of myNewListItems
end repeat
on error
display dialog "You have one or more paragraph styles with nearly the same name. Please rename your paragraph styles distinctively and try again. This script will now close!" buttons {"Cancel"} default button 1 with icon 0
end try
--create the XML tags
try
repeat with j from 1 to count myNewListItems
try
set myXMLTag to make XML tag with properties {name:item j of myNewListItems}
on error
display alert "A tag with that name already exists." & return & (name of item j)
end try
end repeat
end try
end tell
end tell
--change paragraph style name characters to legal XML characters
on doReplace(mylist) --replace any XML specification's illegal character with an underscore
tell application "Adobe InDesign CS3"
set myNewListItems to {}
repeat with anItem in mylist
set legalNames to {}
set anItemChars to characters of anItem
repeat with k from 1 to (count of anItemChars)
set aChar to item k of anItemChars as text
if aChar is not in legalChar then
set aChar to "_" --this may put an underscore at the start of a paragraph style name, but this is legal in both naming of styles AND naming of xml tags If we just got rid of illegal characters a style named Body Serif would conflict with a style named BodySerif once the illegal space character was remove
copy aChar to end of legalNames
else
copy aChar to end of legalNames
end if
end repeat

if first item of legalNames is in illegalNumbers then
set beginning of legalNames to "_"
end if
set legalName to legalNames as string

set end of myNewListItems to legalName

end repeat
end tell
end doReplace

You can also download this from the box.net folder below.

Gregory Ledger

Advertisements

About this entry