Article View: adobe.scripting.indesign
Article #16002So close to exporting character styles into XML but not there yet!
From: abe_hays
Date: Fri, 17 Oct 2003 15:34
Date: Fri, 17 Oct 2003 15:34
52 lines
3552 bytes
3552 bytes
Hi, all. I've been lurking about on this forum, gleaning many very usefull bits and pieces to help me on my project, but now I'm stumped, and so I'm posting my question.
I've been fighting the good fight to map character styles to XML tags, and I have a script that does it right, most of the time. (Actually, I'm using it right now for mapping font styles, but originally it was for character styles and it works for either one, anyhow.) The script does a search for text that has the desired character format, or font style. Then it adds plain-text tags to either end of the found text chunk. After I do an XML export I run the XML file through a script that takes the clunky "###beginbold" tag and turns it into a nice regular "b" tag. I know it's a prety heavy-handed way to work with XML, but it gets the job done, as we simply need to get a bunch of XML files out of this process and never mind the mess we make of the InDesign file.
Here's the code:
tell application "InDesign 2.0.1"
set find preferences to nothing
set change preferences to nothing
set theDocument to active document
tell theDocument
set mybtag to "###beginbold"
set mybtag2 to "###endbold"
set mytext2 to search with find attributes {font style:"Bold"}
repeat with n from (count of mytext2) to 1 by -1
set contents of item n of mytext2 to mybtag & contents of item n of mytext2 & mybtag2
end repeat
set mytext2 to nothing
end tell
end tell
Although this works very well in almost all cases, there are some spots where it fails. Let's say we're looking for bold text, the InDesign document is already structured into XML, and piece of bold text comes at the beginning of a chunk of XML tagged text. The script will take the bold text and wrap it in my plain-text tags, as was the plan, but then it will stick it all inside the preceeding tag, instead of inside the tag where it came from.
e.g. If "Richard Thomas" is in bold in the following example then:
<divider/>
<_text>Richard Thomas has developed a revolutionary new way of eating grapes.</_text>
becomes:
<divider>###beginboldRichard Thomas###endbold</divider>
<_text>Richard Thomas has developed a revolutionary new way of eating grapes.</_text>
(Note that the original instance of the bold string remains where it originally was, so now there are two instances of the text "Richard Thomas".)
I've also tried rewriting the script it with the central bit of the script like this, but there is no difference:
set mytext2 to search with find attributes {font style:"Bold"}
repeat with n from (count of mytext2) to 1 by -1
set oldtext to text of contents of item n of mytext2
set newtext to mybtag & oldtext & mybtag2
set contents of item n of mytext2 to newtext
end repeat
Does anybody have any guesses as to what it's doing?
Thanks a bunch,
Abe
Technical Details
Message-ID:
<2ccde442.-1@webx.la2eafNXanI>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!skynet.be!skynet.be!newscore.univie.ac.at!newsfeed01.sul.t-online.de!t-online.de!webxShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f939a,37d2af684fe1b8b7,start X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2003-10-17 08:37:55 PST From: abe_hays@adobeforums.com Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!skynet.be!skynet.be!newscore.univie.ac.at!newsfeed01.sul.t-online.de!t-online.de!webx Newsgroups: adobe.scripting.indesign Subject: So close to exporting character styles into XML but not there yet! Message-ID: <2ccde442.-1@webx.la2eafNXanI> Date: Fri, 17 Oct 2003 08:34:29 -0700 Lines: 77 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit