Skip to main content
Inspiring
December 20, 2021
Answered

ExtendScript: Change the "\r\n" line endings with "\n" while keeping the "\r" line endings as "\r"

  • December 20, 2021
  • 1 reply
  • 370 views

Hey, I'm currently working on a script, and here's what it does:

It reads an already created file and then it writes the content of that file in a new file. Here's the code :

 

var applyFile = File("./Styles/My styles/"+dropdown1.selection.toString()+".style")
applyFile.open("r");
applyFileContent = applyFile.read();
applyFile.close();
        
var tempPreset = File("./Styles/My styles/tempPreset.ffx");
tempPreset.lineFeed = "Unix";
tempPreset.open("w");
tempPreset.write(applyFileContent);
tempPreset.close();

 


And then I got an issue, when writing the file it would convert all of the "\n" line endings to some "\r\n" line endings. To fix that, I added tempPreset.lineFeed = "Unix" which basically sets all of the line endings to "\n"

But that fix got me a new problem: in the already created file there are a few "\r" line endings, and when I'm adding the tempPreset.lineFeed = "Unix" it changes "\r" to "\n"

So here's what I want: I want a way to write the file while keeping the same line endings as the already created file ( so I want the \r to stay \r and the \n to stay \n, either by changing the \r\n by \n or just by writing it with the same line endings ), is there a way to do that?

PS : I already tried the .replace(/\r\n/g, "\n") and it somehow didn't work

Sorry for the long text and my bad English,
RedyMotion

This topic has been closed for replies.