How can I give text variables names to artboard names?

Community Beginner ,
Jan 06, 2018 Jan 06, 2018

Copy link to clipboard

Copied

I am currently trying to learn to script, am quite at the beginning. And I am wondering how to access the variables content, at the moment I only managed to access the variable?

screen.png

Hope that makes sense.

Please advise.

Regards, Eva

TOPICS
Scripting

Views

2.1K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Jan 10, 2018 Jan 10, 2018
Or import a single-column text file directly with this:#target illustratorfunction RenameArtboardsFromText() {    if (app.documents.length) {        function getTextFile() {            var fileType = ($.os.match("Windows")) ? "*.txt;*.csv;" : function(f) {                return f instanceof Folder || (f instanceof File && f.displayName.match(/(\.txt|\.csv)$/i));            };            var textFile = File.openDialog("Choose a text file.", fileType);            if (textFile != null) {           ...

Likes

Translate

Translate
Community Beginner ,
Jan 06, 2018 Jan 06, 2018

Copy link to clipboard

Copied

Forgot to mention that my question is for Extended Java script

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 06, 2018 Jan 06, 2018

Copy link to clipboard

Copied

Hi 0evelynaa0​,

for the beginning:

Re: Script access to dataset variables

Have fun

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 06, 2018 Jan 06, 2018

Copy link to clipboard

Copied

Long story short, the only way(s) to access the variable data content in a dataset are to:

  • Keep track of a textbox which is associated with a variable and analyze its contents every time datasets are updated (populated) in the document.
  • Export the variables.xml file (you can do this once in the beginning of the script) and use wild text-parsing custom-made functions to tear, dissect and Frankenstein back together the variables from the xml text. Or if you can figure out the way to deal with namespaces in ExtendScript's XML() functions, then you can do the same but more properly.

So suppose you have a textframe which has a variable, you can change the artboard's name like so:

var stringData = app.activeDocument.variables.getByName("MyVar").pageItems[0].contents;

app.activeDocument.artboards.getByName("My Artboard").name = stringData;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

Thank you for the answers.

So from what I understand there is no way of cycling directly through the variables contents. I assume the tracking textbox changes is quite winded.

Another question though. Is there any particular reason that variables in Illustrator are thought out this way ?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

In my code, it's all done - it's easier than you may be thinking. Those lines actually accomplish all of that. You just have to have your new dataset activated, the text automatically updates when this happens.

I am not sure if any of the Adobe scriptable applications have a scripting method to go by the variable-data records in memory - and I only say this after just having looked through the InDesign object model where I'm not seeing an object relating to a specific record or a collection of records.

As for why it's set up this way - I think it's because the most common use is for people to import data files and then do edits afterwards.  The Illustrator "Variables" or the InDesign "Data Merge" features are for exporting variable documents from a template. Using scripting to do custom things in the document is probably a rare case, so they likely did not bother to give us the ability to traverse records in the scripting context. However, since we do use scripting, it's just a matter of writing enough custom code to accomplish your task.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

Hi Eva, what are you trying to accomplish? there may be a different approach to achieve what you need.

you said you want to access the variable contents (Grace?) to name your artboards, but you circled the Data Set (also named Grace) in your screen shot.

are your variables bound to text frames? or is that not a requirement?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 17, 2020 Jan 17, 2020

Copy link to clipboard

Copied

Here's the answer to the original question ("...I am wondering how to access the variables content, at the moment I only managed to access the variable?"). With this script, I keep my artboard names up to date. I select the text box object which has my "SHEET NUMBER" named inside it, then click the "Make Object Dynamic" button on the Variables palette to create a variable to hold its string value. I created an action to "Update Data Set" and then "Insert Menu Item..." to execute this script. It's super quick and saves me from having to put in my sheet number in two places every time I update my drawings.

 

Artboard Names populated with Variable ValuesArtboard Names populated with Variable ValuesMulti Artboard LayoutMulti Artboard Layout

Typical Page LayoutTypical Page Layout

For reference, I work in the Architectural Signage industry, which means my drawings go through many revisions. Often, revisions have global "callout" changes, and all pages are detailed with HotDoor CADtools dimensions and labeling. When we did layouts in single artboard documents, these changes took up to four times as long. Now most of us are taking advantage of multiple artboards, meaning we can make global changes within the same file. For output, we use "Save a Copy..." with a single digit number, then select the "Save each artboard to a separate file" box and the "Range" radio button. This generates individual files with a number and an underscore on the front of the Artboard Name. These files are easy to keep track of because the last part of the name is the number of the revision. We use InDesign to arrange and to composite them into a single document.

 

 

 

 

function populateArtboardNamesWithObjectNames() {
        var a_doc = app.activeDocument;
        // initiate rename loop
        for (var ic = 0, abl = a_doc.artboards.length; ic < abl; ic++) {  
            if (a_doc.artboards[ic].active) {
                    }
                    // Object name is passed from Variable to Artboard_Name
                    var obj_name = a_doc.variables[ic].pageItems[0].paragraphs[0].contents;
                    var artboard_name = obj_name;
                    // Artboard is renamed to corresponding Object name in Variables palette
                    a_doc.artboards[ic].name = artboard_name;  
                }    
        }

    function renameArtboards() {
        if (app.documents.length == 0) {  
    alert("Active Document Not Found");  
        } else {  
            populateArtboardNamesWithObjectNames();
        }  
    }

    renameArtboards();

 

 

 

 

I hope this helps out someone, since other people have asked this same question on multiple message boards, but no one ever answers how you get at the data in the Variable (which is what the question really is). It took me more than a day and a half to track it down, and it doesn't seem to be documented very well. Here is the important bit. You have to have all these extra references in order to get at the object data or you just get the Variable name back as value.

 

 

 

var obj_name = a_doc.variables[ic].pageItems[0].paragraphs[0].contents;

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 11, 2020 Dec 11, 2020

Copy link to clipboard

Copied

Seriously, thank you. You just saved me a lot of time, I can't believe how poorly documented these functionnalities are.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 06, 2021 Jan 06, 2021

Copy link to clipboard

Copied

You're welcome. I originally located this info for calling the contents out of a variable on a blog about Illustrator scripting written in Japanese. I'm glad Google Translate works so well 😄

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 06, 2021 Jan 06, 2021

Copy link to clipboard

Copied

LATEST

Also, after this foray into learning Javascript for Illustrator, I tore out my hair for another day and a half figuring out how to search each Artboard within the current document at the coordinates of the Sheet Number AreaType box and automatically create a variable for each Artboard Name with the contents of that box. After that, I run the script example above and all my Artboard Names are updated without having to manually link the AreaType boxes.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 09, 2018 Jan 09, 2018

Copy link to clipboard

Copied

Silly-V, Not sure how I select the dataset. (it gets deselected when I run the script)

CarlosCanto , great question, I'm not sure, they both seam to contain my objects.

To give a bit more context, what I am trying to do is load a csv/txt into illustrator and assign each line to an artbord name. (there is more than one line and more than one artboard, hence the reason I need it automated )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 09, 2018 Jan 09, 2018

Copy link to clipboard

Copied

It's: app.activeDocument.dataSets[0].display()

Likewise, you can put the variable data from the art objects, back into the xml data, by somehow changing the content of the art to go against what's in the current dataset and then using the dataSet.update() method.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 09, 2018 Jan 09, 2018

Copy link to clipboard

Copied

if you just need a list of artboard names and you don't need to bind the variables to text frames, then it's straight forward. You don't need DataSets either. Just put your names list in the variable xml, import it and go through the list

variableNamesToArtboardNames.PNG

// variableNamesToArtboardNames

// CarlosCanto

// https://forums.adobe.com/thread/2434052

var idoc = app.activeDocument;

var vars = idoc.variables;

var abs = idoc.artboards;

for (j=0; j<vars.length; j++) {

    abs.name = vars.name;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jan 10, 2018 Jan 10, 2018

Copy link to clipboard

Copied

Or import a single-column text file directly with this:

#target illustrator

function RenameArtboardsFromText() {

    if (app.documents.length) {

        function getTextFile() {

            var fileType = ($.os.match("Windows")) ? "*.txt;*.csv;" : function(f) {

                return f instanceof Folder || (f instanceof File && f.displayName.match(/(\.txt|\.csv)$/i));

            };

            var textFile = File.openDialog("Choose a text file.", fileType);

            if (textFile != null) {

                return textFile;

            } else {

                alert("Canceled");

                return null;

            }

        };

        var textFile = getTextFile();

        if (textFile != null) {

            textFile.open('r');

            var namesArray = textFile.read().split("\n");

            textFile.close();

            var doc = app.activeDocument;

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

                try {

                    var thisName = namesArray;

                    var thisItem = doc.artboards;

                    thisItem.name = thisName;

                } catch (e) {

                    // do nothing

                }

            }

            alert("Done with " + i + " names");

        }

    } else {

        alert("No AI document is open or no selection.");

    }

}

RenameArtboardsFromText();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 13, 2018 Jan 13, 2018

Copy link to clipboard

Copied

Thank you,

Indeed it does everything all in one so I chose this answer being the most comprehensive as the best because it does what I am trying to accomplish, though I realize my first question was not worded like that.

By the way for importing the variables I was also using one of your scripts

Quite a life saver.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

I urge you to revisit this subject and take a look at my solution, which I posted in January, 2020. I also wrote another script (after the one above) which scans all the Artboards in the file for a textframe located in the lower right corner (see above page examples in my screenshots) and creates a variable with the stored value of whatever is in that textframe. The two scripts together have saved me and my team hours over the past few months. If you're interested in the other script, I would be happy to post it here and go over the finer points of understanding it and modifying it for your purpose.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines