• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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";
}

 

 

 

 

 

 

TOPICS
Scripting

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 19, 2023 Apr 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. 

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

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));

})();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 03, 2024 Oct 03, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2024 Oct 03, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines