Skip to main content
Community Expert
March 9, 2022
Answered

help needed! Illustrator - script importing csv file _ txt file with unicode (u+1234)

  • March 9, 2022
  • 2 replies
  • 2752 views

hi there,

appriciate your help:

 

## scenario

finally the script will import data from a csv file (basicly it is a txt file in my case, separated by tabs and returns) into an array (split, separate,nested) to change existing fonts and for some of them unicode based characters as well.

 

## question

how can I get those unicodes (u+1234) imported from that list in a propper way into that array so I can use them to search and replace others?

 

## already tried

if the array is manualy written it works fine -- I do need to get the unicode imported

already searched and tried for three days now... so please will you help me out?

find the txt and a demo ai-file attached ! ! ! rename attached .pdf ---> .ai to open in Illustrator ! ! ! ...

correct font or correct unicode numbers are not important (it just has to workout to switch existing ones)

 

script:

----------------------------

 

#target illustrator

//// ##### ----->>> This array is suposed to be imported from a csv/txt list

var array0 = [];
array0.push('PoloCEF-Regular');
array0.push('PoloST11K');

var array1 = [];
array1.push('Swift');
array1.push('Sally');

var array2 = [];
array2.push('MyriadPro-Regular');
array2.push('ArialMT');
array2.push('\u2030'); //unicode import does not work
array2.push('\u0023'); //unicode import does not work

var array3 = [];
array3.push('ZapfDingbatsITC');
array3.push('ArialMT');
array3.push('\u2794'); //unicode import does not work
array3.push('\u0023'); //unicode import does not work

var splittedRows = [];
splittedRows.push(array0);
splittedRows.push(array1);
splittedRows.push(array2);
splittedRows.push(array3);

//// ##### ----->>>

var docRef = app.activeDocument;
var allTextFrames = docRef.textFrames;
var autoSubList = ['PoloST11K','SomeFont','Swift','ArialMT','ZapfDingbatsITC','MyriadPro-Regular']

//// ##### ----->>>

changeFontAndGlyphs();
alert("done");

// get list of used fonts based on characters
function changeFontAndGlyphs() {
for (var i=0 ; i<allTextFrames.length; i++) { // check all text frames
var allCharacters = allTextFrames[i].characters; // get all characters of this frame
for (var t=0; t<allCharacters.length; t++) { // check all characters
var charFontName = allCharacters[t].textFont.name; // get font name of this character
var thisChar = allCharacters[t]; // get this one character

for (var c=0; c<splittedRows.length; c++) { // check all rows of changelist
var thisCLFont = splittedRows[c][0]; // get 1st of ChangeList array = font for change
var newCLFont = splittedRows[c][1]; // get 2nd of ChangeList array = sub font to be changed to

for (var a=0; a<autoSubList.length; a++) { // check all fonts of autoSubList
var thisASLFont = autoSubList[a]; // get this AutoSubListFont
if (thisASLFont == thisCLFont && charFontName == thisCLFont) {
// if AutoSubList font is on changeListFont && character font has same name as changeListFont
thisChar.textFont = app.textFonts.getByName(newCLFont); //change character font to new changeList font
if (splittedRows[c].length > 2) { // changelist row >2 ==> has Glyphs to change
thisUcode = splittedRows[c][2];
newUcode = splittedRows[c][3];
myUniCode = thisChar.contents;
thisChar.contents = replaceUniCode(myUniCode);
} else {}
} else {}
}
}
}
}
}

function replaceUniCode() {
myUniCode = myUniCode.replace( thisUcode, newUcode );
return myUniCode;
}

 

-------------- End of Script

 

Textfile looks like this (tab separated):

 

previous font new font previous Glyphe new Glyphe
ArialMT PoloCEF-Regular
ZapfDingbatsITC ArialMT U+2794 U+0023
MyriadPro-Regular ArialMT U+2030 U+0023
 
-------
Thanks in advance
Nils
This topic has been closed for replies.
Correct answer CarlosCanto

try this, hope it helps

 

open a document with a text frame to test

// unicode to string
// carlos canto
// https://community.adobe.com/t5/illustrator-discussions/help-needed-illustrator-script-importing-csv-file-txt-file-with-unicode-u-1234/td-p/12802487

main ();

function main() {
    var idoc = app.activeDocument;
    var itext = idoc.textFrames[0];
    var unicode = "U+0023"; // or "\u2030", or "0023"
    var str = unicodeToString (unicode);
    
    itext.contents = str;
    
    alert(str);
}

function unicodeToString (unicode) {
    var str = unicode.substr(unicode.length-4, 4);
    return String.fromCharCode(parseInt(str,16));
}

 

2 replies

Community Expert
March 14, 2022

and  there might be an other side-issue I might run into:

there will also be font changing in some documents...

 

Question:

as Illustrator does change fonts AND unicode it only changes a font or unicode characters if both fonts contain the same unicode character... or versevise (no similar unicode - no font change)...

so, how can this be solved? replacing with basic characters in between before changing font and glyph?

 

or isn't that an issue at all when using a Script?

CarlosCanto
Community Expert
Community Expert
March 14, 2022

you're welcome. 

 

Yes, absolutely, be my guest. Change the code as needed to get the right digits.

m1b
Community Expert
Community Expert
March 10, 2022

Hi @Nils M. Barner, I am sorry for not understanding, but even with your script and sample files I don't know what you are trying to achieve. When I run your test code it does stuff to your sample file, but I have no idea what it's supposed to do, or where it's failing.

Is your problem that you can't import unicode text from a text file? If so, are you sure that the textfile contains unicode text? How are you importing the text file? Are you importing it as UTF-8?

Your text file seems fine to me (it's UTF-8) but it doesn't contain any characters higher than ascii. You have the unicodes as numerals after 'u' in the text file, but we have no idea what you are doing with those. Why not just enter the actual character in your text file? eg. ➔ instead of u2794?

Sorry for so many questions. I think it would help us if you could make a simpler test file, with just one piece of text that fails for you, and show what it should do if it worked. Also include the text importing code, in case that is part of the problem. Then we can test.

- Mark

Community Expert
March 11, 2022

@m1b  schrieb:

Why not just enter the actual character in your text file?

 

As always with scripting it's all about plenty of files...

Otherwise creating a script would be obsolet 😉

 

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
March 11, 2022

try this, hope it helps

 

open a document with a text frame to test

// unicode to string
// carlos canto
// https://community.adobe.com/t5/illustrator-discussions/help-needed-illustrator-script-importing-csv-file-txt-file-with-unicode-u-1234/td-p/12802487

main ();

function main() {
    var idoc = app.activeDocument;
    var itext = idoc.textFrames[0];
    var unicode = "U+0023"; // or "\u2030", or "0023"
    var str = unicodeToString (unicode);
    
    itext.contents = str;
    
    alert(str);
}

function unicodeToString (unicode) {
    var str = unicode.substr(unicode.length-4, 4);
    return String.fromCharCode(parseInt(str,16));
}