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";
}
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.
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
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.
Copy link to clipboard
Copied
Hi @brianp311, 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));
})();
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.
Copy link to clipboard
Copied
Interesting. Maybe I should try at an insertion point instead?