Snippets
Created by
renderTom -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | // Cut by Guides.
// by Tomas Šinkūnas www.rendertom.com
//
// Script cuts image into small peaces defined by Guides
// and names them according to their grid number/letter.
// Rows are named with numbers [0-9] and columns by letters [A-Z]
// Each slice is saved as PNG file with original name + grid number&letter.
//
// Have one layer in the file. Add bunch of Guides.
// Save file as PNG and run the script. Enjoy the rest of your day!
//
// Use it on your own risk.
(function() {
try {
#target photoshop
if (app.documents.length === 0)
return alert("Open file first");
var doc = app.activeDocument;
if (doc.layers.length !== 1)
return alert("This script can handle only one layer in file. Merge your layers and run script again.");
var originalRulerUnits = app.preferences.rulerUnits,
plot = plotDocument(),
selectionCoordinates = [],
newLayer, newDocument,
layer = doc.layers[0];
app.preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;
layer.isBackgroundLayer && layer.isBackgroundLayer = false;
layer.selected = true;
doc.selection.deselect();
for (var i = 0, il = plot.length; i < il; i++) {
selectionCoordinates[0] = [plot[i].x, plot[i].y];
selectionCoordinates[1] = [plot[i].x, plot[i].y + plot[i].height];
selectionCoordinates[2] = [plot[i].x + plot[i].width, plot[i].y + plot[i].height];
selectionCoordinates[3] = [plot[i].x + plot[i].width, plot[i].y];
doc.selection.select(selectionCoordinates);
if (selectionHasPixels()) {
newLayer = layerViaCopy();
newLayer.name = plot[i].name;
duplicateToNewDocument(newLayer)
app.activeDocument.trim(TrimType.TRANSPARENT);
savePNG(File(doc.path + "/" + getBaseName(doc) + "-" + plot[i].name + ".psd"));
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = doc;
newLayer.remove();
}
}
app.preferences.rulerUnits = originalRulerUnits;
alert("done")
/////// HELPER FUNCTIONS ///////
/**
* Converts document Guides information to array of coordinates blocks
* @return {[Array]} [Array of objects, containing x, y, width, height, name properties]
*/
function plotDocument() {
var guidesArray = getGuidesCoordinates(),
coords = [],
vStart, vCurrent = vMin = 0,
hStart, hCurrent = hMin = 0,
columnName = "";
if (guidesArray.vertical[0] !== 0)
guidesArray.vertical.unshift(0);
if (guidesArray.vertical[guidesArray.vertical.length - 1] < Number(app.activeDocument.width))
guidesArray.vertical.push(Number(app.activeDocument.width))
if (guidesArray.horizontal[0] !== 0)
guidesArray.horizontal.unshift(0);
if (guidesArray.horizontal[guidesArray.horizontal.length - 1] < Number(app.activeDocument.height))
guidesArray.horizontal.push(Number(app.activeDocument.height))
for (var h = 1, hl = guidesArray.horizontal.length; h < hl; h++) {
hStart = hCurrent;
hCurrent = guidesArray.horizontal[h];
vCurrent = guidesArray.vertical[0];
vMin = guidesArray.vertical[0];
columnName = "a";
for (var v = 1, vl = guidesArray.vertical.length; v < vl; v++) {
vStart = vCurrent;
vCurrent = guidesArray.vertical[v];
coords.push({
x: vStart,
y: hStart,
width: vCurrent - vMin,
height: hCurrent - hMin,
name: h + columnName
});
vMin = vCurrent;
columnName = String.fromCharCode(columnName.charCodeAt(0) + 1).toString();
}
hMin = hCurrent;
}
return coords
}
/**
* Creates object with vertical/horizontal arrays that contain Guides coordinates
* @return {[Object]} []
*/
function getGuidesCoordinates() {
var docGuides = app.activeDocument.guides,
verticalGuides = [],
horizontalGuides = [];
for (var g = 0, gl = docGuides.length; g < gl; g++) {
if (docGuides[g].coordinate >= 0) {
if (docGuides[g].direction === Direction.VERTICAL && docGuides[g].coordinate <= Number(app.activeDocument.width)) {
verticalGuides.push(Number(docGuides[g].coordinate))
} else if (docGuides[g].direction === Direction.HORIZONTAL && docGuides[g].coordinate <= Number(app.activeDocument.height)) {
horizontalGuides.push(Number(docGuides[g].coordinate))
}
}
}
return {
vertical: removeDuplicatesFromArray(verticalGuides).sort(),
horizontal: removeDuplicatesFromArray(horizontalGuides).sort()
}
}
/**
* Removes Dulpicates from array
* @param {[Array]} userArray [Array with values]
* @return {[Array]} [New array without duplicates]
*/
function removeDuplicatesFromArray(userArray) {
var seen = {},
out = [],
j = 0,
item;
for (var i = 0, il = userArray.length; i < il; i++) {
item = userArray[i];
if (seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
}
/**
* Checks if selection has live pixels
* @return {[Bool]} [True if selection contains pixels]
*/
function selectionHasPixels() {
try {
app.activeDocument.selection.copy();
return true
} catch (e) {
return null
}
}
/**
* Creates new layer via copy.
* @return {[Object]} [New layer]
*/
function layerViaCopy() {
app.activeDocument.selection.copy();
app.activeDocument.artLayers.add();
app.activeDocument.paste();
return app.activeDocument.activeLayer;
}
/**
* Duplicates selected layer to new document
* @param {[Object]} layer [Layer to duplicate]
* @return {[Object]} [Returns layer in new document]
*/
function duplicateToNewDocument(layer) {
var doc = app.activeDocument;
var newDocument = app.documents.add(doc.width, doc.height, doc.resolution);
app.activeDocument = doc;
var newLayer = layer.duplicate(newDocument, ElementPlacement.INSIDE);
app.activeDocument = newDocument;
newDocument.layers[newDocument.layers.length - 1].remove();
return newLayer;
}
/**
* Saves active document as PNG file with transparency
* @param {[Object]} saveFile [File Object with PNG extension]
* @return Nothing
*/
function savePNG(saveFile) {
var pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.compression = 9;
pngSaveOptions.interlaced = false;
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
};
/**
* Gets document name without extension
* @param {[Object]} document [Optional. Document to whos name to get]
* @return {[String]} [Document name without extension]
*/
function getBaseName(document) {
document = document || app.activeDocument;
return decodeURI(document.name.substring(0, document.name.lastIndexOf(".")));
}
} catch (e) {
alert(e.toString() + "\nScript File: " + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +
"\nFunction: " + arguments.callee.name +
"\nError on Line: " + e.line.toString())
}
})();
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.