Skip to content

Instantly share code, notes, and snippets.

@prdolmos
Last active June 20, 2018 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prdolmos/0dbb3e10fc5b4fe1dd4ad8afb2398346 to your computer and use it in GitHub Desktop.
Save prdolmos/0dbb3e10fc5b4fe1dd4ad8afb2398346 to your computer and use it in GitHub Desktop.
Prepare PrawnPDF document for Zund CutCenter with layers
#target Illustrator
// script.name = Prawn2ZundPDF-FOLDER.jsx;
// script.description = cleans and organizes single layer sticker PDFs for ZUND cut center;
// script.parent = Pablo D. Camaloon // 22/03/2017
// script.elegant = false?
/**
* Prerequisites:
* Single layer PDF with Register and Cut paths on a single layer.
* All elements are RGB. Register circles have path+fill; Cutlines only have stroke.
* Diecut: #EC008C; KissCut: #00AEEF; Register: #000000;
* Register dots are the only CompoundPathItems in the document
*/
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
sourceFolder = Folder.selectDialog( 'Select the folder with PDFs to process');
if ( sourceFolder != null ) {
files = new Array();
files = sourceFolder.getFiles( "*.pdf" );
if ( files.length > 0 ) {
// Get the destination to save the files
//destFolder = Folder.selectDialog( 'Select the folder where you want to save files.', '~' );
destFolder = sourceFolder;
for ( i = 0; i < files.length; i++ ) {
var docRef = app.open(files[i]); // returns the document object
main(docRef);
};
} else {
alert( 'No matching files found' );
}
}
function main(docRef) {
// 1) remove all pathItems that are clipping masks
var clippingCount = 0;
for (i = docRef.pageItems.length - 1; i >= 0; i--) {
if (docRef.pageItems[i].clipping == true) {
docRef.pageItems[i].remove();
}
};
// 2) ungroup all existing layers
if (docRef.groupItems.length) {
for (var i = 0; i < docRef.layers.length; i++) ungroup(docRef.layers[i]);
};
// 3) create spot colors swatches
addSpot('DieCut', 0, 100, 0, 0); //thru-cuts are pure magenta
addSpot('KissCut', 100, 0, 0, 0); //kiss-cuts are pure cyan
// 4) create layers for all cuts and register
docRef.layers[0].name = "Artwork"
var dieCutLayer = docRef.layers.add();
dieCutLayer.name = "Register";
dieCutLayer.printable = true;
var kissCutLayer = docRef.layers.add();
kissCutLayer.name = "KissCut";
kissCutLayer.printable = false;
var dieCutLayer = docRef.layers.add();
dieCutLayer.name = "DieCut";
dieCutLayer.printable = false;
docRef.layers["Register"].move( docRef, ElementPlacement.PLACEATBEGINNING );
app.redraw();
// 5) replace all RGB cut strokes with spot colors and move to correct layer
for (var i = 0; i < docRef.pathItems.length; i++) {
var pathArt = docRef.pathItems[i];
replaceStrokeRGBwithSpot(pathArt, 0, 174, 239, 'KissCut');
replaceStrokeRGBwithSpot(pathArt, 236, 0, 140, 'DieCut');
};
moveRegisterDots();
app.redraw();
var saveOptions = new IllustratorSaveOptions();
var aiDoc = new File(destFolder);
saveOptions.compatibility = Compatibility.ILLUSTRATOR15;
saveOptions.embedICCProfile = true;
docRef.saveAs( aiDoc, saveOptions );;
docRef.close();
}
function addSpot(name, c, m, y, k) {
try {
swatch = app.activeDocument.swatches[name]; // if swatch exists....
addSpot(name += '1', c, m, y, k); // ...add 1 to swatch name
} catch (e) {
var newSpot = app.activeDocument.spots.add();
newSpot.name = name;
var newColor = new CMYKColor();
newColor.cyan = c;
newColor.magenta = m;
newColor.yellow = y;
newColor.black = k;
newSpot.colorType = ColorModel.SPOT;
newSpot.color = newColor;
var newSpotColor = new SpotColor();
newSpotColor.spot = newSpot;
}
};
function replaceStrokeRGBwithSpot (pathArt, r, g, b, spotName){
var selectionArray = [];
if (pathArt.strokeColor.red == r
&& pathArt.strokeColor.green == g
&& pathArt.strokeColor.blue == b) {
pathArt.filled = false;
pathArt.stroked = true;
pathArt.strokeColor = docRef.swatches.getByName(spotName).color;
pathArt.strokeWidth = 0.25;
pathArt.move(docRef.layers[spotName], ElementPlacement.PLACEATEND)
}
};
function moveRegisterDots() {
var items, i = 0,
n = 0,
item, color, selectionArray = [];
items = docRef.compoundPathItems;
n = items.length;
if (items.length == 0) {
//alert("No images found");
return;
}
for (i = 0; i < n; i++) {
item = items[i];
item.move(docRef.layers['Register'], ElementPlacement.PLACEATEND);
}
}
/**
* all group to ungroup v.1 - CS, CS2,CS3,CS4
*
* Author: Nokcha (netbluew@gmail.com)
*
* This Script is Can be easily ungrouping to all group items in the Document.
*
*
* JS code (c) copyright: Jiwoong Song ( netbluew@nate.com )
* Copyright (c) 2009 netbluew@nate.com
* All rights reserved.
*
* This code is derived from software contributed to or originating on wundes.com
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by netbluew@nate.com
* and its contributors.
* 4. Neither the name of wundes.com nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY WUNDES.COM AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
function getChildAll(obj) {
var childsArr = new Array();
for (var i = 0; i < obj.pageItems.length; i++) childsArr.push(obj.pageItems[i]);
return childsArr;
}
function ungroup(obj) {
var elements = getChildAll(obj);
if (elements.length < 1) {
obj.remove();
return;
} else {
for (var i = 0; i < elements.length; i++) {
try {
if (elements[i].parent.typename != "Layer") elements[i].moveBefore(obj);
if (elements[i].typename == "GroupItem") ungroup(elements[i]);
} catch (e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment