-1

I have multiple textframes in my document and i have created a script to replace specific words from a textframe as such:

var linebreak = "<br/>";

     if (string.indexOf(linebreak) >= 0) {
        var string2 = string.replaceAll(linebreak, "\r");
    } else {
        alert("false");
    }

myFrame = app.activeDocument.textFrames.itemByName("test");
myFrame.contents = string2;

But without having to choose the name of the textframe i would like to do it to all active textframes in the document

Liam
  • 27,717
  • 28
  • 128
  • 190
lanes123
  • 149
  • 1
  • 18
  • 2
    Please do not add "Solved" **inside your question**. Answers go in the box labelled "Your Answer". You are even allowed to mark your own answer as accepted. – Jongware Apr 07 '18 at 20:21

3 Answers3

2

Untested, but something along these lines should work:

var linebreak = "<br/>";

var myFrames = app.activeDocument.textFrames;

for (var i=0; i<myFrames.length; i++) {
   var myFrame = myFrames[i];
   var string = myFrame.contents;

   if (string.indexOf(linebreak) >= 0) {
      myFrame.contents = string.replaceAll(linebreak, "\r");
   } else {
      alert("false");
   }
}
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
  • returns an error saying "string.replaceall is not a function" – lanes123 Apr 05 '18 at 15:53
  • You should be able to modify it to suit your needs – Josh Voigts Apr 05 '18 at 16:07
  • @lanes123: there is no `replaceall` in Josh' code so you must have copied something wrong. Weirdly, there is no built-in `replaceAll` function either, but you seem to have it working in your own code. – Jongware Apr 05 '18 at 17:23
  • @usr2564301 there is a replaceAll in the IF statement where it is getting the error. Yeah it seems to work on my one but not Josh's. Weird, any solutions? – lanes123 Apr 06 '18 at 07:50
  • @lanes123: JavaScript is a case sensitive language, and your first comment said it was `replaceall`, that's why I mentioned it. If it works with your own script, check *why*. Does it include more lines than you are showing here? Also, does it actually *need* a specific function, rather than a plain `replace`? – Jongware Apr 06 '18 at 08:29
0

A different approach, also untested, but also should work:

app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = "<br/>";
app.changeGrepPreferences.changeTo = "\\r"
activeDocument.changeGrep();
cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
0

answer from OP

Solved:

var linebreak = "<br />";
var myFrames = app.activeDocument.textFrames;

for (var i = 0; i<myFrames.length; i++) {
   var myFrame = myFrames[i];
   var string = myFrame.contents;

    if (string.indexOf(linebreak) >= 0) {
            var string2 = string.replace(new RegExp(linebreak, 'g'), '\r');
    } else {
            alert("ERROR: Cant find linebreak");
    }
   myFrame.contents = string2;
}
Liam
  • 27,717
  • 28
  • 128
  • 190