Copy link to clipboard
Copied
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
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();
//
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
my grateful thanks with kisses for your magic again
Cheers
Suzan