I am currently working on a script that takes artwork that has a number of text fields in illustrator and duplicating the artitem with the text fields filled with data from a FileMaker XML file. Note: Even though this is an Illustrator extendscript, I'm pretty sure its more relevant to javascript.
This is the first record in the XML file to give you an idea:
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="01-07-2009" NAME="FileMaker" VERSION="ProAdvanced 10.0v1"/>
<DATABASE DATEFORMAT="" LAYOUT="" NAME="" RECORDS="123" TIMEFORMAT=""/>
<METADATA></METADATA>
<RESULTSET FOUND="123">
<ROW MODID="11" RECORDID="11116">
<COL>
<DATA>BB-1</DATA>
</COL>
<COL>
<DATA>ELEVATOR</DATA>
</COL>
<COL>
<DATA>MACHINE</DATA>
</COL>
<COL>
<DATA>ROOM</DATA>
</COL>
<COL>
<DATA>107</DATA>
</COL>
<COL>
<DATA></DATA>
</COL>
<COL>
<DATA></DATA>
</COL>
<COL>
<DATA></DATA>
</COL>
</ROW>
And here is the block that is giving me problems:
//counts the amount of records in the XML = 123
var recordsLength = contents.elements().child(4).elements().length();
//counts the amount of data elements in each record = 8
var dataLength = contents.elements().child(4).elements().child(0).elements().length();
function drawArt() {
//this loop iterates through each record; matches the groupitem name in illustrator to the first data element in the record; and duplicates the groupitem and positions it in the document.
for (var i = 0; i < recordsLength; i++) {
signType = contents.elements().child(4).elements().child(i).elements().child(0).child(0).text();
gpTarget = docRef.groupItems.getByName(signType);
newArt = gpTarget.duplicate();
newArt.top = y;
newArt.left = x;
x+=(gpTarget.width + 20);
redraw();
//counts the amount of text fields in each newArt (i added - 1 to the end because there is always a text field at the end that should not be filled in)
var txtFrmLength = newArt.textFrames.length - 1;
//this loop iterates through each data element and gets the length of it
for (var t = 1; t < dataLength; t++) {
nodeLength = contents.elements().child(4).elements().child(i).elements().child(t).child(0).text().length();
//this if statement looks to see if a data element contains data or is empty; if its not empty move to next loop; if it is, go back to first loop
if(nodeLength > 0) {
//this loop iterates through the text fields and fills in the data from the XML
for (var u = 0; u < txtFrmLength; u++)
newSign.textFrames[u].contents = contents.elements().child(4).elements().child(i).elements().child(t).child(0).text();
} else {
break;
}
}
}
}
drawArt();
}
main();
Basically the script should duplicate and position a group item, put the data from the XML into its text fields and then move to the next artitem.
I set the test to i < 2 in the first loop to see if I could narrow down where the problem is. The result I'm getting is each artitem thats been duplicated, all its text fields are filled with the 5th data node.
Can anybody help me figure out what I'm doing wrong? Thanks.