Hi All,
Need to insert tagging (character style name) surrounding the text which has character styles applied in InDesign document.
Example: This is <italic>an</italic> example of <bold>character style</bold> tagging text.
Please provide JS code for this.
Thanks in advance,
Mon
I think a simple grep find/change will work, look at the image below.
The .+ finds any character, one or more times,
In the Change To field type the code you want to use eg <bold>$0</bold>, the $0 puts back what is found.
Finally, in the Find Format area, select the formating that you are looking for.
Dear All,
I have tried myself. But there is a problem with my code. It selects characterStyle [NONE]. And add <> symbol before and after of every paragraph style. Actually, I need to insert <> symbol before and after character style applied text (see my First Query).
How can we avoid NONE characterstyle in my below code. Please help.
var myAppliedCStyle = [app.findGrepPreferences.appliedCharacterStyle]
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.changeGrepPreferences.changeTo = "<$0>";
for (i=0; i<myAppliedCStyle.length; i++)
{
app.findGrepPreferences.findWhat = ".+";
app.changeGrepPreferences.appliedCharacterStyle = myAppliedCStyle[i];
app.activeDocument.changeGrep();
}
Thanks,
Mon
Hi,
Try this. I'm also fresher...
First u have to create tags manually with names b, bi,i, sc (bold,bolditalic,italic, small caps)...
then execute this js...
docObj=app.activeDocument;
xmlObj = docObj.xmlTags;
pageObj=docObj.pages.item(0);
myTextFrame=pageObj.textFrames.item(0);
myText=myTextFrame.parentStory.paragraphs.item(0);
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.fontStyle = "Bold";
app.changeTextPreferences.markupTag=xmlObj[0].name;
alert(app.changeTextPreferences.markupTag);
app.activeDocument.changeText();
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.fontStyle = "Bold Italic";
app.changeTextPreferences.markupTag=xmlObj[1].name;
alert(app.changeTextPreferences.markupTag);
app.activeDocument.changeText();
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.fontStyle = "Italic";
app.changeTextPreferences.markupTag=xmlObj[2].name;
alert(app.changeTextPreferences.markupTag);
app.activeDocument.changeText();
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.capitalization = Capitalization.SMALL_CAPS;
app.changeGrepPreferences.markupTag=xmlObj[4].name;
alert(app.changeGrepPreferences.markupTag);
app.activeDocument.changeGrep();
I have already seen this code that link is provided by Larry. This code is for XML tags whereas there is no XML tag in my document.
I just want to replace text using character style with character style name code i.e., <bold>, <italic> etc.
I write below code, its working but adds code (< and >) before and after every paragraph. How to avoid NONE character style sheet in my code?
var myAppliedCStyle = [app.findGrepPreferences.appliedCharacterStyle]
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.changeGrepPreferences.changeTo = "<$0>";
for (i=0; i<myAppliedCStyle.length; i++)
{
app.findGrepPreferences.findWhat = ".+";
app.changeGrepPreferences.appliedCharacterStyle = myAppliedCStyle[i];
app.activeDocument.changeGrep();
}
Thanks in advance,
Mon
Hi Velprakash,
In my document there are some character styles applied in text i.e., bold, italic, boldItalic, and superscript etc.,
I want to use this character styles name as a code surrounding character style applied text, so that I can use this tags as a XML tag when required.
For example:
This is a <bold>bold</bold> text and <italic>italic</italic> text.
In above example character style bold and italic is used. And I need those styles as a codes before and after styled text.
Please help.
Thanks,
Mon
Try this:
styleCodes = ['Bold', 'Italic', 'Bold Italic'];
styleTags = ['bold', 'italic', 'boldItalic'];
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
for (i = 0; i < styleCodes.length; i++)
{
app.findGrepPreferences.fontStyle = styleCodes[i];
app.changeGrepPreferences.changeTo = '<'+styleTags[i]+'>$0<' + '/' + styleTags[i] + '>';
app.activeDocument.changeGrep();
}
Peter
Hi Peter,
Firstly, I would like to thanks for all your time and support.
Your JS code is searching fontStyle defined in variable (styleCodes) whereas I need to search any appliedCharacterStyle in the document. There are lots of character style available in the document. Is there any JS code to achieve this?
I also want to admit that Last year, I bought InDesign javascripting book written by you. It helps me a lot. Its great book for learner.
Thanks!
Mon
True, that's what you wanted:
charStyles = app.activeDocument.allCharacterStyles;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '.+';
// Start at 1 to skip charstyle [None]
for (i = 1; i < charStyles.length; i++)
{
app.findGrepPreferences.appliedCharacterStyle = charStyles[i];
app.changeGrepPreferences.changeTo = '<'+charStyles[i].name+'>$0<' + '/' + charStyles[i].name + '>';
app.activeDocument.changeGrep();
}
Peter
North America
Europe, Middle East and Africa
Asia Pacific