Copy link to clipboard
Copied
Hello Everyone,
I need help to replace within one specific xml tag, <Name>, all paragraph return with a Line break and keep the paragraph return that is already at the end of the tag.
Thanks
Paul
You have to adapt this code snippets to your situation, e.g. ...
...var _rootXMLElement = app.activeDocument.xmlElements.firstItem();
var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name");
for(var i=0; i<_nameXMLElements.length; i++) {
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "~b(?=[^~b])";
app.changeGrepPreferences.changeTo = "\n";
_nameXMLElements.changeGrep(true);
app
Copy link to clipboard
Copied
Hello Paul,
you can try this ...
Get xml elements with evaluateXPathExpression:
RootXMLElement.evaluateXPathExpression("//Name")
And change the paragraph returns in this xml elements with changeGrep in a loop:
app.findGrepPreferences.findWhat = "~b(?=[^~b])";
app.changeGrepPreferences.changeTo = "\n";
XMLElement.changeGrep(true);
Roland
Copy link to clipboard
Copied
Hello Roland,
Thank you for your help. I am getting an error message "RootXMLElement is undefined".
Could you please help? I am truly a beginner in Javascript.
Thank you again.
Copy link to clipboard
Copied
You have to adapt this code snippets to your situation, e.g. ...
var _rootXMLElement = app.activeDocument.xmlElements.firstItem();
var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name");
for(var i=0; i<_nameXMLElements.length; i++) {
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "~b(?=[^~b])";
app.changeGrepPreferences.changeTo = "\n";
_nameXMLElements.changeGrep(true);
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
Roland
Copy link to clipboard
Copied
Hi Roland,
Thank you again for your support. Unfortunately the code still showing the error " undefined is not an objet". I am not sure what to do.
Copy link to clipboard
Copied
The first line add;
#target indesign
Copy link to clipboard
Copied
Thank you uniqued_tol​ but now I am getting : Result: undefined
Copy link to clipboard
Copied
Hello Everyone,
I am getting result: undefined. And after running the script in indesign nothing change in the document.
Can you please help.
Copy link to clipboard
Copied
The console would only log what YOU would return. As your code returns anything but undefined, console displays…
Copy link to clipboard
Copied
Yes, I understand. So what should I do?
Copy link to clipboard
Copied
//Some code
//some code
//some code
"yolo" //will output "yolo" in the console
or if you are using a function
function yolo() {
return "yolo";
}
yolo();
//will output "yolo" in the console
Copy link to clipboard
Copied
var _rootXMLElement = app.activeDocument.xmlElements.firstItem();
var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name");
var count = 0;
for(var i=0; i<_nameXMLElements.length; i++) {
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "~b(?=[^~b])";
app.changeGrepPreferences.changeTo = "\n";
count+=_nameXMLElements.changeGrep(true).length;
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
alert(count+" replacements have been made…");
Copy link to clipboard
Copied
Thank you so much, Loic.AigonThe script is working now. Also my many thanks to drerol74 uniqued_tol
Copy link to clipboard
Copied
You could have scored drerol74​ as I mostly used his snippet here
Copy link to clipboard
Copied
Yes, You are right. I will do that. Sorry.
Copy link to clipboard
Copied
Can you please suggest the code I should put to have the result. The problem actualy is that when I run the code in Indesign nothing happen. All I wanted to do is : to replace within one specific xml tag, <Name>, all paragraph return(\r) with a Line break(\n) and keep the paragraph return that is already at the end of the tag.
Thanks for your help.