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

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

Community Expert ,
Mar 09, 2022 Mar 09, 2022

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
TOPICS
Scripting
2.9K
Translate
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 , Mar 11, 2022 Mar 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
...
Translate
Adobe
Community Expert ,
Mar 10, 2022 Mar 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

Translate
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 ,
Mar 11, 2022 Mar 11, 2022

Hi Mark,

no worries. Looks I'm too deep into it as this just a small part of a hugh script...

 

to break it down:

i want to import a .txt file containing two unicode elements into an array to use them to search with the first element and replace with the second

 

Please find a super simple example attached

! ! ! rename the .pdf to .ai to open it in Illustrator ! ! !

 

the issue:

maybe I don't understand how unicode works in ExtendScript...

if I write it in code as variable replacing works fine:

array2.push('\u2030');
array2.push('\u0023');

 

if it's imported it does not... 

I know it's called U+2030 in the txt file but I thought to split and replace it would work out

but even imported as \u2030 does not do the job...

 

heres is my import code as well:

var ChangeListName = 'change_file.txt';

var separator = '/'; //for mac //for win:  separator = '\\';

//scriptRef is script path...

ChangeListPath = scriptRef + separator + ChangeListName;
var myFile = File (ChangeListPath);
myFile.open("r");
var curContentsCL = myFile.read(); //get text
myFile.close();

 

Translate
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 ,
Mar 11, 2022 Mar 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 😉

 

Translate
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 ,
Mar 11, 2022 Mar 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));
}

 

Translate
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 ,
Mar 13, 2022 Mar 13, 2022

holy moly!

CarlosCanto your the man!

didn't know that there is String.fromCharCode() yet...   NOW IT WORKS!

THANKS A LOT ! ! !

 

for those who want to follow up:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

 

I changed your function as there will be unicode with 3 digits as well... if I got you right yours is based on the last 4 digits/characters...

maybe I'm wrong but going with split will be more flexible to different unicode values later on...

pls let me know...

 

my final script (part of):

 

//importing txt file containing

//MyriadPro-Regular ArialMT U+2030 U+002a //-->several lines each tab separated

//split into rows

 

var thisGlyph = splittedRows[z][2]; //then get 3rd object in the desired line
var thisSubGlyph = splittedRows[z][3]; //and 4th object
thisNumberArray = thisGlyph.split('\+'); //splitt each using the +
thisNumberArray.shift(); //remove first array element
thisSubNumberArray = thisSubGlyph.split('\+'); //same with the substitut value
thisSubNumberArray.shift(); //remove first array element

 

if (splittedRows[c].length > 2) { // if changelist row >2 ==> has Glyphs to change
thisUcode = unicodeToString (thisNumberArray); //do the magic here
newUcode = unicodeToString (thisSubNumberArray); //same for substitut value
myUniCode = thisChar.contents; //as based on characters
thisChar.contents = replaceUniCode(myUniCode); //will be replaced

 

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


function unicodeToString (unicode) {
return String.fromCharCode(parseInt(unicode,16));
}

 

Translate
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 ,
Mar 13, 2022 Mar 13, 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?

Translate
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 ,
Mar 13, 2022 Mar 13, 2022
LATEST

you're welcome. 

 

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

Translate
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