Snippets

renderTom AE Script: JSX Compiler

Created by renderTom - last modified
  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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**********************************************************************************************
    Compile JSX to JSXBIN files from After Effects.
    Copyright (c) 2017 Tomas Šinkūnas. All rights reserved.
    www.rendertom.com

    Description:
    	Execute this script from After Effects.
    	Compile individual or deeply nested JSX files to JSXBIN from After Effects.
    	Choose to keep or remove original JSX files after compilation is finished.
    	NOTE: You must have ExtendScript Toolkit installed for script to work.

	Change log:
		v1.0 - 2017 09 08
			- Initial release

	Released as open-source under the MIT license:
		The MIT License (MIT)
		Copyright (c) 2017 Tomas Šinkūnas www.renderTom.com
		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 (thisObj) {
	var scriptName = "JSX Compiler",
		compilerOptions = {
			removeOriginalFile: false,
			selectScriptFolder: false
		},
		isWin = $.os.indexOf("Windows") > -1;

	var jsxCompilerESTK = function () {
		var compilerOptions, scriptFiles, isWin, PB, numCompiledFiles = 0;

		compilerOptions = {
			removeOriginalFile: false,
			selectScriptFolder: false
		};

		isWin = $.os.indexOf("Windows") > -1;
		PB = new ProgressBar("JSX compiler");

		scriptFiles = getScriptFiles(compilerOptions.selectScriptFolder);
		if (!scriptFiles) return;

		PB.show("Compiling files.", scriptFiles.length);
		for (var i = 0, il = scriptFiles.length; i < il; i++) {
			PB.up(File.decode(scriptFiles[i].displayName) + " (" + (i + 1) + "/" + scriptFiles.length + ")");
			if (exportJSBIN(scriptFiles[i])) {
				numCompiledFiles++;
				if (compilerOptions.removeOriginalFile === true) {
					scriptFiles[i].remove();
				}
			}
		}

		PB.close();
		alert("Compiled " + numCompiledFiles + " " + ((numCompiledFiles > 1) ? "scripts." : "script."));

		// Utilities
		// ---------------
		function exportJSBIN(inputFile) {
			var fileContent = readFileContent(inputFile),
				outputFile = changeExtension(inputFile, "jsxbin"),
				compiledString,
				includepath,
				compileSuccess = true;

			try {
				includepath = inputFile.parent ? inputFile.parent.absoluteURI : "/";
				compiledString = app.compile(fileContent, inputFile.absoluteURI, includepath);
				writeFile(outputFile, compiledString);
			} catch (e) {
				compileSuccess = false;
				alert("Unable to compile file " + File.decode(inputFile.displayName) + "\nFile path: " + inputFile.fsName + "\n" + e.toString() + "\nLine: " + e.line.toString());
			}

			return compileSuccess;
		}

		function getScriptFiles(getScriptsInFolders) {
			var scriptFiles, targetFolder;

			if (getScriptsInFolders === true) {
				targetFolder = Folder.selectDialog("Select scripts folder you want to compile");
				if (targetFolder) {
					scriptFiles = getAllFiles(targetFolder, true, "js, jsx");
				}
			} else {
				scriptFiles = selectFiles(true, "js, jsx", "Please select \"*.js\" or \"*.jsx\" files");
			}

			return scriptFiles;
		}

		// Read Write
		// ---------------
		function readFileContent(fileObj, encoding) {
			var fileContent;
			encoding = encoding || "utf-8";

			fileObj.open('r');
			fileObj.encoding = encoding;
			fileContent = fileObj.read();
			fileObj.close();
			return fileContent;
		}

		function writeFile(pathToFile, fileContent, encoding) {
			var newFile = (pathToFile instanceof File) ? pathToFile : new File(pathToFile);
			encoding = encoding || "utf-8";

			newFile.encoding = encoding;
			newFile.open("w");
			newFile.write(fileContent);
			newFile.close();
		}

		// Files
		// ---------------
		function getAllFiles(pathToFolder, recursion, extensionList) {
			var pathFiles = Folder(pathToFolder).getFiles(),
				files = [],
				subfiles = [];

			if (extensionList === undefined)
				extensionList = "";
			else if (typeof extensionList !== "function")
				extensionList = new RegExp("\.\(" + extensionList.replace(/,/g, "|").replace(/ /g, "") + ")$", "i");

			PB.show("Collecting files.", pathFiles.length);
			for (var i = 0, il = pathFiles.length; i < il; i++) {
				if (pathFiles[i] instanceof File) {
					PB.up(File.decode(pathFiles[i].displayName));
					if (pathFiles[i].name.match(extensionList)) {
						files.push(pathFiles[i]);
					}
				} else if (pathFiles[i] instanceof Folder && recursion === true) {
					subfiles = getAllFiles(pathFiles[i], recursion, extensionList);
					for (var j = 0, jl = subfiles.length; j < jl; j++) {
						files.push(subfiles[j]);
					}
				}
			}
			return files;
		}

		function selectFiles(multiSelect, extensionList, infoMessage) {
			infoMessage = infoMessage || (multiSelect ? "Please select multiple files" : "Please select file");
			var filter = isWin ? "*." + extensionList.replace(/ /g, "").replace(/,/g, ";*.") : function (file) {
				var re = new RegExp("\.\(" + extensionList.replace(/,/g, "|").replace(/ /g, "") + ")$", "i");
				if (file.name.match(re) || file.constructor.name === "Folder")
					return true;
			};
			return File.openDialog(infoMessage, filter, multiSelect);
		}

		function changeExtension(file, newExtension) {
			file = (file instanceof File) ? file : new File(file);
			return file.fsName.substring(0, file.fsName.lastIndexOf(".")) + "." + newExtension;
		}

		function ProgressBar(panelTitle) {
			"use strict";

			var win = new Window("palette", panelTitle, undefined);
			win.alignChildren = ["fill", "top"];
			win.preferredSize.width = 360;
			win.spacing = 10;

			win.etProgressName = win.add("statictext");
			win.pbProgressBar = win.add("progressbar");
			win.pbProgressBar.preferredSize.height = 5;
			win.etProgressInfo = win.add("statictext");

			this.name = function (progressName) {
				win.etProgressName.text = progressName;
			};

			this.info = function (progressInfo) {
				win.etProgressInfo.text = progressInfo;
			};

			this.show = this.reset = function (progressName, maxValue) {
				win.pbProgressBar.value = 0;
				win.pbProgressBar.maxvalue = maxValue;

				this.name(progressName);

				win.center();
				win.show();
			};

			this.up = function (progressInfo) {
				win.pbProgressBar.value++;

				this.info(progressInfo);
				win.update();
			};

			this.hide = function () {
				win.hide();
			};

			this.close = function () {
				win.close();
			};
		}
	};

	scriptBuildUI(thisObj);

	function main() {
		var pathToESTK, compilerScript, cmd;

		if (!canWriteFiles()) return;

		pathToESTK = getPathToESTK();
		if (!pathToESTK) return;

		compilerScript = getCompilerScript();
		if (!compilerScript) return;

		cmd = "cd " + "\"" + compilerScript.parent.fsName + "\"";
		if (isWin) {
			cmd = "cmd /c " + cmd + " && \"" + pathToESTK + "\" -cmd \"" + compilerScript.displayName + "\"";
		} else {
			cmd = cmd + "; " + "\"" + pathToESTK + "\"" + " -cmd " + "\"" + compilerScript.name + "\"";
		}

		system.callSystem(cmd);
		// compilerScript.remove();
	}

	function scriptBuildUI(thisObj) {
		var win = (thisObj instanceof Panel) ? thisObj : new Window("palette", scriptName, undefined, {
			resizeable: true
		});
		win.alignChildren = ["fill", "top"];
		win.spacing = 2;

		win.checkDeleteOriginal = win.add("checkbox", undefined, "Delete original files");
		win.checkDeleteOriginal.helpTip = "Removes original JSX file after compilation.";
		win.checkDeleteOriginal.onClick = function () {
			compilerOptions.removeOriginalFile = this.value;
		};

		win.btnProcessFolder = win.add("button", undefined, "Process folder");
		win.btnProcessFolder.helpTip = "Precess deeply nested JSX files in folders";
		win.btnProcessFolder.onClick = function () {
			compilerOptions.selectScriptFolder = true;
			main();
		};

		win.btnSelectIndividual = win.add("button", undefined, "Process individual files");
		win.btnSelectIndividual.helpTip = "Select individual JSX files to compile.";
		win.btnSelectIndividual.onClick = function () {
			compilerOptions.selectScriptFolder = false;
			main();
		};

		win.onResizing = win.onResize = function () {
			this.layout.resize();
		};

		if (win instanceof Window) {
			win.center();
			win.show();
		} else {
			win.layout.layout(true);
			win.layout.resize();
		}
	}

	function getCompilerScript() {
		var compilerAsString, compilerFile;

		compilerAsString = getCompilerAsString();
		compilerAsString = compilerAsString.replace("removeOriginalFile: false", "removeOriginalFile: " + compilerOptions.removeOriginalFile);
		compilerAsString = compilerAsString.replace("selectScriptFolder: false", "selectScriptFolder: " + compilerOptions.selectScriptFolder);

		compilerFile = new File("~/Desktop/JSX Compiler-ESTK.js");
		writeFile(compilerFile, compilerAsString);

		return compilerFile;
	}

	function getCompilerAsString() {
		var disclaimerString =
			"/**********************************************************************************************\n" +
			"\tJSX Compiler-ESTK.js\n" +
			"\tCopyright (c) 2017 Tomas Šinkūnas. All rights reserved.\n" +
			"\twww.rendertom.com\n" +
			"\n" +
			"\tDescription:\n" +
			"\t\tExecute this script from ExtendScript Toolkit.\n" +
			"\t\tChange \"compilerOptions\" to your liking.\n" +
			"\n" +
			"\tReleased as open-source under the MIT license:\n" +
			"\t\tThe MIT License (MIT)\n" +
			"\t\tCopyright (c) 2017 Tomas Šinkūnas www.renderTom.com\n" +
			"\t\tPermission is hereby granted, free of charge, to any person obtaining a copy of this\n" +
			"\t\tsoftware and associated documentation files (the \"Software\"), to deal in the Software\n" +
			"\t\twithout restriction, including without limitation the rights to use, copy, modify, merge,\n" +
			"\t\tpublish, distribute, sublicense, and/or sell copies of the Software, and to permit persons\n" +
			"\t\tto whom the Software is furnished to do so, subject to the following conditions:\n" +
			"\n" +
			"\t\tThe above copyright notice and this permission notice shall be included in all copies or\n" +
			"\t\tsubstantial portions of the Software.\n" +
			"\n" +
			"\t\tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,\n" +
			"\t\tINCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR\n" +
			"\t\tPURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE\n" +
			"\t\tFOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n" +
			"\t\tOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\n" +
			"\t\tDEALINGS IN THE SOFTWARE.\n" +
			"\n" +
			"**********************************************************************************************/\n\n";

		var compilerAsString = jsxCompilerESTK.toString();
		return disclaimerString + "//@target estoolkit#dbg\n(" + compilerAsString + ")();";
	}

	function getPathToESTK() {
		var pathToESTK = "",
			pathToExec = "",
			searchPath = "",
			extension = "",
			executable = null;

		if (isWin) {
			pathToESTK = "C:\\Program Files (x86)\\Adobe\\Adobe ExtendScript Toolkit CC\\ExtendScript Toolkit.exe";
			extension = "exe";
		} else {
			pathToESTK = "/Applications/Adobe ExtendScript Toolkit CC/ExtendScript Toolkit.app";
			pathToExec = "/Contents/MacOS/ExtendScript Toolkit";
			extension = "app";
		}

		searchPath = pathToESTK + pathToExec;

		if (File(searchPath).exists) {
			executable = searchPath;
		} else {
			pathToESTK = selectFiles(false, extension, "Please select ExtendScript Toolkit." + extension);

			if (pathToESTK) {
				searchPath = pathToESTK.fsName + pathToExec;
				if (File(searchPath).exists) {
					executable = searchPath;
				} else {
					alert("\"" + File.decode(pathToESTK.name) + "\" is not ExtendScript " + File.decode("%C2%AF%5C_(%E3%83%84)_/%C2%AF") + ". Try again!");
				}
			}
		}

		return executable;
	}

	function writeFile(pathToFile, fileContent, encoding) {
		var newFile = (pathToFile instanceof File) ? pathToFile : new File(pathToFile);
		encoding = encoding || "utf-8";

		newFile.encoding = encoding;
		newFile.open("w");
		newFile.write(fileContent);
		newFile.close();
	}

	function canWriteFiles() {
		if (!isSecurityPrefSet()) {
			alert(scriptName + " requires access to write files.\n" +
				"Go to the \"General\" panel of the application preferences and make sure " +
				"\"Allow Scripts to Write Files and Access Network\" is checked.");
			app.executeCommand(2359);
			if (!isSecurityPrefSet()) return null;
		}
		return true;
	}

	function isSecurityPrefSet() {
		var securitySetting = app.preferences.getPrefAsLong("Main Pref Section", "Pref_SCRIPTING_FILE_NETWORK_SECURITY");
		return (securitySetting === 1);
	}

	function selectFiles(multiSelect, extensionList, infoMessage) {
		infoMessage = infoMessage || (multiSelect ? "Please select multiple files" : "Please select file");
		var filter = ($.os.indexOf("Windows") != -1) ? "*." + extensionList.replace(/ /g, "").replace(/,/g, ";*.") : function (file) {
			var re = new RegExp("\.\(" + extensionList.replace(/,/g, "|").replace(/ /g, "") + ")$", "i");
			if (file.name.match(re) || file.constructor.name === "Folder")
				return true;
		};
		return File.openDialog(infoMessage, filter, multiSelect);
	}

})(this);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.