Skip navigation
Currently Being Moderated

Scripting inline styles to text

Aug 1, 2012 9:06 PM


I am having trouble getting my head around the indesign DOM in regards to programmatically (javascript) applying text styles inline and on the fly. By that I mean the text items I want formatted don't exist in the current document at the moment the script is executed. To give an example

In the current document there is only one textframe called "frame1"

In the script...

var str1 = "$";

var str2 = "19";

var st3 = "95";


The result I wish to see in the text "frame1" (if written in html) is <sup>$</sup>19<sup>95</sup> All on the one line


I found this post Solution 2 maybe how to do it,


myTextFrame = app.selection[0]; // is this necessary? can i use textFrames.itemByName(name)?

firstInsertionPoint = myTextFrame.insertionPoints[-1].index;

myTextFrame.contents += "New Text";

myAddedText = myTextFrame.characters.itemByRange(myTextFrame.insertionPoints[firstI nsertionPoint],


// i assume i would apply position.superscript here

myAddedText.appliedFont = app.fonts.itemByName("Trajan Pro"); // etc.


but I do not understand exactly what it's doing especially with the insertion points.  I have coded Visual Studio Projects for Office Word and the syntax looks kind of familiar but I find indesign documentation too spartan to help me. Grant Gamble's book was very helpful until I wanted to go down this weird path.


I am using indesign 5.5 and ESTK


Thank you in advance



Hi again,

I tried playing with the above source post solution 1, the move method of the paragraph collection has it's parameter list around the wrong way at least according to indesign 5.5. Has there been a change between versions? Is this the right way of going about this?

thanks again

  • Currently Being Moderated
    Aug 1, 2012 11:14 PM   in reply to uraeus1618

    Maybe you shouldn't be bothered with insertionPoints -- depends on what you really want to do. In your case (styling money amounts) it might be easier to fill your frame with text, then using Find/Change to add formatting. Example:


    myFrame = app.activeDocument.textFrames.item("frame1");

    myFrame.contents = "$19.95";


    app.findGrepPreferences = app.changeGrepPreferences = null;

    app.findGrepPreferences.findWhat = "\\\x{0024}";

    app.changeGrepPreferences.position = Position.superscript;




    Mark as:
  • Currently Being Moderated
    Aug 3, 2012 12:39 PM   in reply to uraeus1618

    The dollar symbol is a special case. With any other character you could have used the literal, e.g. "£" and "¥". But ironically, InDesign's grep has trouble finding the dollar symbol, which is also a metacharacter standing for "end of string". That's why you should search for the dollar symbol's unicode value. In other words, searching "\\$" does not work, but "\\\x{0024}" does.


    To superscript just the 95 cents part, you would do search "(?<=\\\x{0024}\\d\\d\\.)\\d\\d", which stands for two digits (\d\d) preceded by dollar-digit-digit-digit-dot (the (?<=. . .) construct stands for 'preceded by').


    This, though, matches only two-digit dollar amounts followed by two-digit cents, and you probably want to match single- and triple-dollar amounts too. You found yourself a tough intro into grep, what with InDesign's difficulty finding dollar symbols and Ggrep's general inability to cope with variable-length strings in so-called lookbehinds (the (?<=. . .) constructs). You could do multiple queries:









    If you feel like an alchemist, join the club. Grep isn't the easiest thing around but that shouldn't discourage you from learning it because it's really very powerful and even a smattering of grep will get you a long way. As to resources, for grep in InDesign you could try



    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 1:15 AM   in reply to uraeus1618

    > Interesting, you share the same name as the author of the book


    The cause for much confusion!


    It's in fact easier in your case not to try to automate the Find/Change panel, so to speak, but to go a different route:


    app.findGrepPreferences = null;

    app.findGrepPreferences.findWhat = "\\x{0024}\\d+";

    found = app.activeDocument.findGrep();

    for (i = 0; i < found.length; i++))


       found[i].characters[-2].position = Position.superscript;

       found[i].characters[-1].position = Position.superscript;



    Instead of "\\x{0024}\\d+", by the way. you can use \x{0024}\d+/.source, which makes expressions easier to read because there's no need to escape backslashes. It's also easier to transfer grep expressions from a script to the Find/Changedialog and v.v.


    Not sure what you meant by "I will need to run the grep after looping through the 'price' variables. I can store the regex in string array as vars are processed, then loop at the end."



    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 3:00 AM   in reply to uraeus1618

    Not at all, interesting story, thank you.



    Mark as:
  • Currently Being Moderated
    Aug 6, 2012 6:29 AM   in reply to uraeus1618

    myTextFrame.paragraphs.everyItem().justification = Justification.centerAlign;


    But you'd probably be better off defining a paragraph style and using that.



    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points