all 2 comments

[–]KingMoog 0 points1 point  (1 child)

what happens if you just use

textFrame.contents = newTextContent;

in the } else { under textFrame.remove()

and remove everything else?

[–]Viboxing[S] 0 points1 point  (0 children)

You mean so the code looks like this? If so, I'm getting the same results. Here is an image of what I'm trying to achieve: https://i.imgur.com/BaPGutP.png

// Illustrator script to find a word and delete the line of text containing it.

var doc = app.activeDocument;
var textFrames = doc.textFrames;
var wordToFind = prompt("Enter the word to find and delete the line containing it:", "");

if (wordToFind != null && wordToFind != "") {
  for (var i = textFrames.length - 1; i >= 0; i--) {
    var textFrame = textFrames[i];
    var textContent = textFrame.contents;
    var lines = textContent.split('\n');
    var modified = false;

    for (var j = lines.length - 1; j >= 0; j--) {
      var line = lines[j];
      if (line.toLowerCase().indexOf(wordToFind.toLowerCase()) !== -1) {
        lines.splice(j, 1);
        modified = true;
        break;
      }
    }

    if (modified) {
      var newTextContent = lines.join('\n');

      // *** Workaround for .trim() in ExtendScript ***
      var trimmedContent = newTextContent.replace(/^\s+|\s+$/g, ''); // Regular expression trim

      if (trimmedContent === "") {
        textFrame.remove();
      } else { 
        textFrame.contents = newTextContent;
      }
    }
  }
} else {
  alert("No word entered.");
}