Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to change Object Layer Options of a placed PDF

Participant ,
Jan 19, 2022 Jan 19, 2022

Is there some trick to writing a script to change to the object layer options of a PDF placed in an InDesign file? I have no trouble doing it with placed INDD, PSD, or AI files, but placed PDFs do not work.

 

For example, here's just a simple script to turn off the second layer of a placed graphic, selected in the active INDD document:

app.selection[0].graphics[0].graphicLayerOptions.graphicLayers[1].currentVisibility = false;
 
I've found that this works just fine for INDD, PSD, and AI files, but running it on a PDF selected returns the error "Object is invalid". I believe this is due to the layers of a PDF appearing under a sort of "folder" containing the name of the file that originally generated the PDF. For example:
 
Object layer options of a placed INDD file:
INDD.pngexpand image
Object layer options of a placed PDF (made from the above INDD file):
PDF.pngexpand image
 
I also found that if I refer to "graphicLayers[0]", the script does not fail, but it doesn't really do anything. This is why I'm pretty sure it's seeing that Layers.indd "folder" as the only layer in the PDF. 
 
Is there some special way we're supposed to refer to the layers of a placed PDF which is different from other placed graphics? Ultimately, I need to be able to reference the PDF layers by name, because I can't be sure of their order.
 
Thanks for any help.
TOPICS
Scripting
1.0K
Translate
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

Advisor , Jan 19, 2022 Jan 19, 2022

Hello @Dan Byer,

 

In the case of a placed layered PDF exported from InDesign, all the layers are nested in one "layer group" that is "graphicLayers[0]"

 

If you have a single layer you need to reference by name try this..

try { app.selection[0].graphics[0].graphicLayerOptions.graphicLayers[0].graphicLayers.itemByName("Layer 2").currentVisibility = false;
} catch(e) {}

 

And if you have a multiple layers you need to reference by name try this..

myLayers = ["Layer 2", "Layer 3"]

for(var i = 0; i <myLaye
...
Translate
Community Expert ,
Jan 19, 2022 Jan 19, 2022

Hi @Dan Byer, this script will list to the console any graphic layers of the selected graphic. It will at least tell you if your pdf has layers that Indesign can see. I tested with a pdf saved from Illustrator and it worked fine. Have a look here for more info.

- Mark

 

function main() {

    // list layers of selected graphic
    var item = app.activeDocument.selection[0],
        layers;

    if (item.hasOwnProperty('graphics'))
        item = item.graphics[0];

    if (item.hasOwnProperty('graphicLayerOptions'))
        layers = item.graphicLayerOptions.graphicLayers;

    if (layers == undefined) {
        alert('Please select a graphic with layers and try again.')
        return;
    }

    for (var i = 0; i < layers.length; i++) {
        $.writeln(i + ': ' + layers[i].name + ' (visible = ' + layers[i].currentVisibility + ')');
    }

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'script');

 

Translate
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
Advisor ,
Jan 19, 2022 Jan 19, 2022

Hello @Dan Byer,

 

In the case of a placed layered PDF exported from InDesign, all the layers are nested in one "layer group" that is "graphicLayers[0]"

 

If you have a single layer you need to reference by name try this..

try { app.selection[0].graphics[0].graphicLayerOptions.graphicLayers[0].graphicLayers.itemByName("Layer 2").currentVisibility = false;
} catch(e) {}

 

And if you have a multiple layers you need to reference by name try this..

myLayers = ["Layer 2", "Layer 3"]

for(var i = 0; i <myLayers.length; i++){
var myLayerOverride = myLayers[i];
try { app.selection[0].graphics[0].graphicLayerOptions.graphicLayers[0].graphicLayers.itemByName(myLayerOverride).currentVisibility = false;
 } catch(e) {}
}

 

Regards,

Mike

Translate
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 Expert ,
Jan 19, 2022 Jan 19, 2022

Thanks Mike, I didn't know that about Indesign pdfs. 🙂

- Mark

Translate
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
Participant ,
Jan 20, 2022 Jan 20, 2022
LATEST

Yes, that's exactly what I was missing. It's a layer of a layer. Seems so obvious now! Thanks!

Translate
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