Skip to main content
Known Participant
February 23, 2024
Answered

Does anyone know how to script a find/replace within specific paragraph style?

  • February 23, 2024
  • 2 replies
  • 610 views

Hi again!

 

I'm looking for a script to remove a specific word from any instances of a specific paragraph style. So for any text with ParagraphStyle find Text and replace with "". I've found scripts to do find/replace but never with the style specified. Does anyone have any ideas?

 

Thanks in advance!

 

Eubha

This topic has been closed for replies.
Correct answer rob day

Thank you so much, this does exactly what I need! Only issue is, as Robert alluded to above, it doesn't work if the style is inside a style folder. Can you help me understand how to alter this code to fix that please?

 

Thanks,

 

Eubha


Try this:

 

var ps = app.activeDocument.paragraphStyleGroups.everyItem().paragraphStyles.itemByName("TextStyle")
pStyleSearch(ps,"World", "");


/**
* Paragraph Style Grep find and change 
* @ param ps the find paragraph style 
* @ param f the find string 
* @ param c the change string
* @ return void 
*/
function pStyleSearch(ps,f,c){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = {includeFootnotes:true, includeHiddenLayers:true, includeMasterPages:true, wholeWord:false} 
    app.findGrepPreferences.appliedParagraphStyle = ps;
    app.findGrepPreferences.findWhat = f;
    app.changeGrepPreferences.changeTo = c;
    app.activeDocument.changeGrep( true )
}

 

2 replies

jmlevy
Community Expert
Community Expert
February 23, 2024

I am not a scripter but you don't need any script to do this. Simply run a find change and specify the paragraph style you want by clicking on the icon which is surrounded on my screenshot.

Known Participant
February 27, 2024

Thanks but I'm looking to add it to a script so it's done as part of a larger automated process.

rob day
Community Expert
Community Expert
February 27, 2024

I'm looking to add it to a script so it's done as part of a larger automated process

 

Hi @EubhaLiath , You could add a helper function to your larger script, something like this:

 


pStyleSearch(app.activeDocument.paragraphStyles.itemByName("TextStyle"), "World", "");

/**
* Paragraph Style Grep find and change 
* @ param ps the find paragraph style 
* @ param f the find string 
* @ param c the change string
* @ return void 
*/
function pStyleSearch(ps,f,c){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = {includeFootnotes:true, includeHiddenLayers:true, includeMasterPages:true, wholeWord:false} 
    app.findGrepPreferences.appliedParagraphStyle = ps;
    app.findGrepPreferences.findWhat = f;
    app.changeGrepPreferences.changeTo = c;
    app.activeDocument.changeGrep( true )
}

 

 

Does this:

 

 

 

Robert at ID-Tasker
Legend
February 23, 2024

You need to set appliedParagraphStyle in FindTextPreference - something like this:

 

 

var doc = app.activeDocument;
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = doc.allParagraphStyles("my style");

 

 

https://community.adobe.com/t5/indesign-discussions/script-find-paragraph-style-under-the-folder/td-p/12833881

 

Known Participant
February 27, 2024

Thank you!

Robert at ID-Tasker
Legend
February 27, 2024

You are welcome.