10 Replies Latest reply on Feb 23, 2013 3:10 PM by nick NYP

    Problems getting started using Variables & Datasets

    nick NYP

      Hello,

       

      I am new to this forum and new to scripting in Illustrator and scripting in general.

       

      I have an XML file containing a number of datasets each consisting of a number of text variables which I want to use to create a deck of trading cards.

       

      Each trading card consists of a Text Field for the title of the card and a number of icons, which are instances of various Symbols

       

      I have written a script to create each card and i can load the variables into my javascript using: newCard.importVariables(new File(xmlPath));

       

      I now need to access the data in the datasets in order to populate the card.

       

      To provide some context, this is an image of one of the cards with placeholder art:

       

      Screen Shot 2013-02-22 at 23.02.29.png

      Currently, for the icon in the top right hand corner (the PHASE icon) I am using the following code:

       

      phase = "night";

      phaseIcon = newCard.symbols.getByName(phase);

      phaseIcon1 = newCard.symbolItems.add(phaseIcon);

      phaseIcon1.top = 232;

      phaseIcon1.left = 140;

       

      What i want to be able to do, is to pull the value for the <phase></phase> TextVariable from my XML dataset and insert that into the script, but I don't know how. This is where I am stuck as to how to proceed. Any help is greatly appreciated.

       

      Thanks,

       

      Nick

        • 1. Re: Problems getting started using Variables & Datasets
          CarlosCanto Adobe Community Professional & MVP

          you have to get that info from the textFrame associated with that variable

           

          var idoc = app.activeDocument;
          var ivar = idoc.variables.getByName ('phase');
          
          
          alert(ivar.pageItems[0].contents);
          
          1 person found this helpful
          • 2. Re: Problems getting started using Variables & Datasets
            nick NYP Level 1

            Carlos,

             

            Thanks for your reply, that makes sense to me and I can get it to work from within an open illustrator document, i.e. i can manually bind a variable to the textFrame and then pull the data as in your script, but how would I go about automating this from within the script?

             

            I can create a new textFrame using:

             

            var iTextBox = idoc.textFrames.add();

             

            but i can't associate it with the variable.

             

            Thanks for your help,

             

            Nick

            • 3. Re: Problems getting started using Variables & Datasets
              pixxxel schubser Level 5

              Sorry for OT

               

              You really want to use Illustrator and create a script?

              Have you considered other options into consideration - eg the data merge in InDesign?

              • 4. Re: Problems getting started using Variables & Datasets
                Muppet Mark Level 5

                For me Adobe has provided variable data in it's apps for those who want some automation but don't script… I see little point in scripting this feature… Just MO of cause but if you are going to write javaScript then just do it all with this…? JavaScript to me is far easier to learn than that xml that AI seems to be sooooo bloody fussy over… Why not just CSV like PS… At leats that I can understand…

                • 5. Re: Problems getting started using Variables & Datasets
                  nick NYP Level 1

                  Thanks for posting. I can't give a reason for my choice of software and scripting language other than I *love* Illustrator and have yet to get to grips with InDesign and javascript is a language I have been trying to teach myself and am most familiar with.

                   

                  I have been reading through the ExtendScript Toolkit Javascript docs and have managed to get this working:

                   

                  idoc = app.activeDocument;

                  var wolfCardsXML = new XML ( "<wolfcards> <card> <title>Wind Through the Trees</title> <phase>dusk</phase> <fear>fear1</fear> <weather>weather1d</weather> <bonus1>pluck1</bonus1> </card> </wolfcards>" );

                  var currentCard = wolfCardsXML.card[0];

                  var phaseValue = currentCard.phase.toString();

                  var phaseIcon = idoc.symbols.getByName(phaseValue);

                  var phaseIcon1 = idoc.symbolItems.add(phaseIcon);

                   

                  This works fine to read the XML data, pull the <phase> value and then use that to pull a Symbol of the same name and place it on the artboard, which is exactly what I want to be able to do. Obviously tho, I have 100+ <card> elements i want to store and loop through to produce all the cards in the deck, but it seems that new XML () will let me do this if i can just pull in the xml data from a txt file as one long text string. Could anyone please suggest a way of being able to do this? Thanks

                  • 6. Re: Problems getting started using Variables & Datasets
                    CarlosCanto Adobe Community Professional & MVP

                    Hi nick, I agree with the others above, scripting variables is a little in the dark side. Can you elaborate a little bit more on your workflow? what kind of document do you have right before importing the variable file? what does your script do so far? and what do you need it to do afterwards?

                    • 7. Re: Problems getting started using Variables & Datasets
                      nick NYP Level 1

                      Hello and thanks to everyone for their time and attention.

                       

                      I have a document with a Symbol library containing all the images i need to create the icons and artwork for the cards i want to generate. I also have an Excel spreadsheet containing all the data for each card (100 in total). I have the datasheet as both .csv and xml files.

                       

                      My script generates the card design as illustrated above, bringing in title text and icon names from a list of variables that are currently being inputed directly at the start of the script. That is the bit i want to make dynamic. The script then saves the card and moves on to the next one in the deck. When the script has created all the cards in the deck, it will then lay out the cards in a set of 3x3 grids to allow printing on perforated card stock.

                       

                      Thanks

                      • 8. Re: Problems getting started using Variables & Datasets
                        nick NYP Level 1

                        So, this is what i have now managed to get working at the moment:

                         

                        idoc = app.activeDocument;

                        var xmlPath = "~/desktop/wolfCardsDeck.txt";   // This is my datasheet formatted with XML tags but saved in plain txt format

                        var myXmlFile = new File(xmlPath);

                        var xmlFileValue = myXmlFile.open("r");

                        var myXmlString = myXmlFile.read();

                        var stringContents = myXmlString.toString();

                        var wolfCardsXML = new XML (stringContents); // This line converts the info from the txt file to readable XML data

                        var currentCard = wolfCardsXML.card[0];

                        var phaseValue = currentCard.phase.toString();

                        var phaseIcon = idoc.symbols.getByName(phaseValue); // This finds the icon that corresponds to the phase variable in the XML

                        var phaseIcon1 = idoc.symbolItems.add(phaseIcon);

                         

                        I am not sure if this repeats the functionality of AI's variables/datasets methods but it does now seem to be a workaround that does what i want it to do. I can use this now as a framework to build a set of functions that will loop through the dataset and pull the different variables directly. Am I missing something obvious?

                        • 9. Re: Problems getting started using Variables & Datasets
                          CarlosCanto Adobe Community Professional & MVP

                          ok, then you only needed the datasets to be able to get the string data, right? this proved to be not as easy as it seemed

                           

                          -----------------

                           

                          then to get the string data you used the xml file instead, your last sample looks fine, I was making the same thing while you posted your sample, I used the xml file though

                           

                          var xmlfile = File.openDialog("Select a valid XML file","XML:*.xml", false);
                          if(xmlfile != null) {
                              xmlfile.open("r");
                              var xmlstring = xmlfile.read();
                              xmlfile.close();
                              xmlfile = null;
                          } else {
                          alert("Error opening XML file.");
                          }
                          var wolfCardsXML = new XML (xmlstring);
                          var currentCard = wolfCardsXML.card[0];
                          var phaseValue = currentCard.phase.toString();
                          
                          
                          alert(phaseValue);
                          
                          • 10. Re: Problems getting started using Variables & Datasets
                            nick NYP Level 1

                            Thanks for that snippet Carlos, much appreciated. I've learnt a lot in the last 24 hours! Thanks for your help.