Here is an updated version.
//@target illustrator
//@targetengine "main"
function SaveOutputWithNames () {
if (!Object.entries) {
Object.entries = function (obj) {
var ownProps = Object.keys(obj), i = ownProps.length, resArray = new Array(i);
while (i--)
resArray[i] = [ownProps[i], obj[ownProps[i]]];
return resArray;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function (search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
if (!Array.prototype.includes) {
//or use Object.defineProperty
Array.prototype.includes = function (search) {
return !!~this.indexOf(search);
}
}
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
};
if (!Array.prototype.filter) {
Array.prototype.filter = function (func, thisArg) {
'use strict';
if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
throw new TypeError();
var len = this.length >>> 0,
res = new Array(len), // preallocate array
t = this, c = 0, i = -1;
var kValue;
if (thisArg === undefined) {
while (++i !== len) {
// checks to see if the key was set
if (i in this) {
kValue = t[i]; // in case t is changed in callback
if (func(t[i], i, t)) {
res[c++] = kValue;
}
}
}
} else {
while (++i !== len) {
// checks to see if the key was set
if (i in this) {
kValue = t[i];
if (func.call(thisArg, t[i], i, t)) {
res[c++] = kValue;
}
}
}
}
res.length = c; // shrink down array to proper size
return res;
};
}
// Production steps of ECMA-262, Edition 5, 15.4.4.21
// Reference: https://es5.github.io/#x15.4.4.21
// https://tc39.github.io/ecma262/#sec-array.prototype.reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function (callback /*, initialValue*/) {
if (this === null) {
throw new TypeError( 'Array.prototype.reduce ' +
'called on null or undefined' );
}
if (typeof callback !== 'function') {
throw new TypeError( callback +
' is not a function');
}
// 1. Let O be ? ToObject(this value).
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// Steps 3, 4, 5, 6, 7
var k = 0;
var value;
if (arguments.length >= 2) {
value = arguments[1];
} else {
while (k < len && !(k in o)) {
k++;
}
// 3. If len is 0 and initialValue is not present,
// throw a TypeError exception.
if (k >= len) {
throw new TypeError( 'Reduce of empty array ' +
'with no initial value' );
}
value = o[k++];
}
// 8. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kPresent be ? HasProperty(O, Pk).
// c. If kPresent is true, then
// i. Let kValue be ? Get(O, Pk).
// ii. Let accumulator be ? Call(
// callbackfn, undefined,
// « accumulator, kValue, k, O »).
if (k in o) {
value = callback(value, o[k], k, o);
}
// d. Increase k by 1.
k++;
}
// 9. Return accumulator.
return value;
}
}
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Array.prototype.find = function (predicate) {
if (this == null) {
throw TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (typeof predicate !== 'function') {
throw TypeError('predicate must be a function');
}
var thisArg = arguments[1];
var k = 0;
while (k < len) {
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
k++;
}
return undefined;
}
}
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty, hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
], dontEnumsLength = dontEnums.length;
return function (obj) {
var wasNull = obj === null;
var errorMessageTypeReadout = (wasNull) ? "input was null" : "input was " + typeof obj;
if (typeof obj !== 'object' && typeof obj !== 'function' || wasNull)
throw new TypeError("Object.keys called on non-object (" + errorMessageTypeReadout + ").");
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop))
result.push(prop);
}
if (hasDontEnumBug) {
for (var i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i]))
result.push(dontEnums[i]);
}
}
return result;
};
})();
}
// ------------------------------------------------------------------------------------------------------------------------ //
// ------------------------------------------------------------------------------------------------------------------------ //
var _a, _b, _c, _d, _e, _f, _g;
var settingsPath = "~/Desktop/SaveOutputWithNames_SETTINGS.txt";
var settingsFile = File(settingsPath);
var AppLayerNames;
(function (AppLayerNames) {
AppLayerNames["REGMARK"] = "Regmark";
AppLayerNames["DIMENSIONS"] = "Dimensions";
AppLayerNames["BOUNDING"] = "Bounding";
AppLayerNames["DIELINE"] = "Dieline";
AppLayerNames["ARTWORK"] = "Artwork";
})(AppLayerNames || (AppLayerNames = {}));
var InstructionTypes;
(function (InstructionTypes) {
InstructionTypes["WORKING"] = "_WORKING";
InstructionTypes["PRINT"] = "_PRINT";
InstructionTypes["DIE"] = "_DIE";
})(InstructionTypes || (InstructionTypes = {}));
var getPDFOptions = function (preset) {
var result = new PDFSaveOptions();
var allPresets = app.PDFPresetsList;
if (allPresets.includes(preset)) {
result.pDFPreset = preset;
}
else {
alert("Could not locate the preset '" + preset + "' in the application preset list:\n" + allPresets.join("\n") + "\n\nUsing " + allPresets[0]);
result.pDFPreset = allPresets[0];
}
return result;
};
var InstructionFormats = (_a = {},
_a[InstructionTypes.WORKING] = {
format: ".ai",
saveOptions: function () { return new IllustratorSaveOptions(); },
pdfPreset: null
},
_a[InstructionTypes.PRINT] = {
format: ".pdf",
saveOptions: getPDFOptions,
pdfPreset: "[High Quality Print]"
},
_a[InstructionTypes.DIE] = {
format: ".pdf",
saveOptions: getPDFOptions,
pdfPreset: "[Smallest File Size]"
},
_a);
if (settingsFile.exists) {
settingsFile.open("r");
var contents = settingsFile.read();
settingsFile.close();
var contentsSplit = contents.split(",");
var instructionFormatKeys = [InstructionTypes.PRINT, InstructionTypes.DIE];
if (contentsSplit.length != instructionFormatKeys.length) {
return;
}
var counter = 0;
var validEntries = Object.entries(InstructionFormats).filter(function (m) { return m[1].pdfPreset != null; });
for (var _i = 0, validEntries_1 = validEntries; _i < validEntries_1.length; _i++) {
var _h = validEntries_1[_i], key = _h[0], instructionFormat = _h[1];
var preset = contentsSplit[counter++];
if (app.PDFPresetsList.includes(preset)) {
instructionFormat.pdfPreset = preset;
}
}
}
var Instructions = (_b = {},
_b[AppLayerNames.REGMARK] = (_c = {},
_c[InstructionTypes.WORKING] = { "delete": false, visible: true },
_c[InstructionTypes.PRINT] = { "delete": false, visible: true },
_c[InstructionTypes.DIE] = { "delete": false, visible: true },
_c),
_b[AppLayerNames.DIMENSIONS] = (_d = {},
_d[InstructionTypes.WORKING] = { "delete": false, visible: true },
_d[InstructionTypes.PRINT] = { "delete": false, visible: false },
_d[InstructionTypes.DIE] = { "delete": true, visible: true },
_d),
_b[AppLayerNames.BOUNDING] = (_e = {},
_e[InstructionTypes.WORKING] = { "delete": false, visible: true },
_e[InstructionTypes.PRINT] = { "delete": false, visible: true },
_e[InstructionTypes.DIE] = { "delete": true, visible: true },
_e),
_b[AppLayerNames.DIELINE] = (_f = {},
_f[InstructionTypes.WORKING] = { "delete": false, visible: true },
_f[InstructionTypes.PRINT] = { "delete": false, visible: false },
_f[InstructionTypes.DIE] = { "delete": true, visible: true },
_f),
_b[AppLayerNames.ARTWORK] = (_g = {},
_g[InstructionTypes.WORKING] = { "delete": false, visible: true },
_g[InstructionTypes.PRINT] = { "delete": false, visible: true },
_g[InstructionTypes.DIE] = { "delete": false, visible: true },
_g),
_b);
var step, doc;
var appLayers = {};
try {
step = "Open-document check.";
doc = app.activeDocument;
step = "Template Layers Check.";
for (var all in AppLayerNames) {
step = "Template Layers Check: '" + AppLayerNames[all] + "'.";
appLayers[all] = doc.layers.getByName(AppLayerNames[all]);
}
}
catch (error) {
alert("There was an error during the '" + step + "' step:\n" + error.message);
return;
}
var w = new Window("dialog", "Save Files");
var g1 = w.add('group');
var btn_chooseDest = g1.add("button", undefined, "Destination");
var e_destFile = g1.add("edittext", undefined, "");
e_destFile.characters = 30;
var g2 = w.add("panel", undefined, "Output PDF Presets");
g2.orientation = "column";
var outputPDFPresets = {};
for (var all in InstructionFormats) {
var instructionType = InstructionFormats[all];
if (instructionType.format == ".ai") {
continue;
}
var newGroup = g2.add("group");
var newLabel = newGroup.add("statictext", undefined, all.replace(/^_/, ""));
newLabel.characters = 10;
var newDropdown = newGroup.add("dropdownlist", undefined, app.PDFPresetsList);
newDropdown.selection = newDropdown.find(instructionType.pdfPreset);
outputPDFPresets[all] = newDropdown;
}
var g_btn = w.add('group');
var btn_ok = g_btn.add("button", undefined, "Ok");
var btn_ccl = g_btn.add("button", undefined, "Cancel");
w.onShow = function () {
btn_chooseDest.onClick = function () {
var chosenDest = app.activeDocument.fullName.saveDlg("Choose where to save the file.");
if (chosenDest) {
e_destFile.text = decodeURI(chosenDest.fsName);
e_destFile.notify("onChange");
}
};
e_destFile.onChange = function () {
this.helpTip = this.text;
};
e_destFile.text = decodeURI(File((app.activeDocument.fullName.toString()).replace(/(\..{2,4}$)/, "_Output$1")).fsName);
e_destFile.notify("onChange");
};
if (w.show() == 2) {
return null;
}
else {
if (e_destFile.text != "") {
var settingsObj = Object.entries(outputPDFPresets).reduce(function (a, b) {
a.push(b[1].selection.text);
return a;
}, []);
settingsFile.open("w");
settingsFile.write(settingsObj.join(","));
settingsFile.close();
saveOutputFiles(e_destFile.text, settingsObj);
app.activeDocument.close();
}
}
function saveOutputFiles(destPath, settingsObj) {
var doc = app.activeDocument;
var destFolder = File(destPath).parent;
var fileName = destPath.replace(/^.+[\\\/]/, "");
var counter = 0;
for (var all in InstructionTypes) {
var key = all;
var _loop_1 = function (lr) {
var thisInstruction = Instructions[lr][InstructionTypes[all]];
var thisLayer = void 0;
thisLayer = Object.entries(appLayers).find(function (m) {
try {
return m[1].name == lr;
}
catch (e) {
return false;
}
})[1];
if (!thisLayer) {
return "continue";
}
thisLayer.visible = thisInstruction.visible;
if (thisInstruction["delete"]) {
thisLayer.remove();
}
};
for (var lr in Instructions) {
_loop_1(lr);
}
var docName = fileName.replace(/\..{2,4}$/, "");
for (var instructionType in InstructionTypes) {
var key_1 = instructionType;
if (docName.endsWith(InstructionTypes[key_1])) {
docName = docName.substring(0, docName.length - InstructionTypes[key_1].length);
break;
}
}
var outputFormat = InstructionFormats[InstructionTypes[key]];
var outputSaveOptions = outputFormat.saveOptions(outputFormat.format == ".pdf" ? settingsObj[counter++] : null);
var destName = destFolder + "/" + docName + InstructionTypes[key] + outputFormat.format;
doc.saveAs(File(destName), outputSaveOptions);
}
}
};
SaveOutputWithNames();
... View more