Highlighted

Run Find/Change Query with Script

Explorer ,
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

697

Likes

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

Run Find/Change Query with Script

Explorer ,
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

698

Likes

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
May 21, 2020 0
Adobe Community Professional ,
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);
    }
}

Likes

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
Reply
Loading...
May 21, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
May 21, 2020 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
May 21, 2020 0
Adobe Community Professional ,
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){}

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

Likes

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
Reply
Loading...
May 21, 2020 0
rob day LATEST
Adobe Community Professional ,
May 21, 2020

Copy link to clipboard

Copied

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 

Likes

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
Reply
Loading...
May 21, 2020 0