• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Run Find/Change Query with Script

Explorer ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

Ok so I've searched everywhere it seems and no luck finding how to do this simply task. 

 

How does one simply run a Find/Change Text query on only selected text with a script?

 

Thanks in advance,

 

-Andy

TOPICS
How to , Scripting , Type

Views

893

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

Disregard previous. I roll with this grep search function, which allows me to set multiple find and change parameters and target the object (doc, story, selection) that I am trying to change. If using escape Grep searches, you need to include two backslashes.

 

 

 

//helper funciton to facilitate easy, robust GREP replacements
var myChangeGrep = function( findPrefs, changePrefs, story ) {

		app.findGrepPreferences = NothingEnum.NOTHING;
		app.changeGrepPreferences = NothingEnum.NOTHING;

		for ( var i in findPrefs ) {
			app.findGrepPreferences[i] = findPrefs[i];
		}
		for ( var j in changePrefs ) {
			app.changeGrepPreferences[j] = changePrefs[j];
		}
		story.changeGrep( true );

		app.findGrepPreferences = NothingEnum.NOTHING;
		app.changeGrepPreferences = NothingEnum.NOTHING;
	};

 

Sample use case: 

 

myChangeGrep({findWhat: "Hello"}, {changeTo: "World", pointSize: 10}, app.selection[0]);  

 

You can expand on it with multiple finds and changes. 

myFindWhatChangeTo = function(selection) {
    var replacements = [
        ["findfirst", "replacement1"],
        ["findsecond", "replacement2"],
    //etc
    ];
    for (var i = 0; i < replacements.length; i++) {
        myChangeGrep({findWhat: replacements[i][0]}, {changeTo: replacements[i][1]}, selection);
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

There is a built-in script called Find Change By List already in the InDesign Scripts panel. Maybe you could open the .jsx file and explore how it is made? I hope that is not too simple an answer for you!

Mike Witherell

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

Try this code sample:

////////////////////////////////////////////////////////////////////////////////////////////

try{
    app.findTextPreferences = NothingEnum.NOTHING;
    app.changeTextPreferences = NothingEnum.NOTHING;
    app.findTextPreferences.findWhat = app.selection[0].texts[0].contents.toString();
    app.changeTextPreferences.changeTo = "My Text To Change";
    app.documents[0].changeText();
    app.findTextPreferences = NothingEnum.NOTHING;
    app.changeTextPreferences = NothingEnum.NOTHING;

    }
catch(e){}

////////////////////////////////////////////////////////////////////////////////////////////

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

OP, this is helpful, but would take the entire selection and use it as your find query, and replace that selection everywhere else it appears in the document. Some modifications, tying together what I explained in a roundabout way above to only find in the selection, unless you want want Sunil offered: 

////////////////////////////////////////////////////////////////////////////////////////////

try{
    app.findTextPreferences = NothingEnum.NOTHING;
    app.changeTextPreferences = NothingEnum.NOTHING;
    app.findTextPreferences.findWhat = "Text to change";
    app.changeTextPreferences.changeTo = "My Text To Change";
    app.selection[0].texts[0].changeText();
    app.findTextPreferences = NothingEnum.NOTHING;
    app.changeTextPreferences = NothingEnum.NOTHING;

    }
catch(e){}

////////////////////////////////////////////////////////////////////////////////////////////

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

LATEST

If you want to run multiple searches you could wrap the search setup in a function and call it as many times as needed. Something like this grep search where the parameters are the find string and the change string.

 

//replaces multiple tabs with single tab
grepSearchSelection("\\t+", "\\t");
//replaces multiple spaces with one space
grepSearchSelection("\\p{zs}{2,}", "\\s")

function grepSearchSelection(f,c){
    app.findGrepPreferences.findWhat=NothingEnum.NOTHING
    app.changeGrepPreferences.changeTo=NothingEnum.NOTHING
    app.findGrepPreferences.findWhat = f;
    app.changeGrepPreferences.changeTo = c;
    try {app.activeDocument.selection[0].changeGrep( true )}catch(e) {}  
}

Also 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines