Quitter
  • Communauté internationale
    • Langue:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

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

Community Expert ,
Apr 18, 2023 Apr 18, 2023

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

 

 

 

 

 

 

SUJETS
Scripting
2.1K
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines

correct answers 1 bonne réponse

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. 

Traduire
Adobe
Community Expert ,
Apr 18, 2023 Apr 18, 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

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
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. 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
Apr 19, 2023 Apr 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));

})();

 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Guide ,
Apr 19, 2023 Apr 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. 

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

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

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Explorateur ,
Oct 03, 2024 Oct 03, 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.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Community Expert ,
Oct 03, 2024 Oct 03, 2024
LA PLUS RÉCENTE

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.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines