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 @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));
})();
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?
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.
Copy link to clipboard
Copied
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.