Skip to main content
martink15467522
Inspiring
November 9, 2018
Question

Script for printing out multiple PDFs with different layers

  • November 9, 2018
  • 5 replies
  • 5581 views

Hi

im not much familiar with illustrator scripting languages, but i thought maybe i could ask if this kind of procedure is even possible in scripting.

I have  ai files for different garments of clothing for example.

in the file there are the following layers>

Info

outline XS

outline S-M

outline L-XL

design XS

design S-M

design L-XL

the layer names stay the same in all files

would it be possible for a script to print the layers in the following way:

1) Prompt the user which sizes he wants to print ( tick a box for example?) + ask to add a prefix for the file.

2) use print preset "x"

3) 1st pdf: only layers: info, outline xs, design xs, named prefix--xs

     2nd pdf only layers: info, outline s-m, design s-m, named prefix--s-m

     3rd pdf: only layers: info outline l-xl, design l-xl, named prefix--l-xl

pdf-s can be placed in the same folder as the source file

is this possible, if not, could this be done via a plugin?

This topic has been closed for replies.

5 replies

subieguy2
Inspiring
November 13, 2018

Ok so I spent a little time testing this. The PDF files that it is creating are causing issues where the files are corrupted. So here is an updated script. This is using the saveAs method. I have also made it so you can check multiple boxes and get multiple files.

I have put in comments to try to explain the script. I would work off of this. Post back if you come up with your requirements.

#target illustrator-23

// Make a dialog with the header name "Select the size..."

var selectSizeWindow = new Window('dialog', "Select the size...");

// Create a group for the UI

var checkBoxGroup = selectSizeWindow.add("group");

checkBoxGroup.orientation = "row";

/*   CHECKBOX NOTES   */

// The checkbox is assigned to a variable to store the value to

// \u00A0 adds space between the checkbox and the label

// Add a checkbox to our check box group... XS

var isXS = checkBoxGroup.add('checkbox', undefined, "\u00A0XS");

// Add a checkbox to our check box group... S - M

var isSM = checkBoxGroup.add('checkbox', undefined, "\u00A0S - M");

// Add a checkbox to our check box group... L - XL

var isLXL = checkBoxGroup.add('checkbox', undefined, "\u00A0L - XL");

// Create a group for the UI

var buttons = selectSizeWindow.add("group")

// Create a button... OK

buttons.add("button", undefined, "OK", {name: "ok"});

// Create a button... CANCEL

buttons.add("button", undefined, "Cancel", {name: "cancel"});

// Display our UI

selectSizeWindow.show();

// Get the active document

var doc = app.activeDocument;

// Get the active document layers

var allLayers = doc.layers;

// Check box for XS selected...

if (isXS.value == true) {

    // Look through the layers...

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

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline XS" || allLayers.name == "design XS") {

            // Layer related to XS

            allLayers.visible = true;

        } else {

            // Not a layer related to XS

            allLayers.visible = false;

        }

    }

    // Store the original file location to reopen

    var originalFile = doc.fullName;

    // The new file name and location to save it to

    var saveLocation = new File(doc.path + "/" + doc.name.split(".")[0] + "_XS.pdf");

    // Variable to store all PDFSaveOptions to

    var saveOpts = new PDFSaveOptions();

    // A saved PDF preset name

    saveOpts.pDFPreset = "Machine Views"

    // Save the new PDF file to the set location with the set options

    doc.saveAs(saveLocation, saveOpts);

    // Close that new file created in the saveAs step

    doc.close();

    // Open the original file

    app.open(originalFile);

}

// Get the active document

var doc = app.activeDocument;

// Get the active document layers

var allLayers = doc.layers;

// Check box for S - M selected...

if (isSM.value == true) {

    // Look through the layers...

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

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline S-M" || allLayers.name == "design S-M") {

            // Layer related to S-M

            allLayers.visible = true;

        } else {

            // Not a layer related to S-M

            allLayers.visible = false;

        }

    }

    // Store the original file location to reopen

    var originalFile = doc.fullName;

    // The new file name and location to save it to

    var saveLocation = new File(doc.path + "/" + doc.name.split(".")[0] + "_S-M.pdf");

    // Variable to store all PDFSaveOptions to

    var saveOpts = new PDFSaveOptions();

    // A saved PDF preset name

    saveOpts.pDFPreset = "Machine Views"

    // Save the new PDF file to the set location with the set options

    doc.saveAs(saveLocation, saveOpts);

    // Close that new file created in the saveAs step

    doc.close();

    // Open the original file

    app.open(originalFile);

}

// Get the active document

var doc = app.activeDocument;

// Get the active document layers

var allLayers = doc.layers;

// Check box for L - XL selected...

if (isLXL.value == true) {

    // Look through the layers...

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

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline L-XL" || allLayers.name == "design L-XL") {

            // Layer related to L-XL

            allLayers.visible = true;

        } else {

            // Not a layer related to L-XL

            allLayers.visible = false;

        }

    }

    // Store the original file location to reopen

    var originalFile = doc.fullName;

    // The new file name and location to save it to

    var saveLocation = new File(doc.path + "/" + doc.name.split(".")[0] + "_L-XL.pdf");

    // Variable to store all PDFSaveOptions to

    var saveOpts = new PDFSaveOptions();

    // A saved PDF preset name

    saveOpts.pDFPreset = "Machine Views"

    // Save the new PDF file to the set location with the set options

    doc.saveAs(saveLocation, saveOpts);

    // Close that new file created in the saveAs step

    doc.close();

    // Open the original file

    app.open(originalFile);

}

alert("Success!");

martink15467522
Inspiring
November 14, 2018

selecting multiple sizes now works

I still need to get the pdf-s printed

when i try to use the new File command in print job options, it gives me the correct file name, but corrupts the pdf.

when i use this, it corrupts the pdf

var printJobOptions= new PrintJobOptions();

       var options = new PrintOptions();

       options.jobOptions = printJobOptions;

        printJobOptions.file= new File(doc.path + "/" + doc.name.split(".")[0] + "_L-XL.pdf");

         

        var PP="PDF print";

        options.printPreset=PP;

       

        doc.print(options);

when i remove the new File joboptions, i get the prompt to name the file myself, and then the pdf is not corrupted. So i guess the problem is in the new filename line?

subieguy2
Inspiring
November 14, 2018

As I mentioned...yes it will give you a corrupted PDF file. The file being created is just a file. Not an actual native PDF.

I typically use this function when writing things like a log file that ends up being a .TXT.

There should be a way we can save your PDF using the same details as what you are trying to accomplish with the print method.

Can you screen shot your preset settings?

Inspiring
November 12, 2018

Script works fine so far. PDFSaveOptions need to be defined. What does it do if you check more than 1 box?

subieguy2
Inspiring
November 12, 2018

Here is something I wrote.... I am still learning myself so it might not be super efficient or 100%, but it should give you a good jump on the code. Hope it helps. Anyone else feel free to chime in if my code is wrong or code be improved.

var doc = app.activeDocument;

var allLayers = doc.layers;

// Panel for size selection

var selectSizeWindow = new Window('dialog', "Select the size...");

var checkBoxGroup = selectSizeWindow.add("group");

checkBoxGroup.orientation = "row";

var isXS = checkBoxGroup.add('checkbox', undefined, "\u00A0XS");

var isSM = checkBoxGroup.add('checkbox', undefined, "\u00A0S - M");

var isLXL = checkBoxGroup.add('checkbox', undefined, "\u00A0L - XL");

var buttons = selectSizeWindow.add("group")

buttons.add("button", undefined, "OK", {name: "ok"});

buttons.add("button", undefined, "Cancel", {name: "cancel"});

selectSizeWindow.show();

// Check box for XS selected...

if (isXS.value == true) {

    // Look through the layers...

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

        alert(allLayers.name);

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline XS" || allLayers.name == "design XS") {

            // Layer related to XS

            allLayers.visible = true;

        } else {

            // Not a layer related to XS

            allLayers.visible = false;

        }

    }

    // Gets the path of the current file

    var saveToLocation = doc.path;

    // Saves active file as.....

    var saveOpts = new PDFSaveOptions();

    doc.saveAs(saveToLocation + "/" + "XS.pdf", saveOpts);

}

// Check box for S - M selected...

if (isSM.value == true) {

    // Look through the layers...

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

        alert(allLayers.name);

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline S-M" || allLayers.name == "design S-M") {

            // Layer related to S-M

            allLayers.visible = true;

        } else {

            // Not a layer related to S-M

            allLayers.visible = false;

        }

    }

    // Gets the path of the current file

    var saveToLocation = doc.path;

    // Saves active file as.....

    var saveOpts = new PDFSaveOptions();

    doc.saveAs(saveToLocation + "/" + "S-M.pdf", saveOpts);

}

// Check box for L - XL selected...

if (isLXL.value == true) {

    // Look through the layers...

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

        alert(allLayers.name);

        // Layers to turn on

        if (allLayers.name == "info" || allLayers.name == "outline L-XL" || allLayers.name == "design L-XL") {

            // Layer related to L-XL

            allLayers.visible = true;

        } else {

            // Not a layer related to L-XL

            allLayers.visible = false;

        }

    }

    // Gets the path of the current file

    var saveToLocation = doc.path;

    // Saves active file as.....

    var saveOpts = new PDFSaveOptions();

    doc.saveAs(saveToLocation + "/" + "L-XL.pdf", saveOpts);

}

martink15467522
Inspiring
November 12, 2018

when i try to run the script in debug mode, it just pops up a script alert with ok button for every layer that is in the file

and gives me an error on the line:

  doc.saveAs(saveToLocation + '/' + 'XS.pdf', saveOpts); and gives me an error 1242 illegal argument, file/folder excpected.

i tried to set pdfoptions like this:

var pdfOptions = new PDFSaveOptions();

pdfOptions.pDFPreset = '[High Quality Print]';

is this correct? although i would need to use a Print preset ( the one that you can select when printing to pdf, not save as)

so maybe something like this ?

var printJoboptions = new printJoboptions();

printJoboptions.printPreset = 'PP Print';

subieguy2
Inspiring
November 12, 2018

q3player

In the script before it allows you to select multiples. I have updated the code to check and make sure only 1 options is selected... (see below).

martink15467522

If you are running this from ESTK I believe you are getting this error because there is no target to Illustrator. I have updated the script to target it (please make sure your version matches as I am targeting the latest release of CC). I also added in a line that you can modify to use your preset. So save a preset with your desired options. Then change the name of the preset as indicated in my script.

Hope this helps!

#target illustrator-23

var doc = app.activeDocument;

var allLayers = doc.layers;

var check = 0;

// Panel for size selection

var selectSizeWindow = new Window('dialog', "Select the size...");

var checkBoxGroup = selectSizeWindow.add("group");

checkBoxGroup.orientation = "row";

var isXS = checkBoxGroup.add('checkbox', undefined, "\u00A0XS");

var isSM = checkBoxGroup.add('checkbox', undefined, "\u00A0S - M");

var isLXL = checkBoxGroup.add('checkbox', undefined, "\u00A0L - XL");

var buttons = selectSizeWindow.add("group")

buttons.add("button", undefined, "OK", {

    name: "ok"

});

buttons.add("button", undefined, "Cancel", {

    name: "cancel"

});

selectSizeWindow.show();

// Make sure only 1 box is checked...

if (isXS.value == true) {

    check++;

}

if (isSM.value == true) {

    check++;

}

if (isLXL.value == true) {

    check++;

}

if (check == 1) {

    // Check box for XS selected...

    if (isXS.value == true) {

        // Look through the layers...

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

            // Layers to turn on

            if (allLayers.name == "info" || allLayers.name == "outline XS" || allLayers.name == "design XS") {

                // Layer related to XS

                allLayers.visible = true;

            } else {

                // Not a layer related to XS

                allLayers.visible = false;

            }

        }

        // Gets the path of the current file

        var saveToLocation = doc.path;

        // Saves active file as.....

        var saveOpts = new PDFSaveOptions();

        // Put the name of your preset in the quotes...

        saveOpts.pDFPreset("Name of preset");

        doc.saveAs(saveToLocation + "/" + "XS.pdf", saveOpts);

    }

    // Check box for S - M selected...

    if (isSM.value == true) {

        // Look through the layers...

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

            // Layers to turn on

            if (allLayers.name == "info" || allLayers.name == "outline S-M" || allLayers.name == "design S-M") {

                // Layer related to S-M

                allLayers.visible = true;

            } else {

                // Not a layer related to S-M

                allLayers.visible = false;

            }

        }

        // Gets the path of the current file

        var saveToLocation = doc.path;

        // Saves active file as.....

        var saveOpts = new PDFSaveOptions();

        // Put the name of your preset in the quotes...

        saveOpts.pDFPreset("Name of preset");

        doc.saveAs(saveToLocation + "/" + "S-M.pdf", saveOpts);

    }

    // Check box for L - XL selected...

    if (isLXL.value == true) {

        // Look through the layers...

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

            // Layers to turn on

            if (allLayers.name == "info" || allLayers.name == "outline L-XL" || allLayers.name == "design L-XL") {

                // Layer related to L-XL

                allLayers.visible = true;

            } else {

                // Not a layer related to L-XL

                allLayers.visible = false;

            }

        }

        // Gets the path of the current file

        var saveToLocation = doc.path;

        // Saves active file as.....

        var saveOpts = new PDFSaveOptions();

        // Put the name of your preset in the quotes...

        saveOpts.pDFPreset("Name of preset");

        doc.saveAs(saveToLocation + "/" + "L-XL.pdf", saveOpts);

    }

alert("Success!");

} else {

    alert("You have selected " + check + " options.\nPlease try again and select 1 option.");

}

edited to remove alerts, fix check error count, add alert for success

Inspiring
November 12, 2018

As a first step I would start by hiding all layers except "info" which shall be seen in every version. This file needs to be saved and is now your base file for all following operations.

After that you start by showing "outline" and "design" layers for the first size, then export as PDF with the given name, and go back to the base file. That's because in scripting you can't do a "save as copy ..." and saving your actual file as PDF will give you the PDF as active document, not the AI file.

These steps have to be repeated for all sizes and that's all.

If you could do this in javascript you could go on and put a UI in the script.

Silly-V
Legend
November 9, 2018

Yes this all is possible via scripting!

martink15467522
Inspiring
November 9, 2018

which language would be best for creating it? im using a windows pc, so i guess javascript? or vb script?

Silly-V
Legend
November 10, 2018

It's easiest to use Javascript if you can, and to get started, download the ExtendScript Toolkit application - it's Adobe javascript text editor which you can use to run your code and observe the data as it runs using its debug mode.

When you download it, you can use this following sets of snippets to get started:

#target illustrator

function test(){

     var doc = app.activeDocument;

     var layers = doc.layers;

     alert(doc.name + " has " + layers.length + " layers in it");

};

test();

Paste the above snippet into the editor and use "Debug > Run" menu item to run the script, or press the green triangular 'play' button at the top of the text editor's frame.

If you have a current document open, it shall not produce any errors. Once you have ran it to observe the alert box, you can click the area to the left of the line which contains the text "var layers = doc.layers". This should produce a red circle, setting a breakpoint. Next time you run, it will pause where the breakpoint is. Open the Window > Data Browser panel and you click Debug > Step In (or Debug > Step Over) and then you can see the 'layers' variable get instantiated with the list of methods and properties appearing.

This way you can see any commands for various objects as well as the current state of your execution - from this you can accomplish most things that you wish via scripting!