Last active
December 12, 2018 20:57
-
-
Save am/1283a10f23826a54d65ab2d74000dd1c to your computer and use it in GitHub Desktop.
Illustrator util to copy layers content to artboards
This file contains 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
// based on https://github.com/michaelchaize/appliness/blob/master/Illustrator-create-artboards/CreateArtboardsLayers.jsx | |
// Illustrator util to copy layers content to artboards | |
// Each layer must contain one single group | |
// It will name the artboard to match the layer name | |
// For each 20 layers it creates a new row | |
// Ensure the original artboard is placed in the top left corner | |
// TODO: translate the original artboard on the top left corner | |
// TODO: calculate the numbers of columns based on artboard dimensions and available width | |
// TODO: group layer contents before translate and ungroup after | |
var columns = 20; | |
var padding = 50; | |
if (app.documents.length > 0) { | |
createArtboardPerLayer(); | |
} else { | |
Window.alert('You must open at least one document.'); | |
} | |
function createArtboardPerLayer() { | |
const doc = app.activeDocument; | |
var totalLayers = doc.layers.length; | |
// hold a reference to the original artboard position and dimentions | |
const rect = doc.artboards[0].artboardRect; | |
const artboardWidth = rect[2] - rect[0]; | |
const artboardHeight = rect[3] - rect[1]; | |
var layer, translateX, translateY, position, artboard, group = null; | |
for (var i = 0; i < totalLayers; i++) { | |
// reset selection | |
doc.selection = null; | |
layer = doc.layers[i]; | |
// skip hidden layers | |
if (layer.visible === false) continue; | |
// unlock the layer | |
layer.locked = false; | |
// select elements on the layer | |
layer.hasSelectedArtwork = true; | |
// calculate translations | |
translateX = ((artboardWidth + padding) * (i % columns)); | |
translateY = -((artboardHeight - padding) * Math.floor(i / columns)) - artboardHeight; | |
position = [ | |
rect[0] + translateX, | |
rect[1] - translateY, | |
rect[2] + translateX, | |
rect[3] - translateY | |
] | |
// create a new artboard at the correct position | |
artboard = doc.artboards.add(position); | |
// change the name based on the layer name | |
artboard.name = layer.name; | |
// translate selected layer | |
group = layer.groupItems[0]; | |
group.translate(translateX, -translateY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment