Copy link to clipboard
Copied
Hi,
let's assume that I have a bunch of paragraphs, each one of them has a xmlTag assigned. Now, say that I want to remove one of them:
// collect elements
var idElements = myDoc.xmlElements.item(0).xmlElements.itemByName('Section_1').xmlElements.everyItem().getElements();
// select one for removal
var removeElement = idElements[3];
// remove element
removeElement.remove();
// remove unused tags
app.activeDocument.deleteUnusedTags();
Which removes the element, but leaves the document with an empty paragraph (empty line) that has a paragraph style assigned to it (the same as the removed xml element). How can I remove the element in a way that leaves no trace of it behind (remove not only the element, but also empty space with the style assigned that's left behind)?
Thanks in advance!
Copy link to clipboard
Copied
Hi,
Your code works fine for me, i think the issue is with the way you have tagged your paragraphs. If the paragraph marker is outside the tag it won't be removed with the remove method. See the screenshot of my document below
-Manan
Copy link to clipboard
Copied
Hi Manan,
thanks for your quick response. It works if your tags 'include' the return character, like in your example. However, because the functionality of some other code, I need to avoid including the return character. Below is an example of how tagging works in my scripts:
app.doScript("main()", ScriptLanguage.JAVASCRIPT, null, UndoModes.ENTIRE_SCRIPT, "Remove single element");
function main() {
var myDoc = app.activeDocument;
var lastPage = myDoc.pages.lastItem();
// add new text frame
var myTextFrame = lastPage.textFrames.add();
myTextFrame.name = 'Section_1';
myTextFrame.geometricBounds = [12, 12, 260, 198];
// Tag this frame
var newXMLTag = myDoc.xmlTags.add('Section_1');
var myXMLElement = myDoc.xmlElements.item(0).xmlElements.add(newXMLTag, myTextFrame.parentStory.insertionPoints.item(-1));
// prepare elements
var elements = {
'element_1': {
'id': 1,
'content': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
},
'element_2': {
'id': 2,
'content': 'Natoque penatibus et magnis dis parturient montes nascetur ridiculus mus.'
},
'element_3': {
'id': 3,
'content': 'Sollicitudin ac orci phasellus egestas tellus rutrum tellus pellentesque eu.'
}
};
// display elements
for(ei in elements) {
var thisElement = elements[ei];
if(thisElement.id > 1) {
// add empty paragraph at the end of parent story to make space before next element
myTextFrame.parentStory.insertionPoints.item(-1).contents = "\r";
}
// create new tag to mark new element
var newXMLTag = myDoc.xmlTags.add("element_" + thisElement.id);
// tag new paragraph
var myXMLElement = myDoc.xmlElements.item(0).xmlElements.add(newXMLTag, myTextFrame.parentStory.insertionPoints.item(-1));
// add content at the end of the parentStory
myXMLElement.contents = thisElement.content;
}
// collect elements and their tags from InDesign
var idElements = myDoc.xmlElements.item(0).xmlElements.itemByName('Section_1').xmlElements.everyItem().getElements();
// select element to remove
var removeElement = idElements[1];
// remove element, but it will leave a gap in its place
removeElement.remove();
}
This example above results in what I mean - after removing the selected element, it will leave a gap (empty line) in its place. Additionally, if that element had a paragraph style assigned to it, the style will remain (and if I ever attemp to place another paragraph in that empty line, it will have that remaining style applied to it).
Does anyone have an idea how to remove the element (without having to include the 'return' character within its tag) so it also removes the gap and any styles that were assigned to it? Thanks.
Copy link to clipboard
Copied
In that case use the following line
removeElement.paragraphs.everyItem().remove();
just before the removeElement.remove()
This will remove all the paragraphs within the xmlelement
-Manan
Copy link to clipboard
Copied
It doesn't really work (when I try to remove the element after removing its paragraphs, InDesign will throw an error saying that 'The requested action could not be completed because the object no longer exists') but your answer has put me on a right track and after some trial and error I've managed to get what I wanted (removing different elements with different methods seems to work the way I want it to work, i.e. regular text is removed by paragraphs.everyItem().remove() and stuff like tables, graphics are removed with simply using .remove() on an object). Thanks Manan.
Using the occasion can anyone tell me what's the difference between paragraphs, texts, words and lines in InDesign (especially in regards to removing the elements)?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more