Skip to main content
Known Participant
June 10, 2024
Answered

Apply character style to whole word which contains a letter

  • June 10, 2024
  • 1 reply
  • 401 views

Hello.

 

I have a long list of words (one word per line) like so:

"Pothole 

Painting 

Comb

School 

Tent

Ring"

 

I want to apply character style to all the words that contain letters "n", and extract those words to a new indesign file.

 

Please help.

Thanks and regards. 

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

For exporting the matched words to a file you can use the following script

 

var doc = app.documents[0]
app.findGrepPreferences.findWhat = "[\\l\\u]*[nN]+[\\l\\u]*"
var f = doc.findGrep()
app.findGrepPreferences.findWhat = NothingEnum.NOTHING
var s = ""
for(var i = 0; i < f.length; i++){
    s += f[i].contents + "\n"
}
if(s != ""){
    var f = new File("~/Desktop/out.txt")
    f.open("w")
    f.write(s)
    f.close()
}

 

I could not make your regex work so I used @pixxxelschubser's regex. You can change the regex to what you want in the code in line no. 2, just make sure you escape \. The script will create a file names out.txt on the desktop

-Manan

1 reply

pixxxelschubser
Community Expert
Community Expert
June 10, 2024

Does this Grep helps you?

[\l\u]*[nN]+[\l\u]*

 

Moiz5FB2Author
Known Participant
June 11, 2024

Thanks for the reply.

[\w]*[nN]+[\]*

this grep pattern worked for me.

 

Kindly help with the extraction of all matching words to a new file as well.

I can guess that this involves some kind of script.

Thanks.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
June 11, 2024

For exporting the matched words to a file you can use the following script

 

var doc = app.documents[0]
app.findGrepPreferences.findWhat = "[\\l\\u]*[nN]+[\\l\\u]*"
var f = doc.findGrep()
app.findGrepPreferences.findWhat = NothingEnum.NOTHING
var s = ""
for(var i = 0; i < f.length; i++){
    s += f[i].contents + "\n"
}
if(s != ""){
    var f = new File("~/Desktop/out.txt")
    f.open("w")
    f.write(s)
    f.close()
}

 

I could not make your regex work so I used @pixxxelschubser's regex. You can change the regex to what you want in the code in line no. 2, just make sure you escape \. The script will create a file names out.txt on the desktop

-Manan

-Manan