Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Yes this all is possible via scripting!
Copy link to clipboard
Copied
which language would be best for creating it? im using a windows pc, so i guess javascript? or vb script?
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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';
Copy link to clipboard
Copied
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).
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
Copy link to clipboard
Copied
im trying to get the layers to print, but i get this error and the debug stops on the last line
var saveToLocation = doc.Path;
// Saves active file as.....
var jbOpts = new PrintJobOptions();
var opts = new PrintOptions();
opts.printPreset = "PP Print";
opts.jobOptions = jbOpts;
doc.print ('saveToLocation' + "/" + "_" + "S-M.pdf" , 'opts', 'jbOpts' ); // debug stops on this line
would it also be possible to turn on the option to print multiple pdf-s, not just one? ) select all sizes, or just 2/3 for example.
Copy link to clipboard
Copied
Your doc.print statement I believe is formatted incorrectly.
var jbOpts = new PrintJobOptions();
var opts = new PrintOptions();
opts.printPreset = "PP Print";
opts.jobOptions = jbOpts;
jbOpts.file = new File(saveToLocation);
doc.print(opts);
See if that corrects your first issue....
After that we can work on getting the ability to do multiple selections.
Copy link to clipboard
Copied
now i get a success alert, but instead of a pdf, it creates a tmp file in the folder. when i try to manually print with the selected print preset, it works.
Copy link to clipboard
Copied
Ok so what are you wanting it to do? Save a PDF file? and print?
Copy link to clipboard
Copied
I want the script to print the selected sizes to pdf-s with the size name+ suffix and prefix in the filename. it has to use the adobe pdf printer with a print preset. i do not want it to use saveAs to create the pdf-s.
Copy link to clipboard
Copied
Might be off topic but I'm curious why you prefer Adobe PDF printer and don't want to use save as function to create pdf.
Copy link to clipboard
Copied
pdf has to have settings, that are defined in the print preset, and in the adobe pdf printer settings.
Copy link to clipboard
Copied
So which settings that are present when using Adobe PDF printer are you missing in the PDF presets in Illustrator?
Copy link to clipboard
Copied
im not sure if this is the right way, but that's how my pc has been set up(not by me).
Right now the process goes: Print > use Print preset "x" > save pdf to selected folder. Print preset uses Adobe PDF as printer ( which in the printer properties has a custom preset), PPD is default Adobe PDF. i think you also cant choose color management in the adobe pdf preset, which is why i think i have to do it this way?
Copy link to clipboard
Copied
If possible, in my opinion, would be a better option if you could save multiple PDF files as opposed to printing to a PDF printer.
If you find out what the settings are maybe we could replicate them using the saveAs method.
If you have to go through that method of printing to PDF printer...
This should help...
var printOpts = new PrintOptions();
printOpts.printerName = "Adobe PDF";
// Change the name to your PPD
printOpts.PPDName = "Default (Adobe PDF)";
doc.print(printOpts);
Copy link to clipboard
Copied
i managed to get the pdf printer working by doing this:
var printJobOptions= new PrintJobOptions();
var options = new PrintOptions();
options.jobOptions = printJobOptions;
printJobOptions.copies=1;
var PP="PDF print";
options.printPreset=PP;
doc.print(options);
i copied this part from another script i have.
now the new problem is, that it does not automatically name the file and save it to docpath. it opens the save as window and asks for a name and folder.
Copy link to clipboard
Copied
Can you paste in the latest/updated version of the script so I can modify from that?
Copy link to clipboard
Copied
NB!: Only L-XL has been changed right now
#target illustrator
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 == "Laser Preview" || 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;
//Prints the files using print preset "PDF Print"
var printJobOptions= new PrintJobOptions();
var options = new PrintOptions();
options.jobOptions = printJobOptions;
printJobOptions.copies=1;
var PP="PDF print";
options.printPreset=PP;
doc.print(options,);
}
alert("Success!");
} else {
alert("You have selected " + check + " options.\nPlease try again and select 1 option.");
}
Copy link to clipboard
Copied
Ok...just wanted to make sure I wasn't missing anything else.... here try this...
It will save the PDF in the same folder as the file you are running the script on. I applied the same print options to each check box selection and then altered the file name to reflect what check box was selected.
#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;
}
}
var printJobOptions= new PrintJobOptions();
var options = new PrintOptions();
options.jobOptions = printJobOptions;
printJobOptions.file = new File(doc.path + "/" + "XS.pdf");
printJobOptions.copies=1;
var PP = "PDF print";
options.printPreset=PP;
doc.print(options);
}
// 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;
}
}
var printJobOptions= new PrintJobOptions();
var options = new PrintOptions();
options.jobOptions = printJobOptions;
printJobOptions.file = new File(doc.path + "/" + "S-M.pdf");
printJobOptions.copies=1;
var PP = "PDF print";
options.printPreset=PP;
doc.print(options);
}
// 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;
}
}
var printJobOptions= new PrintJobOptions();
var options = new PrintOptions();
options.jobOptions = printJobOptions;
printJobOptions.file = new File(doc.path + "/" + "L-XL.pdf");
printJobOptions.copies=1;
var PP = "PDF print";
options.printPreset=PP;
doc.print(options);
}
alert("Success!");
} else {
alert("You have selected " + check + " options.\nPlease try again and select 1 option.");
}
Copy link to clipboard
Copied
If you want the document name to be included in the new PDF name and simply add the size to the file name....
Change this line....
printJobOptions.file = new File(doc.path + "/" + "XS.pdf");
To this....
printJobOptions.file = new File(doc.path + "/" + doc.name.split(".")[0] + "_XS.pdf");
Copy link to clipboard
Copied
Script works fine so far. PDFSaveOptions need to be defined. What does it do if you check more than 1 box?
Copy link to clipboard
Copied
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!");
Find more inspiration, events, and resources on the new Adobe Community
Explore Now