Copy link to clipboard
Copied
I have been deleting all notes from stories by using notes.everyItem().remove and also with tables, but I was wondering if it was possible to only delete certain notes say for example notes that have some sort of prefix that lets the script know that only these notes should be removed?
Thanks for your help
Alex
You're absolutely right.
We can fix this easily by adding
j--;inside the if-statement.
The fixed code is:
var st = app.activeDocument.stories;
var c = 0;
var cSt = st.everyItem().notes.length;
for(i=0; i<st.length; i++) {
for(j=0; j<st[i].notes.length; j++) {
if(st[i].notes[j].texts[0].contents.indexOf("Info:") == 0) {
st[i].notes[j].remove();
c++;
j--;
}
}
}
alert(c + "/" + cSt + " notes removed.");
Copy link to clipboard
Copied
Hello @Alex25077296wbvx ,
If you are already using a script for this, it seems to me that a supplement using "if" is enough to resolve the issue only in those that meet the condition defined in the "if".
It's worth noting that you can "look" and apply the "if" condition to what you want to delete or what you don't want to delete. It's just a choice of how to construct the condition, perhaps determined by how your content looks.
Copy link to clipboard
Copied
This does the job:
var st = app.activeDocument.stories;
var c = 0;
var cSt = st.everyItem().notes.length;
for(i=0; i<st.length; i++) {
for(j=0; j<st[i].notes.length; j++) {
if(st[i].notes[j].texts[0].contents.indexOf("Info:") == 0) {
st[i].notes[j].remove();
c++;
}
}
}
alert(c + "/" + cSt + " notes removed.");
In this example, the note is only being removed if it starts with Info:.
Copy link to clipboard
Copied
That is perfect! Thank you very much for your help
Copy link to clipboard
Copied
@GNDGN, you can iterate Stories from 1st element (0 in JavaScript) as you are not removing any of them from the collection of Stories - but for Notes collection - as you are DELETING elements - you should ALWAYS iterate backward.
Unless you have to process from the first element - but then you need to take it into account and do not increase index after deletion.
Not sure about JS - but in VB - this code wouldn't work correctly, as after deleting, let's say 5th note - the next one won't be 6th - but 7th - 6th becomes 5th and 7th -> 6th, and so on...
Copy link to clipboard
Copied
Considering that "notes" is an array of objects, when I delete the contents of position 10, the contents of position 11 don't "fall" into box 10. Or does it immediately? I think it does... but only when the code is finished, right? Besides, I imagine we're deleting the contents of the "house" and not the house itself while the script is running. After the script finishes, the interface, or whatever it's called, reorders the array for the current moment... doesn't it?
Ps.: I understand the idea of deleting backwards. I agree and it makes a lot of sense.
Copy link to clipboard
Copied
Yes @Brito Haroldo, immediately.
In this case, @GNDGN is removing elements of the array - st[i].notes[j].remove(); - he isn't "clearing contents and leaving container" - he is permanently deleting "container".
Then he refers to the "next" element in the array - but "next" element drops down and replaces currently deleted element.
Purging of the elements isn't done "after" - it is happening instantly - that's why going backward - in case of deleting - is so important.
And even if it was a Collection - it would be the same - as he is referring to elements by their Indexes.
He could use "go to the next after the current one" - which wouldn't work either, as "current" doesn't exists anymore and there would be an error... unless he saves a reference to the next BEFORE deleting current.
Copy link to clipboard
Copied
You're absolutely right.
We can fix this easily by adding
j--;inside the if-statement.
The fixed code is:
var st = app.activeDocument.stories;
var c = 0;
var cSt = st.everyItem().notes.length;
for(i=0; i<st.length; i++) {
for(j=0; j<st[i].notes.length; j++) {
if(st[i].notes[j].texts[0].contents.indexOf("Info:") == 0) {
st[i].notes[j].remove();
c++;
j--;
}
}
}
alert(c + "/" + cSt + " notes removed.");
Copy link to clipboard
Copied
But it is still just a workaround for the wrong algorithm...
Why can't you do it right?
Copy link to clipboard
Copied
How do you get this to remove selected notes in tables with the same reference?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now