-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathillustrator_template.jsx
327 lines (274 loc) · 9.99 KB
/
illustrator_template.jsx
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
////////////////////////////////////////////////////////////////////////////////
// NAME: Template
// PURPOSE: Do Some Action...
// CREATOR: gordon@gordonpotter.com
// LICENSE: MIT
// Copyright (c) 2010 Gordon Potter
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
function theScript() {
///////////////////////////////////////////////////
// BEGIN THE SCRIPT
//=================================================
////////////////////////////////////////////////////////////////////////////////
// INITIAL REQUIREMENTS
////////////////////////////////////////////////////////////////////////////////
// Require open document
try {
if (app.documents.length > 0 ) {
// OK to proceed to arguments setup and run main function
var args = {};
//
// args.SCRIPT_NAME = "TEMPLATE";
// args.DOC_REF = app.activeDocument;
// args.SELECTION = args.DOC_REF.selection;
// args.ACTIVE_LAYER = args.DOC_REF.activeLayer;
// args.ACTIVE_LAYER_NAME = args.ACTIVE_LAYER.name;
//
// // Call the main function
main(args);
}
else {
throw new Error('There are no documents open!');
}
}
catch(e) {
alert( e.message, "Script Alert", true);
}
////////////////////////////////////////////////////////////////////////////////
// SCRIPT MAIN PROCESS
////////////////////////////////////////////////////////////////////////////////
// Main Process
// The beginning of the script starts here
// Add main logic routine to this function
function main(args){
var myDialog = sfDialogFactory(testDialog());
myDialog.show();
}
////////////////////////////////////////////////////////////////////////////////
// SCRIPT SUPPORTING FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
// Supporting Functions
// Include functions that offer partial functionality
// These functions are called and acted upon within the mainProcess function
function objReflection(obj, display) {
// Function for easy object reflection
// USAGE EXAMPLE:
// var objectInfo = objReflection(someObject, "none");
// or
// objReflection(someObject, "alert");
var KVOs = [];
for(var member in obj){
KVOs.push("\n" + member + ":" + obj[member]);
};
switch(display)
{
case "alert":
alert("Object Relection: " + KVOs);
break;
case "none":
break;
default:
return undefined;
}
return KVOs;
}
function sfDialogFactory(dialog) {
// A factory method for creating dialog screens
// Dialog Window
var d = new Window("dialog", dialog.title);
// alert("Number of Inputs" + dialog.inputs.length);
// alert("Number of Options" + dialog.options.length);
var i; // counter
var len; // length of array elements
// Generate Groups
if (dialog.groups.length > 0) {
len = dialog.groups.length;
for (i = 0; i < len; i++ )
{
var currentGroup = dialog.groups[i];
var inputGroup = d.add ("panel", undefined, currentGroup.title);
inputGroup.alignChildren = ["fill","fill"];
if (currentGroup.elements.length > 0) {
// Add Elements
var ii;
var elemLen = currentGroup.elements.length;
for (ii = 0; ii < elemLen; ii++ )
{
var currentElement = currentGroup.elements[ii];
var el = inputGroup.add(currentElement.type, undefined, currentElement.value);
// Additional properties added for future reflection
el.elName = currentElement.name;
el.elIndex = ii;
switch(currentElement.type)
{
case "statictext":
el.visible = currentElement.visible;
break;
case "edittext":
el.visible = currentElement.visible;
break;
case "dropdownlist":
el.visible = currentElement.visible;
el.selection = currentElement.selection;
el.onChange = currentElement.onChange;
break;
default:
throw new Error('Unknown Dialog Element Type');
}
}
}
}
}
function getControlValues(set) {
var elementsData = {};
elementsData.controls = [];
// TO DO Add more types
var giLen = set.children.length;
for (var gi = 0; gi < giLen; gi++ )
{
var child = set.children[gi];
// alert(objReflection(child, "none", false));
// alert(child.type);
var control = {};
control.name = child.elName;
control.index = child.elIndex;
control.type = child.type;
control.visible = child.visible;
switch(child.type)
{
case "statictext":
control.value = child.text;
break;
case "edittext":
control.value = child.text;
break;
case "dropdownlist":
control.value = child.selection.text;
break;
default:
throw new Error('Unknown Dialog Element Type');
}
elementsData.controls.push(control);
// alert(objReflection(control, "none", false));
}
return elementsData;
}
// Buttons Group
var buttonGroup = d.add("group");
var bOK = buttonGroup.add("button", undefined, "Continue");
var bCANCEL = buttonGroup.add("button", undefined, "Cancel");
// Button Click Handlers
bOK.onClick = function(){
d.close();
dialog.callback("continue", getControlValues(inputGroup));
return true;
};
bCANCEL.onClick = function (){
d.close();
dialog.callback("cancel");
return false;
};
return d;
}
function testDialog() {
// TEST CASE
var dialogObj = {};
dialogObj.groups = []; // An array of dialog groups
dialogObj.title = "Simple Dialog";
var group1 = {};
group1.title = "Some Group";
// Add Elements using JSON shorthand syntax
group1.elements = [
{
"type":"statictext",
"value":"Test 1",
"visible":true
},
{
"type":"edittext",
"value":"Test 2",
"characters":20,
"visible":true
},
{
"type":"statictext",
"value":"Test 2",
"visible":true
},
{
"type":"dropdownlist",
"value":[1,2,3,4,5],
"visible":true,
"selection":0,
"onChange":function(){ alert("My Drop Downdown change"); return true;}
},
{
"type":"statictext",
"value":"Test 4",
"visible":true
},
{
"type":"statictext",
"value":"Test 5",
"visible":true
},
{
"type":"statictext",
"value":"Test 6",
"visible":true
},
{
"type":"statictext",
"value":"Test 7",
"visible":true
},
{
"type":"statictext",
"value":"Test 8",
"visible":true
},
];
// Add to group list
dialogObj.groups.push(group1);
dialogObj.callback = function(type, formData){
alert("Callback FUNCTION: " + objReflection(formData, "none", false));
if (type === "continue"){
// Do something with formData values here
var cLen = formData.controls.length;
var formValues = '';
for (var c = 0; c < cLen; c++ )
{
formValues = formValues + formData.controls[c].type + ":" + formData.controls[c].value + "\n";
}
alert("Continue Button Clicked.\n" + "Values Returned: \n" + formValues);
}
else {
// Cancel
return false;
}
}
return dialogObj;
}
//=================================================
// END THE SCRIPT
///////////////////////////////////////////////////
}
theScript();