Adobe Illustrator Javascript and Unique Name Prefixes
Posted: Tue Jan 17, 2012 7:04 pm
I'm working on a new flow for certain design jobs that we're working to automate. A PDF or layered AI file is opened in Illustrator and a java script is run that saves out each of 7 layers as a separate pdf file. Because the file is already running in PowerSwitch, it already has a unique name prefix. Most (but oddly enough not all) of the new layers get a second unique name prefix. After being manipulated and arriving in their final respective folders, some files retain the original unique name prefix and some do not. Each of the final folders is set to strip the unique name and to overwrite duplicate files.
I'm thinking If my script could be adjusted so that I save only the original file name plus the layer name on each saved layer, I could eliminate that problem of the second unique name prefix.
I currently know very little scripting. I found this one online and was able to modify it enough to get it to use my output style and a preferred folder in the flow. Anyone want to take a stab at what I should change?
The script follows:
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var docName = baseName(name)
var pdfOptions = new PDFSaveOptions();
pdfOptions.pDFPreset = 'WilliamArthur';
// Turn all layers off
for (var i = 0; i < layers.length; i++) {
layers.visible = false;
}
// Turn each layer on
for (var i = 0; i < layers.length; i++) {
if (i == 0) {
layers.visible = true;
redraw();
var layerName = layers.name;
var saveAsPath = new File('Macintosh HD 2/POWERSWITCH DESIGN/DESIGN WORKFLOW/LAYERS/' + docName + '_' + layerName + '.pdf')
saveAs(saveAsPath, pdfOptions);
} else {
layers[i-1].visible = false;
layers.visible = true;
redraw();
var layerName = layers.name;
var saveAsPath = new File('Macintosh HD 2/POWERSWITCH DESIGN/DESIGN WORKFLOW/LAYERS/' + docName + '_' + layerName + '.pdf')
saveAs(saveAsPath, pdfOptions);
}
}
//close(SaveOptions.DONOTSAVECHANGES);
}
function baseName(fileName) {
var nameString = '';
var extOffset = fileName.lastIndexOf('.');
if (extOffset == -1) {
nameString = fileName;
} else {
nameString = fileName.substr(0, extOffset);
}
return nameString;
}
I'm thinking If my script could be adjusted so that I save only the original file name plus the layer name on each saved layer, I could eliminate that problem of the second unique name prefix.
I currently know very little scripting. I found this one online and was able to modify it enough to get it to use my output style and a preferred folder in the flow. Anyone want to take a stab at what I should change?
The script follows:
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
var docName = baseName(name)
var pdfOptions = new PDFSaveOptions();
pdfOptions.pDFPreset = 'WilliamArthur';
// Turn all layers off
for (var i = 0; i < layers.length; i++) {
layers.visible = false;
}
// Turn each layer on
for (var i = 0; i < layers.length; i++) {
if (i == 0) {
layers.visible = true;
redraw();
var layerName = layers.name;
var saveAsPath = new File('Macintosh HD 2/POWERSWITCH DESIGN/DESIGN WORKFLOW/LAYERS/' + docName + '_' + layerName + '.pdf')
saveAs(saveAsPath, pdfOptions);
} else {
layers[i-1].visible = false;
layers.visible = true;
redraw();
var layerName = layers.name;
var saveAsPath = new File('Macintosh HD 2/POWERSWITCH DESIGN/DESIGN WORKFLOW/LAYERS/' + docName + '_' + layerName + '.pdf')
saveAs(saveAsPath, pdfOptions);
}
}
//close(SaveOptions.DONOTSAVECHANGES);
}
function baseName(fileName) {
var nameString = '';
var extOffset = fileName.lastIndexOf('.');
if (extOffset == -1) {
nameString = fileName;
} else {
nameString = fileName.substr(0, extOffset);
}
return nameString;
}