Copy link to clipboard
Copied
Hi all,
We're in the process of switching to an XML workflow, and as one of our interim steps we're applying tags to paragraph and character styles in some of our documents. I'm aware of the option to map styles to tags and this should work well for paragraph styles. However, we're looking for a way to only map the character styles that have been manually applied.
As an example, we have a paragraph style for instructions that has a bold number nested at the beginning of each paragraph. In other places, the same character style may be used to bold a specific word, but it is applied manually. Is it possible to map to tags ONLY the character styles that were applied manually? We do not want tags identifying character styles that were applied via Nested Styles or GREP Styles.
I am assuming this would take some sort of script, which is why I'm posting in the scripting forum. Does anyone know if this is possible?
Thanks in advance,
Matthew
Hi Matt,
Please try below code, hope it will help you.
...applyTag("bold", "b");
applyTag("italic", "i");
function applyTag(StyleName, TagName)
{
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = StyleName;
list = app.activeDocument.findText();
app.findTextPreferences = null;
while (list.length)
{
next = list.pop();
if (next.appliedCharacterStyle == app.activeDocument.characterStyles.item(StyleName))
{
next.associatedXMLElements[0].xmlElements.add(TagName, ne
Copy link to clipboard
Copied
Hey!
Maybe for start try this little script. It will mark all localy applied formatting.
http://www.indiscripts.com/post/2010/05/show-local-formatting-in-indesign-cs4
--
tomaxxi
http://indisnip.wordpress.com/
Copy link to clipboard
Copied
Thanks, tomaxxi. That's a cool little script. Unfortunately for this project, I don't need to know where there are manual overrides, I need to know where character styles have been manually applied. I'm not even sure if this is possible, which is why I asked. I'm sure your script will come in useful in the future though!
Matt
Copy link to clipboard
Copied
Another InDesign oddity!
If you fill a frame full of placeholder text and assign a "Bold" character style to, for example, a Nested Style:1 word, and a GREP style \<\w{4,6}\>, then use regular (or GREP) Find Text to search for the character style, it'll only highlight words that are at the start of the paragraph. Now that is wrong, wrong, wrong, because
(a) it should not find any of them -- if you click your cursor into one of these bolded words, the Character Style panel says it's [None].
or
(b) it should find all of them, not just those that happen to be at the start of a paragraph. (BTW -- it's not the Nested Style it picks up; when using only GREP styles it'll also find words-at-the-start).
It is funny, if things like this tickle you, that in a script "findText" works exactly the same. However: if you 'found' bold text and then interrogate what character style it has applied, it will only return your "bold" style if it was applied manually.
See this script -- test with a placeholder text, aforementioned Nested/GREP styles, and a few hand-picked words that you applied "bold" to yourself. It will only report the ones you marked.
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = "bold";
list = app.activeDocument.findText();
r = [];
while (list.length)
{
next = list.pop();
if (next.appliedCharacterStyle == app.activeDocument.characterStyles.item("bold"))
r.push (next.contents);
}
alert (r.join("\r"));
Copy link to clipboard
Copied
Thanks Jongware—do you think that's a bug? I don't see how that would be a feature...
The script you provided worked...sort of. I did a test as you suggested with bold applied manually (to the words "paragraph" and "and"), via nested style (through one word), and via GREP style (all four letter words). Here's the result I got:
So it looks like the script returns a list of the words that are affected, yes? This is helpful, but in a long document, how would I know where the affected words were?
What would be really useful is if, instead of returning a list, the script could apply an XML tag to the text that is manually styled. That's what we ultimately want to accomplish. Would that be complicated? Also, would doing this for multiple character styles be as simple as repeating the text you provided and replacing instances of "bold" with a different style name ("italic," for example)? I was hoping we could achieve this using the Find/Change dialog, but with the limitations outlined in my original post it doesn't seem possible.
I clearly need to learn some scripting...it seems more and more important needed with every project! I'll have to read some books on the subject. In the meantime, thanks so much for all your help!
Matt
Copy link to clipboard
Copied
Hi Matt,
Please try below code, hope it will help you.
applyTag("bold", "b");
applyTag("italic", "i");
function applyTag(StyleName, TagName)
{
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = StyleName;
list = app.activeDocument.findText();
app.findTextPreferences = null;
while (list.length)
{
next = list.pop();
if (next.appliedCharacterStyle == app.activeDocument.characterStyles.item(StyleName))
{
next.associatedXMLElements[0].xmlElements.add(TagName, next)
}
}
}
Shonky
Copy link to clipboard
Copied
Shonkyin,
Thanks for your response. I saved the text you provided, but can't seem to make it work. As is, the script didn't do anything...no tags applied, but no warnings either. I replaced instances of StyleName with "bold" and instances of TagName with "bold" but then I started getting error messages. Am I doing something wrong?
Copy link to clipboard
Copied
Shonky's script works for me (nicely done with that XML tagging!). Of course I changed in the top line
applyTag("italic", "i");
the first "italic" to the name for the Italic character style in my document, and the second "i" to the name for the Mark-as-Italic tag.
(Post-thought:) ahhh, perhaps a little more clarification might be prudent.
Shonky wrote a function that can be called with two arguments. His top two lines show how it works. What you should do is repeat the applyTag(xx,yy) line for each of the character styles you want to mark, where
xx = The Character Style name you want to search
yy = The XML Tag name you want to mark it with.
(Another post-though ) The rest of his script should stay unchanged ...
Copy link to clipboard
Copied
Matt,
It's working here on CS4 but keep in mind that your text frame must applied a tag.
Shonky
Copy link to clipboard
Copied
Thanks, Jongware. I tried doing what you suggested, but I'm still getting an error message, even when the frame is tagged. It seems to be the last line of the script that is causing the error. Still no tags applied to the styles. Suggestions?
Copy link to clipboard
Copied
.. even when the frame is tagged ..
I didn't use a tagged frame, but rather a tagged story (the entire text) -- but that was just in the file I happened to test with.
If I run the script on a completely untagged text, I get the same error. BUT when I (1) select all text and apply a root tag to it, or (2) select the frame and apply a root tag to it, it works.
It even works if you select only one frame of a threaded series and apply the tag to that (so it seems you don't really tag the frame itself, but rather the text that's in it -- makes sense, actually).
If you call up the Tags panel and click anywhere inside your text, the 'current' tag should be selected. If you don't see any tag name selected, the text is totally untagged ...
Copy link to clipboard
Copied
That is so strange...it kept giving me the error message, so I reverted to the most recent saved version of the document, ran the script again, and it worked! I'm not sure what made the difference, but it looks like this is doing exactly what I need—finding hand applied character styles and ignoring characters styles applied via nested or GREP styles.
Thanks so much to both of you!
Copy link to clipboard
Copied
If you have one story then below code working for me
var myDocument = app.activeDocument;
var myRootXMLElement = myDocument.xmlElements.item(0);
try{
var myXMLTag = myDocument.xmlTags.add("Text_Frame");
}catch(e){myXMLTag=myDocument.xmlTags.item("Text_Frame")}
var myXMLElementA = myRootXMLElement.xmlElements.add(myXMLTag);
applyTag("bold", "b");
applyTag("italic", "i");
function applyTag(StyleName, TagName)
{
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = StyleName;
list = app.activeDocument.findText();
app.findTextPreferences = null;
while (list.length)
{
next = list.pop();
if(next.parentStory.parent.constructor.name == "Document")
{
myDocument.xmlElements.item(0).xmlElements[-1].markup(myDocument.pages.item(0).textFrames.item(0));
}
if (next.appliedCharacterStyle == app.activeDocument.characterStyles.item(StyleName))
{
next.associatedXMLElements[0].xmlElements.add(TagName, next)
}
}
}
Shonky
Copy link to clipboard
Copied
There is more than one story in the document, but not all of their frames are tagged. I thought this was what was causing the problem, so I deleted all the untagged frames to test it and it DID work. After that, though, I reverted to the most recent saved version, which included the untagged frames and suddenly the script worked! I don't know what the difference was.
Copy link to clipboard
Copied
That's good to know that script is working for you
Shonky