ID Paragraph Styles and XML tags

One of the easiest ways to use XML in InDesign CS3 is to name the XML tags exactly the same as the paragraph styles. That way, when you import the XML doc into your tagged ID doc, every paragraph style is automatically applied. If you are using nested styles, you character styles can be automatically applied as well.

Now ID CS3 has a way of mapping paragraph styles to XML tags, AND a way to map XML tags to paragraph styles. You’d think it was a no-brainer to have the software automatically create XML tags from paragraph styles. But no. You have to create the tags one at a time in the Tags Panel.

Therefore, I wrote this Applescript to solve that very problem. (I’m kind of proud of it, as it is also my first attempt at using try statements and error catchers.)

The first part of the code renames paragraph styles by replacing any XML illegal characters with an underscore. For this script, I consider bullets, colons, and dashes to be illegal characters as well, only because other software programs use reserve those characters for their own programming. I also did not include all the foreign language characters, like Chinese ideograms, as it would have made the list of legal characters extremely long (and perhaps unstable.)

If you have named your styles too similarly, the script will result in an error. (The original ID doc I used to test this had a paragraph style named “Body Style”, one named “Body•Style”, and one named “Body-Style.” I think three different designers had worked on it. Of course, this throws an error immediately. Also, if you have an illegal character at the start of the style name, such as “•Body Copy”, the illegal character will be changed to an underscore. That is legal in XML, but it might just look unsightly in your Styles Panel. So, be careful how you name your styles.

I cannot offer this script as a download as WordPress does not permit (for good reason) this file type in the media library, so you will have to copy and paste it into Script Editor, or another IDE that supports Applescript, and compile it.

Finally, if you want to try to break the script, be my guest. Please tell me what happened in the comment section so I can try to fix it. It worked on my Leopard iMac. You can also reuse, amend, and distribute it, as long as you give me my props!

Here’s the script:

(*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, 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 {}
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 paragraph style
set PStyleNames to name of every paragraph style
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."
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
set legalName to legalNames as string
set legalNameList to legalName
set end of myNewListItems to legalNameList
end repeat
end tell
end doReplace

See ya soon!

Advertisements

About this entry