Copy link to clipboard
Copied
Disclaimer: I can't write in javascript or any other language, I understand the concept of scripting but have no ability to actually write a script.
I am trying to automate my work with large numbers of files and Actions are no longer cutting it for me. Currently I use an action which is designed to help me save large batches of files for print. The action currently selects everything in my document, outlines all fonts, and saves a pdf with a specific preset for our RIP. Because we often use a flatbed i-cut machine, I also need to have layers in my document for outside cut paths, holes and inside cut paths, scoring paths, and registration marks. I need to be able to write an action which will turn of layers by name before saving pdfs, but currently Illustrator doesn't have a way to select layers by name through actions. Currently I have to save files with the layers either turned on or off and then manually save cutting paths and proof/print pdf.
My dream would be something that does this:
Select All
Create Outlines
If there are any Layers named "Outside Cut", "Holes-Inside Cut", "Score", or "regmarks" Save file as a pdf in a folder named "epson", and append filename with "_e"
Hide Layers named "Outside Cut", "Holes-Inside Cut", "Score"
Save file as a pdf in a folder named "print files"
Show Layers named "Outside Cut", "Holes-Inside Cut", "Score"
Delete any layer which isn't named "Outside Cut", "Holes-Inside Cut", "Score", or "regmarks"
Save file as a pdf in a folder named "icut", and append filename with "_icut"
Can anyone point me in the right direction of the tools I'll need to assemble this, or help me with writing it? I have a sample file which shows what I'm working with and looking for.
For a first impression here's a script doing the following:
- ask for a folder containing .ai files
- clean up the file of any hidden layers and objects
- save as PDF for Epson with extension _e.pdf
- hide 3 layers named Outside Cut, Holes-Inside Cut, Score
- save as PDF for printing with extension _print.pdf
- hide all layers
- make 3 layers named Outside Cut, Holes-Inside Cut, Score visible again
- save as PDF for i-cut with extension _icut.pdf
...#target Illustrator
app.userInteractionLevel = UserInteract
Copy link to clipboard
Copied
For a first impression here's a script doing the following:
- ask for a folder containing .ai files
- clean up the file of any hidden layers and objects
- save as PDF for Epson with extension _e.pdf
- hide 3 layers named Outside Cut, Holes-Inside Cut, Score
- save as PDF for printing with extension _print.pdf
- hide all layers
- make 3 layers named Outside Cut, Holes-Inside Cut, Score visible again
- save as PDF for i-cut with extension _icut.pdf
#target Illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var dir = Folder.selectDialog("Select folder containing .ai files for processing.");
var files = dir.getFiles("*.ai");
var pdfOption1 = 'PDFX3_CanDo_X3'; // <--- in here put the name of your PDF settings for EPSON
var pdfOption2 = 'PDFX3_CanDo_X3'; // <--- in here put the name of your PDF settings for printing
var pdfOption3 = 'PDFX3_CanDo_X3'; // <--- in here put the name of your PDF settings for ICUT
var pdfSuff1 = '_e.pdf'; // Suffix for EPSON PDF
var pdfSuff2 = '_print.pdf'; // Suffix print PDF
var pdfSuff3 = '_icut.pdf'; // Suffix ICUT PDF
function delete_hidden () {
var idoc = app.activeDocument;
for (i = idoc.pageItems.length-1; i >= 0; i--){
if (idoc.pageItems.hidden == true){
if (idoc.pageItems.name != typosafe) {
idoc.pageItems.remove()}
}
}
}
function delete_hidden_layers () {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
var subCount = currentLayer.layers.length;
for (var ss = subCount -1; ss >= 0; ss--){
var subLayer = currentLayer.layers[ss];
subLayer.locked = false;
if (subLayer.visible == false){
subLayer.visible = true;
if (subLayer.name != typosafe) {
subLayer.remove()}
else {
subLayer.visible = false};
}
}
if (currentLayer.visible == false){
currentLayer.visible = true;
if (currentLayer.name != typosafe) {
currentLayer.remove()}
else {
currentLayer.visible = false};
}
}
}
function hide_layer (hideme) {
var doc = app.activeDocument;
var myLayers = doc.layers;
try {
HideLayer = myLayers.getByName (hideme);
HideLayer.visible = false;
redraw();
}
catch (e) {}
}
function saveCopyAsPDF (setPDF, namePDF) {
var doc = app.activeDocument;
var options = new PDFSaveOptions ();
options.pDFPreset = setPDF;
options.viewAfterSaving = false;
var targetFile = null;
targetFile = app.activeDocument.fullName.toString().replace(".ai", namePDF);
doc.saveAs (new File (targetFile), options);
doc.close ();
}
function fontvect (){
while (doc.textFrames.length != 0) {
doc.textFrames[0].createOutline();
}
}
function hide_all_but_cut () {
var myDoc = app.activeDocument;
var layerCount = myDoc.layers.length;
var layerCount2 = myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
currentLayer.visible = false;
};
for (var i = layerCount2 - 1; i >= 0; i--) {
var currentLayer2 = myDoc.layers;
if (currentLayer2.name == "Outside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Holes-Inside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Score") {currentLayer2.visible = true};
else {currentLayer2.visible = false};
};
}
for (var f = 0; f < files.length; f++) {
var doc = app.open(files
); var original_file = doc.fullName;
delete_hidden_layers (); // delete all hidden layers
delete_hidden (); // delete all hidden objects
fontvect (); // outline fonts
if (!doc.saved) doc.save(); // base file cleaned up, now save it
splitemup (doc);
};
function splitemup (doc) {
var doc = app.activeDocument;
saveCopyAsPDF (pdfOption1, pdfSuff1); // save as PDF for EPSON
app.open (File (original_file)); // return to original file
hide_layer ("Outside Cut");
hide_layer ("Holes-Inside Cut");
hide_layer ("Score");
saveCopyAsPDF (pdfOption2, pdfSuff2); // save as PDF for Print
app.open (File (original_file)); // return to original file
hide_all_but_cut ();
saveCopyAsPDF (pdfOption3, pdfSuff3); // save as PDF for ICUT
};
All 3 new files are saved in the same folder where the original .ai files are located.
You will have to modify the lines 8, 9, and 10 and put in the exact names of your PDF settings.
Copy link to clipboard
Copied
This is almost perfect. I had to modify hide_all_but_icut to include my regmarks layer, but it is saving the icut file with all of the hidden layers still present. I need to delete those in order to create a file that meets our standards, but I don't understand how this code works well enough to figure out how to make it delete the hidden layers in the icut pdf.
I tried this
function splitemup (doc) {
var doc = app.activeDocument;
saveCopyAsPDF (pdfOption1, pdfSuff1); // save as PDF for EPSON
app.open (File (original_file)); // return to original file
hide_layer ("Outside Cut");
hide_layer ("Holes-Inside Cut");
hide_layer ("Score");
saveCopyAsPDF (pdfOption2, pdfSuff2); // save as PDF for Print
app.open (File (original_file)); // return to original file
hide_all_but_cut ();
delete_hidden_layers ();
saveCopyAsPDF (pdfOption3, pdfSuff3); // save as PDF for ICUT
};
but it gave me an error when working on the icut saying "Error 2: typosafe is undefined. Line: 46 -> if(currentLayer.name != typosafe) {"
Edit1: I tried troubleshooting this by removing delete_hidden_layers() from the splitemup function. I then added hidden layers and objects to the original file and ran the script. The delete_hidden_layers function throws an error if there is a hidden layer or object in the original document as well. This time it stopped running the script before it even saved the first file.
Edit2: In addition to defining my PDF presets, I put single quotes around all instances of typosafe ( != 'typosafe' ) in the delete hidden objects and layers functions, and that seems to have fixed it. I don't know if this was a mistake or I just got lucky that this works, but it now correctly deletes the hidden layer I put in the original file. I went ahead and added delete_hidden_layers back to the splitemup function and now this produces 3 PDFs exactly as specified. Thanks for the script q3player
Copy link to clipboard
Copied
#target Illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var dir = Folder.selectDialog("Select folder containing .ai files for processing.");
var files = dir.getFiles("*.ai");
var pdfOption1 = 'Print pdf'; // <--- in here put the name of your PDF settings for EPSON
var pdfOption2 = 'Print pdf'; // <--- in here put the name of your PDF settings for printing
var pdfOption3 = '[Illustrator Default]'; // <--- in here put the name of your PDF settings for ICUT
var pdfSuff1 = '_e.pdf'; // Suffix for EPSON PDF
var pdfSuff2 = '_print.pdf'; // Suffix print PDF
var pdfSuff3 = '_icut.pdf'; // Suffix ICUT PDF
function delete_hidden () {
var idoc = app.activeDocument;
for (i = idoc.pageItems.length-1; i >= 0; i--){
if (idoc.pageItems.hidden == true){
if (idoc.pageItems.name != 'typosafe') {
idoc.pageItems.remove()}
}
}
}
function delete_hidden_layers () {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
var subCount = currentLayer.layers.length;
for (var ss = subCount -1; ss >= 0; ss--){
var subLayer = currentLayer.layers[ss];
subLayer.locked = false;
if (subLayer.visible == false){
subLayer.visible = true;
if (subLayer.name != 'typosafe') {
subLayer.remove()}
else {
subLayer.visible = false};
}
}
if (currentLayer.visible == false){
currentLayer.visible = true;
if (currentLayer.name != 'typosafe') {
currentLayer.remove()}
else {
currentLayer.visible = false};
}
}
}
function hide_layer (hideme) {
var doc = app.activeDocument;
var myLayers = doc.layers;
try {
HideLayer = myLayers.getByName (hideme);
HideLayer.visible = false;
redraw();
}
catch (e) {}
}
function saveCopyAsPDF (setPDF, namePDF) {
var doc = app.activeDocument;
var options = new PDFSaveOptions ();
options.pDFPreset = setPDF;
options.viewAfterSaving = false;
var targetFile = null;
targetFile = app.activeDocument.fullName.toString().replace(".ai", namePDF);
doc.saveAs (new File (targetFile), options);
doc.close ();
}
function fontvect (){
while (doc.textFrames.length != 0) {
doc.textFrames[0].createOutline();
}
}
function hide_all_but_cut () {
var myDoc = app.activeDocument;
var layerCount = myDoc.layers.length;
var layerCount2 = myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
currentLayer.visible = false;
};
for (var i = layerCount2 - 1; i >= 0; i--) {
var currentLayer2 = myDoc.layers;
if (currentLayer2.name == "Outside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Holes-Inside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Score") {currentLayer2.visible = true};
else if (currentLayer2.name == "regmarks") {currentLayer2.visible = true};
else {currentLayer2.visible = false};
};
}
for (var f = 0; f < files.length; f++) {
var doc = app.open(files
); var original_file = doc.fullName;
delete_hidden_layers (); // delete all hidden layers
delete_hidden (); // delete all hidden objects
fontvect (); // outline fonts
if (!doc.saved) doc.save(); // base file cleaned up, now save it
splitemup (doc);
};
function splitemup (doc) {
var doc = app.activeDocument;
saveCopyAsPDF (pdfOption1, pdfSuff1); // save as PDF for EPSON
app.open (File (original_file)); // return to original file
hide_layer ("Outside Cut");
hide_layer ("Holes-Inside Cut");
hide_layer ("Score");
saveCopyAsPDF (pdfOption2, pdfSuff2); // save as PDF for Print
app.open (File (original_file)); // return to original file
hide_all_but_cut ();
delete_hidden_layers ();
saveCopyAsPDF (pdfOption3, pdfSuff3); // save as PDF for ICUT
};
Copy link to clipboard
Copied
Ok, happy to help.
I didn't know that the hidden layers needed to be deleted, but nice that you've managed that yourself. I forgot to mention the part "typosafe": In the past I have encountered problems when outlining fonts which had been modified in AI, e.g. double outline or stretched. So I told my colleagues to copy the layer with modified typo, name it "typosafe" and hide it. After that the modified typo can be rendered so that there are no effects or any modifications. If you don't need it, delete it.
Copy link to clipboard
Copied
Is there any way to A) make this not save epson/icut files if those layers aren't present and B) make the script place the files in a subfolder instead of the same directory. I'm researching it now, but if you know a way I can add that to the code it would be pretty much perfect for me.
It would need to be if regmarks layer doesnt exist then don't save icut and if Outside Cut/Holes-Inside Cut/Score don't exist then don't save epson.
The subfolders would be consistently named and always present in the folder (though I suppose it would be nice if it could create the folders if it doesn't find them). I don't quite understand how the saving is done in the script, whether I can specify a location and whether that location can be relative to the folder the file is in.
Copy link to clipboard
Copied
Current version of the code, in case it helps anyone. I wasn't able to make it check for folders and create them on the fly, but that's not an issue for me because all of our folders are duplicated from a template folder.
The code will delete hidden layers and objects in the original file, then check for certain layers and save a modified file to each specific folder based on whether or not the layers are present.
#target Illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var dir = Folder.selectDialog("Select folder containing .ai files for processing.");
var files = dir.getFiles("*.ai");
var pdfOption1 = 'Print pdf'; // <--- in here put the name of your PDF settings for EPSON
var pdfOption2 = 'Print pdf'; // <--- in here put the name of your PDF settings for printing
var pdfOption3 = '[Illustrator Default]'; // <--- in here put the name of your PDF settings for ICUT
var pdfFolder1 = '/epson';
var pdfFolder2 = '/print%20files';
var pdfFolder3 = '/icut';
var pdfSuff1 = '_e.pdf'; // Suffix for EPSON PDF
var pdfSuff2 = '_print.pdf'; // Suffix print PDF
var pdfSuff3 = '_icut.pdf'; // Suffix ICUT PDF
function doesLayerExist(layers, name) {
for (i=0; i<layers.length; i++) {
if (layers.name==name) return true;
}
return false;
}
function delete_hidden () {
var idoc = app.activeDocument;
for (i = idoc.pageItems.length-1; i >= 0; i--){
if (idoc.pageItems.hidden == true){
idoc.pageItems.remove()
}
}
}
function delete_hidden_layers () {
var myDoc=app.activeDocument;
var layerCount=myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
var subCount = currentLayer.layers.length;
for (var ss = subCount -1; ss >= 0; ss--){
var subLayer = currentLayer.layers[ss];
subLayer.locked = false;
if (subLayer.visible == false){
subLayer.visible = true;
if (subLayer.name != 'typosafe') {
subLayer.remove()}
else {
subLayer.visible = false};
}
}
if (currentLayer.visible == false){
currentLayer.visible = true;
if (currentLayer.name != 'typosafe') {
currentLayer.remove()}
else {
currentLayer.visible = false};
}
}
}
function hide_layer (hideme) {
var doc = app.activeDocument;
var myLayers = doc.layers;
try {
HideLayer = myLayers.getByName (hideme);
HideLayer.visible = false;
redraw();
}
catch (e) {}
}
function saveCopyAsPDF (setPDF, placePDF, namePDF) {
var doc = app.activeDocument;
var original_file = doc.fullName;
var home = original_file.parent; // here we get the full path to the directory containing your AI file
var export_folder = export_folder = home + placePDF; // there we want to create a subfolder
var options = new PDFSaveOptions ();
options.pDFPreset = setPDF;
options.viewAfterSaving = false;
var targetFile = null;
targetFile = app.activeDocument.name.toString().replace(".ai", namePDF);
//alert (export_folder + "/" + targetFile); // alert for testing save locations
doc.saveAs (new File (export_folder + "/" + targetFile), options); // new save code with export folder directory, a slash, and the appended filename
doc.close ();
}
function fontvect (){
while (doc.textFrames.length != 0) {
doc.textFrames[0].createOutline();
}
}
function hide_all_but_cut () {
var myDoc = app.activeDocument;
var layerCount = myDoc.layers.length;
var layerCount2 = myDoc.layers.length;
for (var ii = layerCount - 1; ii >= 0; ii--) {
var currentLayer = myDoc.layers[ii];
currentLayer.locked = false;
currentLayer.visible = false;
};
for (var i = layerCount2 - 1; i >= 0; i--) {
var currentLayer2 = myDoc.layers;
if (currentLayer2.name == "Outside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Holes-Inside Cut") {currentLayer2.visible = true};
else if (currentLayer2.name == "Score") {currentLayer2.visible = true};
else if (currentLayer2.name == "regmarks") {currentLayer2.visible = true};
else {currentLayer2.visible = false};
};
}
for (var f = 0; f < files.length; f++) {
var doc = app.open(files
); var original_file = doc.fullName;
delete_hidden_layers (); // delete all hidden layers
delete_hidden (); // delete all hidden objects
fontvect (); // outline fonts
if (!doc.saved) doc.save(); // base file cleaned up, now save it
splitemup (doc);
};
function splitemup (doc) {
var doc = app.activeDocument;
if (doesLayerExist(app.activeDocument.layers, "Outside Cut") || doesLayerExist(app.activeDocument.layers, "Holes-Inside Cut") || doesLayerExist(app.activeDocument.layers, "Score")){
saveCopyAsPDF (pdfOption1, pdfFolder1, pdfSuff1); // save as PDF for EPSON
app.open (File (original_file)); // return to original file
hide_layer ("Outside Cut");
hide_layer ("Holes-Inside Cut");
hide_layer ("Score");
}
saveCopyAsPDF (pdfOption2, pdfFolder2, pdfSuff2); // save as PDF for Print
app.open (File (original_file)); // return to original file
if (doesLayerExist(app.activeDocument.layers, "regmarks")){
hide_all_but_cut ();
delete_hidden_layers ();
saveCopyAsPDF (pdfOption3, pdfFolder3, pdfSuff3); // save as PDF for ICUT
} else {
activeDocument.close();
}
};