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:

  1. Run PerfectPrepText_Ask.jsx to neutralise the styles
  2. 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
  3. 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!)

Related Articles
Comments

30 Comments on “PerfectPrepText: A smart way to style local formatting

  1. 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.

  2. 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.

  3. 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)

  4. 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.

  5. 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

  6. 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).

  7. 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.

  8. 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).

  9. 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.

  10. 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”.

  11. 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.

  12. 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?

  13. Hello,
    I downloaded and installed these scripts after watching AM’s tutorial video on Lynda.com but I can’t get it to work! All scripts are together in the same folder. I am using InDesign CS6 which seems to be the issue b/c I can install and run PerfectPrep Text on a diff computer which has CS5 (tried a diff comp. w/ CS6, same error). This is what the error says
    “JavaScript Error!
    Error Number: 21
    Error String: undefined is not an object
    Engine: main File: /Users/lisatudor/Library/Preferences/Adobe InDesign/Version 8.0/en_US/Scripts/Scripts Panel/ePub/PerfectPrepText_Do.jsx
    Line: 14
    Source: app.doScript (prepscript);”
    Thanks for any help!

    • Problem solved. I noticed Marcio’s earlier post where he mentioned the cursor needed to be in the frame, doing so made this error go away.

  14. hi, im Hari. if we r trying to specially apply style while using GREP, there is an error in this script.please can some one help me.consider this pattern()(.+)() in GREP in find what and in change to im using $2,all the char style applied has changed in a different way. For Eg. In [Im Hari], consider hari should be in italics.after running the script if we use GREP to apply style. the word I(regular)m( has changed to italics) har(italics)i(has been changed to regular).

  15. These sound like fantastic scripts and I will download and try them : )

    My problem is a bit different though. Do you think Theunis, Peter, Thomas or David’s script will solve the following:

    All text in my InDesign files is already fully styled – both character and paragraph styles. Character styles are used heavily throughout the documents for special font sizing etc. However bold, italic and underline is applied through ‘local’ formatting and is often on top of an existing character style.

    So my question is will the script be smart enough to replace the underlying character style with a new style that includes all the formatting that was in the character style as well as the bold, italic or underline that is applied on top?

    Also, what would be really great, is if it kept the name that was used for the character style but just adds the word ‘bold’ or ‘italic’ or ‘underline’ to the new style name eg. char style ‘Quote All Cap’ might become ‘Quote All Cap Bold’ ????

    Many thanks. Heulwen

  16. This works great in CS6 (except for the strike through function, but that’s so rarely used anyway). Now if it would only work through the whole document and not just the story that the cursor is in. We often have sidebars and photos with captions that are anchored and therefore the text in them is not part of the main text thread.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>