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

app.changeGrepPreferences.changeTo to a callback?

Explorer ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

I can use app.changeGrepPreferences.changeTo = "$1"; to insert the first match of  app.findGrepPreferences.findWhat. But how can I do this per found item as a call to a function? Can I give it a callback, and if yes: how..?

TOPICS
How to , Scripting

Views

108

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

correct answers 1 Correct answer

Explorer , Aug 11, 2023 Aug 11, 2023

OK, I dont know if there is a solution with changeTo, but I found a simple solution with findGrep and iteration thru the results:

	app.findGrepPreferences.findWhat = "\{\{[^:]*\:[^}]*\}\}";
	var hits = app.activeDocument.findGrep();
	for (var i = 0; i < hits.length; i++) {
 		hits[i].contents = hits[i].contents.replace(/\{\{([^:]*)\:([^}]*)\}\}/g,callback_function_here);
	}

Votes

Translate

Translate
Explorer ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

OK, I dont know if there is a solution with changeTo, but I found a simple solution with findGrep and iteration thru the results:

	app.findGrepPreferences.findWhat = "\{\{[^:]*\:[^}]*\}\}";
	var hits = app.activeDocument.findGrep();
	for (var i = 0; i < hits.length; i++) {
 		hits[i].contents = hits[i].contents.replace(/\{\{([^:]*)\:([^}]*)\}\}/g,callback_function_here);
	}

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

Hi @Lordrhavin, that is the normal way I think. However, bear in mind that editing the contents will replace any textStyleRanges in hits[i] with the styling of the first textStyleRange of hits[i]. So if you find "ABC" and the "B" is bold, you will lose that bold style and "ABC" will be styled to match "A". If this is a problem in your case, you can simply do a second changeGrep (or multiple changeGreps) on the found text, eg. hits[i].changeGrep().

- Mark

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
Explorer ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

I just ound out it is even worse: if I have the fifth char of my search text in bold, i end up with the fifth cha of my replace text in bold. How can I give the whole replacement text the style of the first search char? Because: that would be great!

 

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

I'm surprised it works that way. I can't test it myself right now. Anyway try doing a changeGrep() or changeText() on the hits(i) text object. 

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

Try setting the content to the first insertion point of the found text and then set the found text to blank. Also, you should execute your loop in reverse order, to cater for cases where the search and replacement are of different length. Soemthing like the following

for(var i = hits.length - 1; i >= 0; i--){
    hits[i].insertionPoints[0].contents = "replacementtext" 
    hits[i].contents = ""
}

-Manan

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
Explorer ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

@Manan Joshi Thatr jut sucesfully removed all my text^^ 😄

it looks like this at my oe:

 

	for(var i = hits.length - 1; i >= 0; i--){
		hits[i].insertionPoints[0].contents = hits[i].insertionPoints[0].contents.replace(/\{\{([^:]*)\:([^}]*)\}\}/g,lookupEntry);
		hits[i].contents = "";
	}

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

It does not remove the whole text for me. Probably you share a sample file for me to test the grep expression and your replacement. However, my code does not work as I intended. The following works for me though

for(var i = hits.length - 1; i >= 0; i--){
    hits[i].insertionPoints.itemByRange(1, -1).contents = ""
    hits[i].contents = "replacementtext"
}

-Manan

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

Also looking at your code, you should run the replace method on hits[i].contents and not hits[i].insertionPoints[0].contents. So the code would be something like

 

for(var i = hits.length - 1; i >= 0; i--){
    var rt = hits[i].contents.replace(/\{\{([^:]*)\:([^}]*)\}\}/g,lookupEntry)
    hits[i].insertionPoints.itemByRange(1, -1).contents = ""
    hits[i].contents = rt
}

 

-Manan

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

LATEST

What EXACTLY are you trying to achieve? 

 

ID-Tasker - most powerful tool ever created for InDesign

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