Skip to main content
Participant
August 16, 2020
Answered

Running actions/script to show visible layers and save as PDF

  • August 16, 2020
  • 3 replies
  • 781 views

Hi! I am looking to automate a step for a process I'm working on. 

I have a file with multiple overlapping layers arranged in a certain way for printing purposes for card designs over several pages. I've also utilised Variable Data to feed data into the file which changes depending on the data. I proceed to turn on and off visibility of only certain layers  before printing. It's a repetitive process as the visibility of the specific layers to be turned off and on are fixed. 

Am wondering if there is a way to automate the turning off and on of specific layers and save each desired layout on screen as PDF? Each of the end layout is fixed so the steps are specific. 

thanks!

This topic has been closed for replies.
Correct answer Manan Joshi

Is it very much possible, i have written a small snippet to do this have a look. At the top, I have defined two variables

  • layertoHide :- this is an array with names of the layers that need to be turned off, all the remaining layers are made visible
  • destinationFilePath :- This is the path of the folder where you want to export the pdf, notice the path ends with a trailing /
  • The output file name of the pdf is the name of all the layers to be hidden concatenated with a ,
var layertoHide = ["Layer1", "Layer 2", "Layer 4"]
var destinationFilePath = "/Users/manan/Downloads/"
for(var i = 0; i < app.activeDocument.layers.length; i++)
{
	var l = app.activeDocument.layers[i]
	l.visible = true
	for(var j = 0; j < layertoHide.length; j++)
	{
		try{
			var ly = layertoHide[j]
			if(ly == l.name)
			{
				l.visible = false
				layertoHide = layertoHide.slice(0, i).concat(layertoHide.slice(i + 1))
				break;
			}
		}catch(e){}
	}
}
var pdfSaveOptions = new PDFSaveOptions();
pdfSaveOptions.pDFPreset = "[High Quality Print]"
pdfSaveOptions.viewAfterSaving = false;
app.activeDocument.saveAs(new File(destinationFilePath + layertoHide.toString() + ".pdf"), pdfSaveOptions)

 -Manan

3 replies

Manan JoshiCommunity ExpertCorrect answer
Community Expert
August 16, 2020

Is it very much possible, i have written a small snippet to do this have a look. At the top, I have defined two variables

  • layertoHide :- this is an array with names of the layers that need to be turned off, all the remaining layers are made visible
  • destinationFilePath :- This is the path of the folder where you want to export the pdf, notice the path ends with a trailing /
  • The output file name of the pdf is the name of all the layers to be hidden concatenated with a ,
var layertoHide = ["Layer1", "Layer 2", "Layer 4"]
var destinationFilePath = "/Users/manan/Downloads/"
for(var i = 0; i < app.activeDocument.layers.length; i++)
{
	var l = app.activeDocument.layers[i]
	l.visible = true
	for(var j = 0; j < layertoHide.length; j++)
	{
		try{
			var ly = layertoHide[j]
			if(ly == l.name)
			{
				l.visible = false
				layertoHide = layertoHide.slice(0, i).concat(layertoHide.slice(i + 1))
				break;
			}
		}catch(e){}
	}
}
var pdfSaveOptions = new PDFSaveOptions();
pdfSaveOptions.pDFPreset = "[High Quality Print]"
pdfSaveOptions.viewAfterSaving = false;
app.activeDocument.saveAs(new File(destinationFilePath + layertoHide.toString() + ".pdf"), pdfSaveOptions)

 -Manan

-Manan
Kurt Gold
Community Expert
Community Expert
August 16, 2020

I don't know how your documents are structured, but using Layer Visibility variables (see Variables palette) in conjunction with batch processing (see Actions palette) may be an appropriate approach.

femkeblanco
Legend
August 16, 2020

You can easily toggle visibility of specific layers by scripting, if you can specifically target those layers. Presuming they are named, the easiest way would be to target their name or part of their name. For example, the snippet below will make all layers starting with the (capital) letter X invisible.  

 

for (var i = 0; i < app.activeDocument.layers.length; i++) {
  var prefix = app.activeDocument.layers[i].name.substr(0, 1);
  if (prefix == "X") {
    app.activeDocument.layers[i].visible = false;
  }
}