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

Script to prevent exporting when a layer is present

Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Hi, I want to create a script that will prevent a user from exporting any pdf when unwanted Layers are present. eg: Layer1, Layer Copy

TOPICS
How to , Print , Scripting

Views

1.5K

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 2 Correct answers

Advocate , May 10, 2021 May 10, 2021

Try this below snippet:

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    // Here this is your list of layer names to be matched === If matches code will stop export of pdf
    var listOfLayerNames = ["Layer", "Layer Copy", "Test Layer"];
    var match = false;
    for(var i = 0; i < app.activeDocument.layers.length; i++){
        for(var j = 0; j < listOfLaye
...

Votes

Translate

Translate
Advocate , May 13, 2021 May 13, 2021

Just one more validation added. Now you check & see:

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    var listOfLayerNames = ["N-Spacing", "Specification Box", "Dieline", "FPO/Variable Area", "ARTWORK", "Pre-Printed Artwork", "Substrate - for foil/clear plastic", "Technical Details"];
    var match = "";
    var outOfList = "";
    for(var i = 0; i < app.acti
...

Votes

Translate

Translate
Advocate ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

You just want to match names of layer to some specific texts & if it matches you want to stop them from exporting?

 

Sunil

Votes

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
Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Well, is it possible that if a layer doesnt match to a list of my specific layers, then it will stop me from exporting.

Votes

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
Advocate ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

You need to add event listner.

So that it should be invoked before exporting PDF & that script should check layer names & if your requirement doesn't fullfills it should prevent the default action.

 

Sunil

Votes

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
Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Thank you, do you have any samples of that script? Im still a newb 

Votes

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
Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

I found this script, I just dont know how to change it as an export function, and where to put the Layer finder.
 
#target Indesign
#targetengine "closeConfirmation"
app.addEventListener("beforeClose", confirmClose);

function confirmClose(e) {

if(e.parent.constructor.name !== "LayoutWindow") return;

if (confirm ("Did you proof your page?") === false) {
e.stopPropagation();
e.preventDefault();
}
}

Votes

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
Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

This is the script Ive done so far, unfortunately, it displays all the present Layers on the document.

 

#target indesign

#targetengine "session"

main();

function main(){

var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);


function checkLayer(myEvent){

alert("Unwanted Layers is present:"+"\r"+app.activeDocument.layers.everyItem().name.join("\r"));

}

}

Votes

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
Advocate ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Try this below snippet:

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    // Here this is your list of layer names to be matched === If matches code will stop export of pdf
    var listOfLayerNames = ["Layer", "Layer Copy", "Test Layer"];
    var match = false;
    for(var i = 0; i < app.activeDocument.layers.length; i++){
        for(var j = 0; j < listOfLayerNames.length; j++){
            if(listOfLayerNames[j] == app.activeDocument.layers[i].name){
                match = true;
                break;
                }
            }
        if(match) break;
        }
    if(match){
        e.stopPropagation();
        e.preventDefault();
        }
    }

Best

Sunil

 

Votes

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
Explorer ,
May 11, 2021 May 11, 2021

Copy link to clipboard

Copied

This is perfect! thank you so much. How can I create an alert showing all the unwanted layers?

Votes

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
Explorer ,
May 11, 2021 May 11, 2021

Copy link to clipboard

Copied

Plus, how can I activate the script without actually clicking it? Thanks in advance

Votes

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
Explorer ,
May 11, 2021 May 11, 2021

Copy link to clipboard

Copied

I modified the script youve sent,"if it matches the list of layers" then it will continue exporting. The only thing I need now is it will display all the unwanted layers that doesnt matched the list and the script will automatically run without manually activating it.

 

#target indesign
#targetengine "session"
main();
function main(){
var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
}
function checkLayer(e){
// Here this is your list of layer names to be matched === If matches code will stop export of pdf
var listOfLayerNames = ["N-Spacing", "Specification Box", "Dieline", "FPO/Variable Area", "ARTWORK", "Pre-Printed Artwork", "Substrate - for foil/clear plastic", "Technical Details"];
var match = true;
for(var i = 0; i < app.activeDocument.layers.length; i++){
for(var j = 0; j < listOfLayerNames.length; j++){
if(listOfLayerNames[j] == app.activeDocument.layers[i].name){
match = false;
break;
}
}
if(match) break;
}
if(match){
var r = confirm("Unwanted Layers is present:"+"\r"+app.activeDocument.layers.everyItem().name.join("\r"));
if (r == true)
{
}
else {
e.stopPropagation();
e.preventDefault();
}
}
}

Votes

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
Advocate ,
May 11, 2021 May 11, 2021

Copy link to clipboard

Copied

You can put this script in start up folder instead of script panel. So every time your application restarts this script will be executed.

 

Best

Sunil

Votes

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
Advocate ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

Try this code sample :

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    // Here this is your list of layer names to be matched === If matches code will stop export of pdf
    var listOfLayerNames = ["N-Spacing", "Specification Box", "Dieline", "FPO/Variable Area", "ARTWORK", "Pre-Printed Artwork", "Substrate - for foil/clear plastic", "Technical Details"];
    var match = "";
    for(var i = 0; i < app.activeDocument.layers.length; i++){
        for(var j = 0; j < listOfLayerNames.length; j++){
            if(listOfLayerNames[j] == app.activeDocument.layers[i].name){
                match += app.activeDocument.layers[i].name+"\n";
                }
            }
        }
    if(match != ""){
        if (!confirm("Unwanted Layers is present:"+"\r"+match)){
            e.stopPropagation();
            e.preventDefault();
            }
        }
    }

Best

Sunil

Votes

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
Explorer ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

Hi @Sunil Yadav I tried the code youve sent, It shows the matched layers(listOfLayerNames) on the dialog box. Is there a way that it will show whats not on the list? Thank you so much 

Votes

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
Advocate ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

Sure,

Try this snippet :

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    var listOfLayerNames = ["N-Spacing", "Specification Box", "Dieline", "FPO/Variable Area", "ARTWORK", "Pre-Printed Artwork", "Substrate - for foil/clear plastic", "Technical Details"];
    var match = "";
    var outOfList = "";
    for(var i = 0; i < app.activeDocument.layers.length; i++){
        for(var j = 0; j < listOfLayerNames.length; j++){
            if(listOfLayerNames[j] == app.activeDocument.layers[i].name) match += app.activeDocument.layers[i].name+"\n";
            else outOfList += app.activeDocument.layers[i].name+"\n";
            }
        }
    if(match != ""){
        if (!confirm("Unwanted Layers is present:"+"\r"+outOfList)){
            e.stopPropagation();
            e.preventDefault();
            }
        }
    }

Best

Sunil

Votes

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
Explorer ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

Hello @Sunil Yadav  im having this error. The higlighted layers are those whose not on the list, but when I scrolled down the dialg box, it still shows all the layer names

Screen Shot 2021-05-13 at 10.05.55 AM.pngScreen Shot 2021-05-13 at 10.06.10 AM.png

Votes

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
Advocate ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

Just one more validation added. Now you check & see:

#target indesign
#targetengine "session"
main();
function main(){
    var myBeforeExportEvList = app.addEventListener("beforeExport", checkLayer, false);
    }
function checkLayer(e){
    var listOfLayerNames = ["N-Spacing", "Specification Box", "Dieline", "FPO/Variable Area", "ARTWORK", "Pre-Printed Artwork", "Substrate - for foil/clear plastic", "Technical Details"];
    var match = "";
    var outOfList = "";
    for(var i = 0; i < app.activeDocument.layers.length; i++){
        var found = false;
        for(var j = 0; j < listOfLayerNames.length; j++){
            if(listOfLayerNames[j] == app.activeDocument.layers[i].name){
                match += app.activeDocument.layers[i].name+"\n";
                found = true;
                break;
                }
            }
        if(!found) outOfList += app.activeDocument.layers[i].name+"\n";
        }
    if(match != ""){
        if (!confirm("Unwanted Layers is present:"+"\r"+outOfList)){
            e.stopPropagation();
            e.preventDefault();
            }
        }
    }

Best

Sunil

Votes

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
Explorer ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

LATEST

This is perfect! thank you so much!

Votes

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