Skip to main content
Participant
April 21, 2019
Answered

Find text with both formatting and non-formatting to replace

  • April 21, 2019
  • 2 replies
  • 733 views

With the above screenshot, I need to remove the space before superscript text. I'm a beginner in coding such, could someone suggest a code to replace?

This topic has been closed for replies.
Correct answer Manan Joshi

Hi,

Try the following code

app.findGrepPreferences = null

app.findGrepPreferences.appliedCharacterStyle = "superscript"

var a = app.documents[0].findGrep()

while(b = a.pop())

{

    var idx = b.index

    var con = b.parentStory.insertionPoints.itemByRange(idx - 1,idx).contents

    if(con == " ")

    b.parentStory.insertionPoints.itemByRange(idx - 1,idx).contents = ""

}

app.findGrepPreferences = NothingEnum.nothing

Edit :- Incorporated the suggestion made by Peter Kahrel​ in the comment below, we can also change the algorithm as suggested in that post. However the code in this post will also work fine

-Manan

2 replies

Participant
May 10, 2019

It works fine now, considering the insertion points with XML tags.

app.findGrepPreferences = null; 

app.findGrepPreferences.appliedCharacterStyle = "superscript" 

    var a = app.documents[0].findGrep() 

    while(b = a.pop()) 

    { 

        var idx = b.index 

        var con = b.parentStory.insertionPoints.itemByRange(idx - 2,idx-1).contents 

        if(con == " ") 

        b.parentStory.insertionPoints.itemByRange(idx - 2,idx-1).contents = "" 

    } 

    app.findGrepPreferences = NothingEnum.nothing 

Thanks Peter, and special thanks to Manan Joshi.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
April 21, 2019

Hi,

Try the following code

app.findGrepPreferences = null

app.findGrepPreferences.appliedCharacterStyle = "superscript"

var a = app.documents[0].findGrep()

while(b = a.pop())

{

    var idx = b.index

    var con = b.parentStory.insertionPoints.itemByRange(idx - 1,idx).contents

    if(con == " ")

    b.parentStory.insertionPoints.itemByRange(idx - 1,idx).contents = ""

}

app.findGrepPreferences = NothingEnum.nothing

Edit :- Incorporated the suggestion made by Peter Kahrel​ in the comment below, we can also change the algorithm as suggested in that post. However the code in this post will also work fine

-Manan

-Manan
Participant
May 9, 2019

It's not working still, may be the insertion points referred to the XML tags!

Community Expert
May 10, 2019

I retested it on my end and it works, can you share a sample InDesign document so that i can see what is different at your end

-Manan

-Manan