Skip to main content
Inspiring
December 21, 2021
Answered

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

  • December 21, 2021
  • 1 reply
  • 815 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 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,
Redy

This topic has been closed for replies.
Correct answer r-bin
var applyFile = File(./Styles/My styles/"+dropdown1.selection.toString()+".style");
applyFile.open("r");
applyFile.encoding = "BINARY"; //// !!!!!!
applyFileContent = applyFile.read();
applyFile.close();
        
var tempPreset = File(./Styles/My styles/tempPreset.ffx");
tempPreset.open("w");
tempPreset.encoding = "BINARY"; //// !!!!!!
tempPreset.write(applyFileContent.replace(/\r\n/g, "\n"));  //// !!!!!!
tempPreset.close();

1 reply

Stephen Marsh
Community Expert
Community Expert
December 21, 2021

You just need to escape the \r or \n characters with an extra back-slash \

 

from:

 

\r
\n

 

to:

 

\\r
\\n

 

 

.replace(/\\r\\n/g, "\n");

 

https://www.codingame.com/playgrounds/218/regular-expressions-basics/reserved-characters
https://www.regular-expressions.info/characters.html

RedyCodeAuthor
Inspiring
December 21, 2021

How could I forget that 🤦‍♂

For some reason, it still doesn't work tho...

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.open("w");
tempPreset.write(applyFileContent.replace(/\\r\\n/g, "\n"));
tempPreset.close();

 Any clue why?

Thanks in advance,
Redy

r-binCorrect answer
Legend
December 21, 2021
var applyFile = File(./Styles/My styles/"+dropdown1.selection.toString()+".style");
applyFile.open("r");
applyFile.encoding = "BINARY"; //// !!!!!!
applyFileContent = applyFile.read();
applyFile.close();
        
var tempPreset = File(./Styles/My styles/tempPreset.ffx");
tempPreset.open("w");
tempPreset.encoding = "BINARY"; //// !!!!!!
tempPreset.write(applyFileContent.replace(/\r\n/g, "\n"));  //// !!!!!!
tempPreset.close();