Skip to main content
Suzan_V
Known Participant
May 4, 2016
Answered

Export and import variables

  • May 4, 2016
  • 1 reply
  • 1152 views

Hi all,

actually I'm seeking to find anyway to select all text on illustrator file then convert them all to variables then export it to .XML at the same path with illustrator file to be translated then import the same .XML after translation

i tried a lot but i stopped at the first step is to select all text frames and convert them to variables

var idoc = app.activeDocument;

var ilayer = idoc.activeLayer;

for (i=0; i<ilayer.textFrames.length; i++) {

    var itext = ilayer.textFrames;

    var idocvar = idoc.variables.add();

    idocvar.kind = VariableKind.TEXTUAL;

    try {itext.contentVariable = idocvar}

    catch (e) {idocvar.remove()}

}

any help please !!

Suzan

This topic has been closed for replies.
Correct answer Silly-V

Here is a basic example to get you started.

#target illustrator

function test(){

  var doc = app.activeDocument, thisVar, thisText, newDataset;

  // Set up the variables for each text & bind.

  for(var i=0; i<doc.textFrames.length; i++){

    thisText = doc.textFrames;

    thisVar = doc.variables.add();

    thisVar.kind = VariableKind.TEXTUAL;

    thisText.contentVariable = thisVar;

  };

  // Capture one dataset

  newDataset = doc.dataSets.add();

  newDataset.name = "Test Data Set";

  newDataset.update();

  // Export xml file in same directory as the AI file.

  doc.exportVariables(File(File(doc.fullName).parent + "/Test Variables.xml"));

};

test();

1 reply

Silly-V
Silly-VCorrect answer
Legend
May 4, 2016

Here is a basic example to get you started.

#target illustrator

function test(){

  var doc = app.activeDocument, thisVar, thisText, newDataset;

  // Set up the variables for each text & bind.

  for(var i=0; i<doc.textFrames.length; i++){

    thisText = doc.textFrames;

    thisVar = doc.variables.add();

    thisVar.kind = VariableKind.TEXTUAL;

    thisText.contentVariable = thisVar;

  };

  // Capture one dataset

  newDataset = doc.dataSets.add();

  newDataset.name = "Test Data Set";

  newDataset.update();

  // Export xml file in same directory as the AI file.

  doc.exportVariables(File(File(doc.fullName).parent + "/Test Variables.xml"));

};

test();

Suzan_V
Suzan_VAuthor
Known Participant
May 4, 2016

Thanks a lot dear, wroks like magic

well then can i ask about a code to import the same file back if possible?

Cheers

Suzan

Silly-V
Legend
May 4, 2016

You can import it back like this:

    var doc = app.activeDocument;

    var varsFile = File(File(doc.fullName).parent + "/Test Variables.xml");

    doc.importVariables(varsFile);

The text will already have those variables bound, so all you have to choose is the Test Data Set again from the datasets dropdown and the text will be updated.

Doing doc.dataSets[0].display() does not seem to work, so I force-refresh this by opening a new document and closing it, if that's really needed.