Skip to main content
brian_p_dts
Community Expert
Community Expert
April 19, 2023
Answered

[JSX] Can't replace with line break character in text area frame

  • April 19, 2023
  • 2 replies
  • 1933 views

Hi all, 

I have a block of Area Text, no returns in it, and am trying to insert a line break into a line if the first character is a tilde (and delete the second character, a space. Below is my function. When I run it, it replaces the tilde with a hard return.  Font is Arial Bold. Running on a Windows machine, AI 27.4.1. Any thoughts? 

 

 

var seekFirstTilde = function(l /*Story.lines[i]*/) {
    try {
        var characs = l.characters;
        var conts = characs[0].contents;
        if (conts == "~") {
            characs[1].remove();
            characs[0].contents = "\n";
        }
    } catch(e) {}
    
    
    //l.characters[idx-1].contents = "\n";
}

 

 

 

 

 

 

Correct answer brian_p_dts

It was alerting as a square box (same if I paste into FR window). It showed a charCodeAt 3, so I ended up using contents = String.fromCharCode(3) and that worked. 

2 replies

femkeblanco
Legend
April 19, 2023

It appears that a new line is converted to a carriage return when a string is assigned to the contents of a text frame. Both the escape character and decimal value change. 

brian_p_dts
Community Expert
Community Expert
April 19, 2023

Interesting. Maybe I should try at an insertion point instead? 

m1b
Community Expert
Community Expert
October 3, 2024

It seems that the Extendscript File object write() and writeln() do this as well. It's *very* surprising to silently convert characters. For example, this explicit use of write converts the newline to a carriage return:

var f = new File("/tmp/test.log"); 
f.open("w"); 
f.write("test\n");
f.close();

 

Results:

> hexdump /tmp/test.log
0000000 6574 7473 000d
0000005

Same results if I replace `write("test\n")` with `writeln("test")`!

 

This is a surprising bug.


Yes that is bad. Don't expect that bug to be fixed though. Adobe is (I assume!) working hard on getting UXP scripting working for Illustrator which will give us much more modern access to the file system. I'm not convinced that the DOM object bugs will be automatically fixed in the UXP transition though so I still lodge and vote on DOM bugs.

Community Expert
April 19, 2023

That's strange. How about inserting a soft return manually and reading it via script to see what does Illustrator interpret it as.

-Manan

-Manan
brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertAuthorCorrect answer
Community Expert
April 19, 2023

It was alerting as a square box (same if I paste into FR window). It showed a charCodeAt 3, so I ended up using contents = String.fromCharCode(3) and that worked. 

m1b
Community Expert
Community Expert
April 19, 2023

Hi @brian_p_dts, I can't believe I haven't noticed this before (maybe I have and have forgotten). I consider this a bug, due to the behaviour being unexpected—converting U+000A to U+000D silently. And using U+0003 for a linebreak is non-standard, surely.

 

I have lodged a bug, so feel free to vote on it.

 

Here is script showing issue clearly:

 

(function () {

    var doc = app.documents.add();

    var tf1 = doc.textFrames.add();
    tf1.position = [100, 100];
    tf1.contents = 'Break\u000Ame.';
    alert('Linefeed (10)\ncharCode = ' + tf1.contents.charCodeAt(5));

    var tf2 = doc.textFrames.add();
    tf2.position = [200, 100];
    tf2.contents = 'Break\u0003me.';
    alert('End-of-text (3)\ncharCode = ' + tf2.contents.charCodeAt(5));

})();