0

I'm using JavaScript script in Photoshop (Ps 22.5.1 on macOS 11.6) to write out some information to a file.

When I represent a newline in my code with a \n, instead of getting a newline I get a ^M (control-M) in the file. How can I get a normal newline in the output?

Here is an example script that demonstrates the problem. I have this saved as test.jsx and am calling it in Photoshop as File->Scripts->Browse and then selecting the script.

#target photoshop

main();

function main() {
    var file = File("/some/path/test_info.txt");
    file.open('a');
    file.write("this is line #1\n");
    file.write("this is line #2\n");
    file.close();
}

The output is:

this is line #1^Mthis is line #2^M

The ^M characters are actually control-M, though if I do an "od -c" on the file I see they are \r (?):

$ od -c test_info.txt 
0000000    t   h   i   s       i   s       l   i   n   e       #   1  \r
0000020    t   h   i   s       i   s       l   i   n   e       #   2  \r
0000040

BTW, the script is loosely based on this code snippet from StackExchange.

1 Answer 1

1

Use file.writeln('str') (write line) instead of file.write('str') (write). Also you won't need the \n in your code after each line.

You can check the PS Tools Guide for more info on the File I/O functions.

2
  • Just tried that and unfortunately I see no change. However, setting file.lineFeed = "Unix" before writing makes it work. Bizarrely, setting lineFeed to "Macintosh" produces the control-Ms, while "Unix" doesn't. Thanks for putting me on the right scent!
    – raindog308
    Oct 25, 2021 at 18:02
  • @raindog308 weird. I also usually set file encoding to UTF-8, maybe that's what was missing? file.encoding = "UTF-8"; Oct 26, 2021 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.