Skip to main content
Inspiring
December 11, 2018
Question

InDesign show/hide layers via javascript

  • December 11, 2018
  • 1 reply
  • 2523 views

I'm attempting to create a data-merge document with a placed Illustrator showing the 50 states on each page. The illustrator file will eventually contain hidden layers showing each state in a contrasting color.

During the data-merge step, I'll place a state abbreviation in a text box identifiable via a script label (stateName). I'm hoping to set the visibility of its corresponding layer in the placed illustrator file to true. (If stateName = "CA," the CA layer in the placed illustrator file will be changed to visible on that same page).

Then the script would progress to the next page and perform the same function. For testing purposes, my attempt at this point only contains layers for California and Minnesota. Eventually it will contain all 50 states.

Currently, InDesign is pulling the variable twice from the page I have open, and placing the map twice on the second page, so I have a lot of work to do...

var doc = app.activeDocument

doc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin

var _folder = Folder.selectDialog("Select a folder"); 

var _files = _folder.getFiles(); 

var myPages =doc.pages.length;

 

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


   var stateLabel = "stateName"

   myPage = app.properties.activeWindow && app.activeWindow.activePage

   myTextFrames = myPage.textFrames.everyItem().getElements().slice(0), 

   l = myTextFrames.length

   myVariable 

  

   while (l--) { 

   if (myTextFrames[l].label != stateLabel) continue

   myVariable = myTextFrames[l].contents

   break;

  } 


   if(myVariable == "CA")

  { 

   function FindOCG(stateDestinationMap) {

   this.layers["CA"].visible = true;

  };

  }

   else(myVariable == "MN")

  {

   function FindOCG(stateDestinationMap) {

   this.layers["MN"].visible = true;

  };

  }

}

This topic has been closed for replies.

1 reply

Community Expert
December 11, 2018

Hi matthewjm ,

hm, I'm not sure how a single page looks like after the merge.

As I understand there is one graphic from AI placed plus a text frame among others that is labeled "stateName" as key and its value is the layer name that should show with the graphic.

How did you do the label?

Did you use method insertLabel() ?

Did you use the Scriptlabel Panel ?

Did you name the text frame?

Regards,
Uwe

matthewjmAuthor
Inspiring
December 11, 2018

Thank you for your reply.
Imagine the end product is a postcard. I'd set-up a single page document containing the map, and a text box that would receive text during the data merge specifying the state. Supposing we have 2500 recipients, after the data merge, that single page document would become a 2500-page document with postcards that are identical except for the variables we fed into it.

So no, I wouldn't need to use insertLabel( )—I set the label using the ScriptLabel panel. Affirmative to the next question—the text frame is named (stateName).

matthewjmAuthor
Inspiring
December 11, 2018

On the "template" document the text boxed labeled stateName would contain a tag <<state>>. After the data merge, that tag would be replaced 2500 times on 2500 dynamically created pages by the text "CA", "IN", "DE", etc. depending on the individual recipient's state. The goal is to highlight that state by setting the visibility of its corresponding layer in the illustrator to visible.