Import variables in Illustrator

Post Reply
User avatar
Terkelsen
Advanced member
Posts: 262
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Import variables in Illustrator

Post by Terkelsen » Mon Dec 13, 2021 9:20 am

I'm in need of a script to import a xml as variables in Illustrators variables panel.

I have the correct formatted XML and it works as expected when importing manually. I would like to automate this through Switch, which is why I'm looking for at script that can do this.

Anyone?

User avatar
Terkelsen
Advanced member
Posts: 262
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Import variables in Illustrator

Post by Terkelsen » Wed Dec 22, 2021 8:55 am

485 views but 0 replies :? Well, I found help elsewhere so here is the solution if anybody else would need it:

main();

//------------- MAIN CLOSURE -----------------------------
//--------------------------------------------------------

function main() {
var document = app.activeDocument;
var dataFile = new File("<path to the xml file>");;
document.importVariables(dataFile);
var dataSet = document.dataSets.getByName('<name of required data set>');
document.activeDataset = dataSet;
dataSet.display();
redraw();
}

freddyp
Advanced member
Posts: 691
Joined: Thu Feb 09, 2012 3:53 pm

Re: Import variables in Illustrator

Post by freddyp » Wed Dec 22, 2021 11:18 am

There was not much to go by Erik :D And the Illustrator scripting documentation was also not very helpful when looking for XML. Variables is the keyword I see now. A couple of remarks about the code snippet, though.

There is no need to have the main procedure but it is certainly not wrong. As documented here (https://www.enfocus.com/manuals/UserGui ... tions.html) you should use $doc instead of app.activeDocument, and you could make use of the arguments to get cleaner code. Not tested, but this is how I would rewrite it:

Code: Select all

var dataFile = new File($arg1); //place the path to the XML in the first argument of the element
var datasetName = $arg2; //and the dataset name in the second argument

$doc.importVariables(dataFile);
var dataSet = $doc.dataSets.getByName(datasetName);
$doc.activeDataset = dataSet;
dataSet.display();
redraw();

Post Reply