PerfectPrepText: A smart way to style local formatting
One of the most popular free scripts for InDesign, preptext.jsx, was written by our own InDesignSecrets contributor Theunis DeJong, aka “Jongware.” He wrote it a few years ago, seemingly on the fly, in response to a plea for help from an InDesign user on the Adobe InDesign User-to-User forum. Preptext.jsx applies character styles to text with local formatting (bolds, italics, superscripts, and a few others) so you can retain the formatting when you apply your own paragraph styles or when you export to EPUB. It goes through the active story in just a few seconds, seeking out locally-styled text, creating the character styles (yes!) for you as needed and applying them throughout.
The problem is that preptext.jsx sometimes does too good of a job, creating and applying character styles to every instance of bold (or italic, or all-small caps, etc.) text, even if it’s not locally-formatted; that is, even if the formatting is part of the paragraph style applied to the text. Jongware says that’s because his script is designed to be run before you apply any InDesign styles, like when you first import a Word file and opt to delete styles but retain local formatting (you’ll find this option in the Word Import Options dialog box). But sometimes I forget, and need to run it after.
For example, I’ll often want to run Jongware’s script at this point, when I have some basic paragraph styles already applied — Headline and Body — but haven’t yet built up any character styles. The italic and small caps etc. you see in the sample are all locally formatted.
After I run preptext.jsx, it looks the same, except now we have a bunch of character styles (yay!) that have been applied to the locally-formatted text (yay!)
BUT: Take a look at my insertion point in the headline in the second screen shot. Do you see how the script created and applied the Bold character style to it? Even though the bold text wasn’t locally formatted—the Head paragraph style uses a bold typeface? That’s a potential problem. What if you edit the Head style so it used a non-Bold typeface? This text would still be bold, because of the character style applied to it.
Even if you never need to edit the paragraph styles, this “double-hit” of style sheets makes editing EPUBs or HTML files exported from affected files very difficult. Look at how the headline text in this exported file has both paragraph style classes and span (character style) classes for the same bit of text, “An Introduction to”:
Just as in the live InDesign file, if you decide that you want your CSS for the Head style to not be bold, you’d have to remember to go back to the HTML file and strip out all the superfluous span classes affecting the Head paragraphs too.
I asked my friend and über-scripter Peter Kahrel if he could help me out. Peter’s no longer doing freelance scripting, but in his off-hours, over a period of a few days (and a few versions he sent over), he came up with a solution! I’m calling it PerfectPrepText. It doesn’t replace Jongware’s wonderful preptext.jsx, it’s more of a prologue and epilogue to it.
How PerfectPrepText Fixes the Problem
Peter’s add-on to Jongware’s script deconstructs the story temporarily; first removing (he calls it “neutralizing”) the paragraph styles, then runs preptext.jsx (you need to have the preptext.jsx script at the same level as Peter’s somewhere in your InDesign scripts folder), then reasserts the original paragraph styles. I asked Peter to give me two versions of his script, the first called PerfectPrepText_Ask.jsx that asks what step you want to do, like this:
The steps would be:
- Run PerfectPrepText_Ask.jsx to neutralise the styles
- Run preptext.jsx (or any other style mapper) to create and apply character styles, and optionally do other things to the text while in this weird limbo state
- Run PerfectPrepText_Ask.jsx again to restore the styles—it remembers what gets applied where, somehow
And then for the rest of us who would prefer not to have a heart attack seeing all our styling “neutralised” in Step 1, Peter made an “unattended mode” version called PerfectPrepText_Do.jsx that zips through all three steps on its own. It still requires that you have the preptext.jsx script in your scripts folder (at the same level) but you don’t need to run it yourself.
I ran PerfectPrepText, and here’s the “After” on the same bit of text I started with at the top. All the character styles were created and applied to the locally-formatted text only. Notice how the word “Introduction” in the headline (where my cursor is) has no character style applied to it. Yay!
And the proper styling goes all the way through to EPUB/HTML export, too — no extraneous <span> classes in the <p> class:
Peter doesn’t have a Donate button on his web site, but he said he just wanted to help support InDesignSecrets. Thank you Peter! And I have to mention that if you need more oomph, David’s PowerStyles plug-in does the same intelligent creation/application of character styles to locally-formatted text, plus it has many other useful text styling features. If you haven’t checked out the free trial yet, you really should — it’s pretty cool.
Meanwhile, you can download PerfectPrepText.zip from our web site — it contains three files: The _Ask and _Do versions of PerfectPrepText, and preptext.jsx (Thank you Jongware!)
InDesignSecrets






The link for Thomas Silkjær’s script is down. Would you please email me a copy of the scriptm or repost a live link for download?
In the following link (Adobe Foruns) you find detailed instructions by Jongware himself on how to adapt the preptext script to run with Batcher:
http://forums.adobe.com/thread/429756?start=0&tstart=0
Hi David, thanks for the tip! I tried PerfecPrepText with the batcher script (fantastic idea, btw), but it didn’t work. I think it’s because preptext needs the cursor in a frame before execution.
@Marcio: There is a batch tool available here: http://www.rorohiko.com/wordpress/batcher-automate-processing-of-multiple-indesign-extendscript-documents-via-dragdrop/ I don’t know if it works with this particular script, but it’s worth a try.
Thanks a lot for this, I’m using it frequently, saves a lot of work! But I have hundreds of files and one question: it’s possible to batch apply this script? Would be fantastic to have an “unattended mode”.
Great script! very useful. My solution to include styles like “medium italic”is: before running the script, you can use find/change to apply a character style, in the character style options activate Export Tagging (CS6) and select a class for exporting, then run the script for the standard bold and italic text.
Thank you for the quick answer, Jongware. I had no idea it would be that difficult. But I think your “Find Font”-solution could at least make things a bit easier, since I work on books with more than 300 pages and many overrides that I need to export to epub, and I don’t want to embed fonts for that anyway (print fonts do not necessarily work on web/devices, and eReaders mostly forbid embedding).
Peter, you’re right about the issue with non-standard “bold” and “italics” names. Unfortunately there is no real good solution! InDesign only knows a font is bold or italics because it checks some internal bits inside the font, and that information is not accessable through acripting.
My own solution before running the script is to use Find Font to replace all non-standard fonts to a standard one. But that’s a pre-production solution, and it won’t work nicely on an otherwise done document.
If you have a fairly limited set of fonts, your suggestion to add the oddball style names to the script ought to work just fine.
okay, I guess I found the problem:
The script only recognizes “bold”, “italic” and so on, but some fonts have other names like “medium italic”, “75 Bold” (Helvetica Neue) and so on.
So preptext and also perfectpreptext works perfectly fine on CS 6, it just doesn’t recognize custom font names for “bold” and “italic”, which is a bit of a shame for people like me dealing with many different fonts, but nonetheless thank you for this amazing script and maybe I can customize it so it recognizes the font names I tell him to (shouldn’t be too difficult).
This used to work fine for me in CS 5.5, but now in CS 6 it doesn’t work anymore.
I get a JavaScript error when placing my cursor anywhere (roughly translated from German (from normal preptext): “errorstring: undefined is no object”, line: 3, source: if). When placing it in the textframe, it seems to do something, but in fact there are no character styles created and the overrides remain.
The whole problem seems to occur only with preptext, since the wrapping around with your addition works fine, but the script itself does nothing.
Anyone here with CS 6 having the same problem? It would be amazing to be able to keep using this handy script!
Greetings from Germany,
Peter
Here’s a link to Thomas’ site…lots of great scripts!
http://indesigning.net/auto-create-paragraph-and-character-styles
You’re not missing a thing, Erica! Sounds like a great script. Do you have a link?
I’m not sure if I’m missing something (and it’s quite possible I am), but I’ve been using Thomas Silkjær’s simple auto create p_c styles script to format imported text. I can run it directly after the import and it creates all paragraph and character styles for me. If I have any formatting already done, say a header with a bold font, it doesn’t create anything new for that text…I assume it sees it has a paragraph style and ignores it. It creates character styles for any locally formatted text.
The only thing I need to fix afterwards is the fact that the created character styles include font info, which I usually don’t want…I keep my character styles simple, like “bold,” “italic,” “red,” etc.
there are a few problems with align left/right and baseline.
after using the _Do-script or the sequence of “_Ask, preptext, _Ask”, the following happens:
- para-style: “justified”, locally formatted to “align left” -> looses its local formatting
- para-style: “justified”, locally formatted to “align right” -> retains its local formatting
similar to that are problems with the baseline:
- para-style “align to baseline”, locally formatted to ignore the baseline -> looses its local formatting
- para-style “don’t align to baseline”, locally formatted to align to the baseline -> retains its local formatting
but these problems also happen, when you load the para-styles “manualy” in the para-styles-window (via “load para-styles”), so it’s no bug of this script.
(using cs5/cs5.5 with newest updates on win7)
Yeh I guess you do. But as I say, I only use a new document to place text into before copying to the main document. I don’t usually save these files first.
I’m using it PerfectPrepText in CS5.5 without an issue. Eugene I’m guessing you need to save your document?
This script worked perfectly in CS3 – but wouldn’t work in CS5 and the “make it run script” doesn’t seem to work with it.
http://indesignsecrets.com/downloads/PreserveLocalFormatting.jsx
This is great. I’ve been wanting to ask for a fix for this for a long time. Thank you very much. But I’ll hold the champagne cork and party streamers for a moment; I’m getting an error
http://oi43.tinypic.com/avsojn.jpg
My thanks to the Dynamic Duo of Anne-Marie and Peter for coming up with this solution!
I couldn’t think of anything to change in preptext itself, but maybe that’s because I’m positively neurotic about my document construction: I always make sure all formatting is done through character styles, and hence I usually do not need this.
That is utterly fantastic.
Lovely, absolutely lovely. Thanks to you all.