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..?
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);
}
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);
}
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
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!
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.
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
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 = "";
}
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
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
Copy link to clipboard
Copied
What EXACTLY are you trying to achieve?