Last active
September 13, 2022 19:08
-
-
Save Inventsable/a74c998986628778b722399c338205e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No need for target lines, redundant in standalone scripts not utilizing Bridgetalk. | |
// Not personally a fan of writing out globals like this, a bit messy/cluttered to my mind. | |
// The active document | |
var doc = app.activeDocument; | |
// The path of the original document | |
var originalDocPath = doc.path; | |
// The name of the original document | |
var originalDocName = doc.name; | |
// Get just the file name. Ignore the file extension .pdf and .ai | |
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, ""); | |
//Add visual size to filename | |
if (layerExists("Visual")) | |
app.activeDocument.layers["Visual"].hasSelectedArtwork = true; | |
var objects = app.activeDocument.selection; | |
var decimalPlaces = 0; | |
// Collect length info | |
var objWidth = app.selection[0].width; | |
var objHeight = app.selection[0].height; | |
// Conversions | |
var ppi = 2.834646; | |
var Width = objWidth / ppi; | |
var Height = objHeight / ppi; | |
// ---- | |
// JS utility functions | |
function get(type, parent) { | |
if (arguments.length == 1 || !parent) parent = app.activeDocument; | |
var result = []; | |
if (!parent[type]) return []; | |
for (var i = 0; i < parent[type].length; i++) result.push(parent[type][i]); | |
return result; | |
} | |
Array.prototype.find = function (callback) { | |
for (var i = 0; i < this.length; i++) | |
if (callback(this[i], i, this)) return this[i]; | |
return null; | |
}; | |
Array.prototype.forEach = function (callback) { | |
for (var i = 0; i < this.length; i++) callback(this[i], i, this); | |
}; | |
function getKeys(obj) { | |
var result = []; | |
for (var key in obj) result.push(key); | |
return result; | |
} | |
// ---- | |
// Illustrator specific general functions | |
function getLayerByName(name) { | |
var target = get("layers").find(function (layer) { | |
return layer.name == name; | |
}); | |
return target ? target : null; | |
} | |
function layerExists(name) { | |
/** | |
* app.activeDocument.layers.getByName returns silent failure, causing the script to stop running. | |
* You prevent this by manually constructing an array and creating a function that has a soft error | |
* case, allowing the script to continue running even if the layer of this name wasn't found. | |
*/ | |
if (getLayerByName(name)) return true; | |
else return false; | |
} | |
// ---- | |
// Script specific functions to make things much easier to look at: | |
function verifyFolder(folderPath) { | |
var temp = Folder(folderPath); | |
if (!temp.exists) temp.create(); | |
return true; | |
} | |
function createSaveString(root, suffix, ext, shouldVerifyFolder) { | |
if (arguments.length == 3) shouldVerifyFolder = false; | |
if (shouldVerifyFolder) verifyFolder(root + "/" + suffix); | |
return ( | |
root + | |
"/" + | |
suffix + | |
"/" + | |
originalDocName + | |
"_" + | |
Width.toFixed(decimalPlaces) + | |
"x" + | |
Height.toFixed(decimalPlaces) + | |
"_" + | |
suffix + | |
"." + | |
ext | |
); | |
} | |
function createExportOptions(type) { | |
getKeys(type.params).forEach(function (param) { | |
type.options[param] = type.params[param]; | |
}); | |
return type.options; | |
} | |
// Even if it looks long, it's far easier to store your data like this because then we can dynamically create it. | |
// It'll look much nicer when we go to actually use it inside of the createExportOption function just above: | |
var exportConfig = { | |
Artwork: { | |
options: new IllustratorSaveOptions(), | |
ext: "ai", | |
params: { | |
pdfCompatible: false, | |
}, | |
}, | |
LowRES: { | |
options: new PDFSaveOptions(), | |
ext: "pdf", | |
params: { | |
pDFPreset: "[Smallest File Size]", | |
}, | |
}, | |
Print: { | |
options: new PDFSaveOptions(), | |
ext: "pdf", | |
params: { | |
pDFPreset: "[PDF/X-4:2008]", | |
}, | |
}, | |
ZUND: { | |
options: new IllustratorSaveOptions(), | |
ext: "ai", | |
params: { | |
compatibility: Compatibility.Illustrator8, | |
}, | |
}, | |
}; | |
// The main function of the script: | |
function main() { | |
var path = doc.path; // You originally had this pointing to something like doc.path.path? | |
// Artwork: | |
doc.saveAs( | |
File(createSaveString(path, "ARTWORK", "ai", false)), | |
createExportOptions(exportConfig.Artwork) | |
); | |
// LowRes: | |
doc.saveAs( | |
File(createSaveString(path, "LR", "pdf", true)), | |
createExportOptions(exportConfig.LowRES) | |
); | |
//Hide Layers | |
if (layerExists("Visual")) | |
app.activeDocument.layers["Visual"].visible = false; | |
if (layerExists("Cutter")) | |
app.activeDocument.layers["Cutter"].visible = false; | |
// PDF: | |
doc.saveAs( | |
File(createSaveString(path, "PRINT", "pdf", true)), | |
createExportOptions(exportConfig.Print) | |
); | |
// Not sure I understand the need for toggling back on if it's going to be removed (or why we need to remove artwork if already hidden) | |
var temp; | |
if (layerExists("Visual")) { | |
temp = getLayerByName("Visual"); | |
temp.visible = true; | |
temp.remove(); | |
} | |
if (layerExists("Cutter")) { | |
temp = getLayerByName("Cutter"); | |
temp.visible = true; | |
temp.remove(); | |
} | |
redraw(); | |
// ZUND: | |
doc.saveAs( | |
File(createSaveString(path, "ZUND", "ai", true)), | |
createExportOptions(exportConfig.ZUND) | |
); | |
} | |
// Which is called for execution once the entire file loads: | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment