0% found this document useful (0 votes)
229 views6 pages

Array Generator 1 0 0.Jsx

This document contains the source code for a Photoshop script that allows users to generate a two-dimensional array by cloning the selected layer multiple times in rows and columns. The script displays a dialog box where the user can specify the number of rows and columns in the array as well as the spacing between each element. It then duplicates the selected layer across and down according to the user's specifications.

Uploaded by

superdiaulo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views6 pages

Array Generator 1 0 0.Jsx

This document contains the source code for a Photoshop script that allows users to generate a two-dimensional array by cloning the selected layer multiple times in rows and columns. The script displays a dialog box where the user can specify the number of rows and columns in the array as well as the spacing between each element. It then duplicates the selected layer across and down according to the user's specifications.

Uploaded by

superdiaulo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

<!-- saved from url=(0089)http://morris-photographics.

com/photoshop/scripts/down
loads/Array%20Generator%201-0-0.jsx -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859
-1"><script type="text/javascript" src="chrome-extension://bfbmjmiodbnnpllbbbfbl
cplfjjepjdn/js/injected.js"></script><style type="text/css"></style></head><body
><pre style="word-wrap: break-word; white-space: pre-wrap;">// Array Generator -
Adobe Photoshop Script
// Description: generates a two-dimensional array by cloning the selected layer;
user defines number of rows and columns, as well as the horizontal and vertical
spacing
// Requirements: Adobe Photoshop CS, or higher
// Version: 1.0.0, 9/July/2009
// Author: Trevor Morris ([email protected])
// Website: http://morris-photographics.com/
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Script
s\'
// 2. Restart Photoshop
// 3. Choose File &gt; Scripts &gt; Array Generator
// ============================================================================
// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop
// bring application forward for double-click events
app.bringToFront();
///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
// initial dialog values
var dlgValues = new Object();
dlgValues.numRows = 3; // number of rows in array
dlgValues.numCols = 3; // number of columns in array
dlgValues.gapRows = 10; // space between rows
dlgValues.gapCols = 10; // space between columns
// declare local variables
var doc = activeDocument;
var layer = doc.activeLayer;
var name = layer.name;
var bounds = layer.bounds;
// check if Background layer is selected
if (layer.isBackgroundLayer) {
alert("The Background layer can't be used to create an array.",
'Background Layer', false);
return;
}
// check if current layer contains artwork
if (bounds[0] == bounds[2]) {
alert('The current layer contains no artwork.\n' +
'Please select another layer and try again.', 'Empty Lay
er', false);
return;
}
// check if a group (layer set) is selected; prompt to use entire group
if (layer.typename == 'LayerSet' &amp;&amp; !confirm('Are you sure you w
ish to create an array from the selected layer group?', false, 'Center Entire Gr
oup?')) {
return;
}
// remember layer lock state
var allLock = layer.allLocked;
var posLock = layer.positionLocked;
// unlock layer
layer.allLocked = false;
layer.positionLocked = false;
// get layer dimensions
// BUG: both width and height will be off by +2 px for shape layers
var width = Number(bounds[2] - bounds[0]);
var height = Number(bounds[3] - bounds[1]);
// display dialog; continues on OK or Enter key press (OK = 1, Cancel =
2)
var win = getDialog(dlgValues);
if (win.show() == 1) {
// handle rows
for (var row = 1; row &lt;= dlgValues.numRows; row++) {
// duplicate across (columns)
for (var col = 1; col &lt; dlgValues.numCols; col++) {
var duplicate = doc.activeLayer.duplicate();
doc.activeLayer = duplicate;
duplicate.name = name + ' [R' + row + 'C' + (col
+ 1) + ']';
duplicate.translate(width + dlgValues.gapCols);
}
// duplicate down (rows)
if (row &lt; dlgValues.numRows) {
duplicate = layer.duplicate(doc.activeLayer, Ele
mentPlacement.PLACEBEFORE);
doc.activeLayer = duplicate;
duplicate.name = name + ' [R' + (row + 1) + 'C1]
';
duplicate.translate(0, row * (height + dlgValues
.gapRows));
}
}
// name initial layer
layer.name = name + ' [R1C1]';
}
// restore original lock state
layer.allLocked = allLock;
layer.positionLocked = posLock;
}
///////////////////////////////////////////////////////////////////////////////
// getDialog - build Array Generator dialog
///////////////////////////////////////////////////////////////////////////////
function getDialog(dlgValues) {
// define dialog for Photoshop CS3 (and higher)
if (parseInt(version, 10) &gt;= 10) {
var dlg = "dialog {text: 'Array Generator', alignChildren: 'fill
', \
dims: Panel {orientation: 'column', \
text: 'Array Dimensions', alignChildren: 'right'
, \
numRows: Group {orientation: 'row', \
st: StaticText {text: '&amp;Rows:'}, \
et: EditText {text: " + dlgValues.numRow
s + ", characters: 4} \
}, \
numCols: Group {orientation: 'row', \
st: StaticText {text: '&amp;Columns:'},
\
et: EditText {text: " + dlgValues.numCol
s + ", characters: 4} \
} \
}, \
gaps: Panel {orientation: 'column', \
text: 'Array Spacing', alignChildren: 'right', \
gapRows: Group {orientation: 'row', \
st: StaticText {text: 'Ro&amp;w Spacing:
'}, \
et: EditText {text: " + dlgValues.gapRow
s + ", characters: 4} \
}, \
gapCols: Group {orientation: 'row', \
st: StaticText {text: 'Column &amp;Spaci
ng:'}, \
et: EditText {text: " + dlgValues.gapCol
s + ", characters: 4} \
} \
}, \
buttons: Group {orientation: 'row', \
okBtn: Button {text: 'OK', properties: {name: 'o
k'}}, \
cancelBtn: Button {text: 'Cancel', properties: {
name: 'cancel'}} \
} \
}";
}
// define dialog for Photoshop CS2
else {
var dlg = "dialog {text: 'Array Generator', alignChildren: 'fill
', \
dims: Panel {orientation: 'column', \
text: 'Array Dimensions', alignChildren: 'right'
, \
numRows: Group {orientation: 'row', \
st: StaticText {text: 'Rows:'}, \
et: EditText {text: " + dlgValues.numRow
s + ", preferredSize: [40, et.preferredSize.height]} \
}, \
numCols: Group {orientation: 'row', \
st: StaticText {text: 'Columns:'}, \
et: EditText {text: " + dlgValues.numCol
s + ", preferredSize: [40, et.preferredSize.height]} \
} \
}, \
gaps: Panel {orientation: 'column', \
text: 'Array Spacing', alignChildren: 'right', \
gapRows: Group {orientation: 'row', \
st: StaticText {text: 'Row Spacing:'}, \
et: EditText {text: " + dlgValues.gapRow
s + ", preferredSize: [40, et.preferredSize.height]} \
}, \
gapCols: Group {orientation: 'row', \
st: StaticText {text: 'Column Spacing:'}
, \
et: EditText {text: " + dlgValues.gapCol
s + ", preferredSize: [40, et.preferredSize.height]} \
} \
}, \
buttons: Group {orientation: 'row', \
okBtn: Button {text: 'OK', properties: {name: 'o
k'}}, \
cancelBtn: Button {text: 'Cancel', properties: {
name: 'cancel'}} \
} \
}";
}
// create dialog
var win = new Window(dlg);
win.center();
// check dialog values
with (win) {
// make first field active
dims.numRows.et.active = true;
// check number of rows
dims.numRows.et.onChange = function() {
this.text = checkValue(this.text, 1, 20);
dlgValues.numRows = Number(this.text);
// disable row spacing for a single row
if (dlgValues.numRows == 1) {
gaps.gapRows.et.enabled = false;
}
// (re)enable row spacing for mulitple rows
else {
gaps.gapRows.et.enabled = true;
}
}
// check number of columns
dims.numCols.et.onChange = function() {
this.text = checkValue(this.text, 1, 20);
dlgValues.numCols = Number(this.text);
// disable column spacing for a single column
if (dlgValues.numCols == 1) {
gaps.gapCols.et.enabled = false;
}
// (re)enable column spacing for mulitple columns
else {
gaps.gapCols.et.enabled = true;
}
}
// check row spacing
gaps.gapRows.et.onChange = function() {
this.text = checkValue(this.text, 0, 100);
dlgValues.gapRows = Number(this.text);
}
// check column spacing
gaps.gapCols.et.onChange = function() {
this.text = checkValue(this.text, 0, 100);
dlgValues.gapCols = Number(this.text);
}
}
return win;
}
///////////////////////////////////////////////////////////////////////////////
// checkValue - error-check value on change
///////////////////////////////////////////////////////////////////////////////
function checkValue(dlgValue, min, max) {
// declare local variables
var value = Number(dlgValue);
// ensure value is a valid number greater than or equal to min
if (isNaN(value) || value &lt; min) {
value = min;
}
// ensure value is less than max
else if (value &gt; max) {
value = max;
}
// ensure value is an integer
else if (Math.round(value) != value) {
value = Math.round(value);
}
// value is fine
else {
return dlgValue;
}
// show error and return correct value
alert('Please enter an integer between ' + min + ' and ' + max + '.', 'I
nteger Required', true);
return value.toString();
}
///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
if (parseInt(version, 10) &gt;= 9) {
return true;
}
else {
alert('This script requires Adobe Photoshop CS2 or higher.', 'Wr
ong Version', false);
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
if (documents.length) {
return true;
}
else {
alert('There are no documents open.', 'No Documents Open', false
);
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
if (confirm('An unknown error has occurred.\n' +
'Would you like to see more information?', true, 'Unknown Error'
)) {
alert(err + ': on line ' + err.line, 'Script Error', tru
e);
}
}
// test initial conditions prior to running main function
if (isCorrectVersion() &amp;&amp; isOpenDocs()) {
// remember unit settings; switch to pixels
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try {
// suspend history for CS3 (v10) or higher
if (parseInt(version, 10) &gt;= 10) {
activeDocument.suspendHistory('Array Generator', 'main()
');
}
// just run main for CS2 (v9)
else {
main();
}
}
catch(e) {
// don't report error on user cancel
if (e.number != 8007) {
showError(e);
}
}
// restore original unit setting
preferences.rulerUnits = originalRulerUnits;
}
</pre></body></html>

You might also like