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

Failing regex causing XML files to not be encoded correctly when run from illustrator.

Community Beginner ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

My script takes a TSV file of messages, writes XML files for each one, then imports those in as variables on a template file, save pdf, rinse and repeat. 

The script works fine when run from vscode but I can't get it to work when running from illustrators scripts folder. The XML files generated aren't encoded correctly or "Text encoding Unicode (UTF-8) isn’t applicable." as Finder puts it. The files that fail are ones with characters like right quote marks or an é.

The script should regex these out before writing the XML file and it does: from vscode. But not when run from illustrator. And I'm losing my mind trying to find out why. Link to github repo of script and test files: https://github.com/MitchPope/AddMessageToCard.git

TOPICS
Scripting

Views

212

Translate

Translate

Report

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

Valorous Hero , Mar 06, 2021 Mar 06, 2021

Hmmm do any of those special characters do something to spoil the XML? I just used UTF-8 as file encoding, when you open a file for writing, (set the encoding to "UTF-8") and I was able to use just the replacing of strings which are not XML-valid:

 

function disguiseXmlEntities(str){
  str=str.toString();
  str=str.replace(/&(?!(amp;|gt;|lt;|quot;|apos;))/g, "_#_amp_#_");
  str=str.replace(/</g, "_#_lt_#_");
  str=str.replace(/>/g, "_#_gt_#_");
  str=str.replace(/'/g, "_#_apos_#_");
  str=str.re
...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

Hmmm do any of those special characters do something to spoil the XML? I just used UTF-8 as file encoding, when you open a file for writing, (set the encoding to "UTF-8") and I was able to use just the replacing of strings which are not XML-valid:

 

function disguiseXmlEntities(str){
  str=str.toString();
  str=str.replace(/&(?!(amp;|gt;|lt;|quot;|apos;))/g, "_#_amp_#_");
  str=str.replace(/</g, "_#_lt_#_");
  str=str.replace(/>/g, "_#_gt_#_");
  str=str.replace(/'/g, "_#_apos_#_");
  str=str.replace(/"/g, "_#_quot_#_");
  return str;
};

function undisguiseXmlEntities(str){
  str=str.toString();
  str=str.replace(/_#_amp_#_/g, "&amp;");
  str=str.replace(/_#_lt_#_/g, "&lt;");
  str=str.replace(/_#_gt_#_/g, "&gt;");
  str=str.replace(/_#_apos_#_/g, "&apos;");
  str=str.replace(/_#_quot_#_/g, "&quot;");
  return str;
};

So far as I know, this has worked out for users across the world in many different languages with special characters. Maybe something else is at play due to your script running in different ways from VSCode as compared to Illustrator.

Also out of curiosity, is there some reasons why in your workflow it's necessary to use the datasets instead of simple scripting methods to set the text of text frames? I'm curious because it's a lot more work to do this compared to the latter and there may be some interesting reasons why this is preferred and I'd like to know those!

Votes

Translate

Translate

Report

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 Beginner ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

LATEST

I still have no idea what the root cause was and I never will because the second part of your reply made me reassess my aproach. It wasn't necassary to use datasets, I was just re-using code from another more complicated project.

I just rewrote the script to change the text directly without an xml inbeween and illustrator seems ot just deal with any special characters. Thanks for your reply, I'd forgot there's more than one way to do things! Updated the repo if anyone comes across this and needs it.

Votes

Translate

Translate

Report

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