Copy link to clipboard
Copied
Hi,
I want with javascript select text from a XLM element (like screen capture : double-clic on "CHAINE", select "CINE CINEMA)
Thanks for your help
Regards
Ronald
Copy link to clipboard
Copied
Hi Ronald,
I actually have no idea of XML, but when I look at your screenshot, you can reach a specific element with the following lines. If you need all lines, you can loop through the elements.
// root-Element
var root = app.activeDocument.xmlElements[0];
// first child-Element
var notule = root.xmlElements[0];
// specific Element
var chaine = notule.xmlElements.itemByName("CHAINE");
alert (chaine.contents);
Kai
Copy link to clipboard
Copied
Hi Kai,
Thanks you for your answer, but I want selected on the page the XLMelement's contents, not get only ...
Regards
Copy link to clipboard
Copied
Ronald, did you try my lines? In most cases there is no need to select something. The line chaine.contents give you the content of the xml-element. chaine.characters or chaine.paragraphs gives you characters or paragraphs. E.g. if you want to apply a character style to the content you could do something like:
var c = chaine.characters.everyItem();
c.appliedCharacterStyle = app.activeDocument.characterStyles.itemByName("red");
Copy link to clipboard
Copied
yes I tried your lines but I really need to select the text from xml elment 😉
Copy link to clipboard
Copied
I'm interested in why you have to select the text?
In the meantime: I was not able to select the text, if I try chain.characters.everyItem().select().
But you can try:
chaine.texts.everyItem().select();
Copy link to clipboard
Copied
Thanks Kai, it works 🙂
var root = app.activeDocument.xmlElements[0];
var notule = root.xmlElements[0];
var chaine = notule.xmlElements.itemByName("CHAINE");
chaine.texts.everyItem().select();
app.selection[0].fillColor= "DOC";
app.activeDocument.select(NothingEnum.NOTHING);
It's for TV Magazine, the script import on a page several "notule" and I must change the color of <CHAINE> depending on the notule's attribute "rubrique". There may be on the page, several identical <CHAINE> but with different colors.
Bonne soirée
Copy link to clipboard
Copied
In this case, I would loop through all 'chaines' and would apply the colors in dependence from the attribute or maybe do a GREP-Search.
However: I did not see the importance of selecting text. Instead of your last 3 lines, the following should also work:
chaine.texts.everyItem().fillColor = "DOC";
If you have more than one chain-element on the page, I would assume, that you get a error, if you try to select something?!
Copy link to clipboard
Copied
In my testfile this one assign a color in dependance of the value of the attribute:
// root-element
var root = app.activeDocument.xmlElements[0];
// loop through the children
for ( var i = 0; i < root.xmlElements.length; i++ ) {
var notule = root.xmlElements;
var rubNotule = notule.xmlAttributes.itemByName("rubrique");
// specific Element
var chaine = notule.xmlElements.itemByName("CHAINE");
if ( rubNotule.value == "cinema" ) {
chaine.texts.everyItem().fillColor = "DOC";
}
else if ( rubNotule.value == "TV" ) {
chaine.texts.everyItem().fillColor = app.activeDocument.swatches[5];
}
}
Copy link to clipboard
Copied
You're right, why make it complicated when you can make simple 😉
Thank you again for your help
Ronald