Snippets

renderTom AE Script: Export to Binary String

Updated by renderTom -

File Export To Binary String.js Added

  • Ignore whitespace
  • Hide word diff
+// Convert any file to binary string.
+// By Tomas Sinkunas. www.rendertom.com
+// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
+
+
+(function (thisObj) {
+	var options = {
+		oneFile: true,
+		addRes: true,
+	};
+
+	scriptBuildUI(thisObj);
+
+	function scriptBuildUI(thisObj) {
+		var typeOfWindow = getTypeOfWindow();
+		var win = (thisObj instanceof Panel) ? thisObj : new Window(typeOfWindow, "Export To Binary", undefined, {
+			resizeable: true
+		});
+		win.alignChildren = ["fill", "fill"];
+		var checkOneFile = win.add("checkbox", undefined, "Write everything to one file");
+		checkOneFile.value = options.oneFile;
+
+		var checkAddRes = win.add("checkbox", undefined, "Add \"Create File\" option");
+		checkAddRes.value = options.addRes;
+
+		var btnConvert = win.add("button", undefined, "Export To Binary");
+		btnConvert.onClick = function () {
+			options.oneFile = checkOneFile.value;
+			options.addRes = checkAddRes.value;
+			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 main() {
+		try {
+
+			var sourceFiles = File.openDialog("Select files to with to export to binary", undefined, {
+				multiSelect: true
+			});
+			if (!sourceFiles) return;
+
+			var binaryStringsArray = [],
+				resourceStringsArray = [],
+				finalString = "",
+				binaryString = "",
+				resourceString = "",
+				displayName = "",
+				fileName = "",
+				extension = "",
+				variableName = "",
+				i, il;
+
+			if (options.oneFile === true) {
+				for (i = 0, il = sourceFiles.length; i < il; i++) {
+					displayName = File(sourceFiles[i]).displayName;
+					fileName = displayName.split(".")[0];
+					extension = displayName.split(".")[1];
+
+					variableName = toVariableName(fileName);
+					binaryString = "var " + variableName + "_bin = " + convertToBinary(sourceFiles[i]);
+					resourceString = "var " + variableName + " = createResourceFile (\"" + fileName + "." + extension + "\", " + variableName + "_bin, userDataFolder);";
+
+					binaryStringsArray.push(binaryString);
+					resourceStringsArray.push(resourceString);
+				}
+
+				finalString = buildString(binaryStringsArray, resourceStringsArray);
+
+				saveFile(File.decode(sourceFiles[0].path) + "/ALL.txt", finalString);
+
+			} else {
+				for (i = 0, il = sourceFiles.length; i < il; i++) {
+					binaryString = convertToBinary(sourceFiles[i]);
+					saveFile(sourceFiles[i].fsName + ".txt", binaryString);
+				}
+			}
+
+			alert("Done");
+
+		} catch (e) {
+			alert(e.toString() + "\nLine: " + e.line.toString());
+		}
+
+	}
+
+	function buildString(binaryStringsArray, resourceStringsArray) {
+		var finalString = binaryStringsArray.join("\n");
+
+		if (options.addRes === true) {
+			finalString += "\n\n\nvar userDataFolder = \"~/Desktop/\";\n";
+			finalString += resourceStringsArray.join("\n") + "\n\n";
+
+			finalString += "function createResourceFile(filename, binaryString, resourceFolder) {\n" +
+				"\tvar fileObject = new File(resourceFolder + \"/\" + filename);\n" +
+				"\tif (!File(fileObject).exists) {\n" +
+				"\t\tfileObject.encoding = \"BINARY\";\n" +
+				"\t\tfileObject.open(\"w\");\n" +
+				"\t\tfileObject.write(binaryString);\n" +
+				"\t\tfileObject.close();\n" +
+				"\t}\n" +
+				"\treturn fileObject;\n" +
+				"}\n";
+		}
+
+		return finalString;
+	}
+
+	function toVariableName(string) {
+		var varName = string;
+
+		// remove non A-Z from beginning
+		varName = varName.replace(/[^A-Za-z]*/, "");
+		if (varName.length === 0) {
+			varName = "undef_" + string;
+		}
+
+		// capitalise first letters of the word;
+		varName = varName.replace(/\w\S*/g, function (txt) {
+			return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
+		});
+
+		// remove white spaces;
+		varName = varName.replace(/\s+/g, "");
+
+		// lowercase first letter;
+		varName = varName.charAt(0).toLowerCase() + varName.substr(1);
+
+		return varName;
+	}
+
+
+
+	function convertToBinary(fileObj, appendix) {
+		var fileContent = readFileContent(fileObj, "BINARY");
+		var binaryString = fileContent.toSource();
+
+		binaryString = binaryString.replace(/^\(new String\(/, "");
+		binaryString = binaryString.replace(/\)\)$/, ";");
+
+		return binaryString;
+	}
+
+	function getTypeOfWindow() {
+		var isAfterEffects = function () {
+			return BridgeTalk.appName.toLowerCase().match("aftereffects");
+		};
+
+		return isAfterEffects() ? "palette" : "dialog";
+	}
+
+	function saveFile(filePath, fileContents, encoding) {
+		var fileObject = new File(filePath);
+		encoding = encoding || "UTF-8";
+
+		fileObject.open("W");
+		fileObject.encoding = encoding;
+		fileObject.write(fileContents);
+		fileObject.close();
+	}
+
+	function readFileContent(fileObj, encoding) {
+		encoding = encoding || "UTF-8";
+		fileObj.encoding = encoding;
+		fileObj.open('r');
+		var fileContent = fileObj.read();
+		fileObj.close();
+		return fileContent;
+	}
+})(this);

File Export To Binary String.jsx Deleted

  • Ignore whitespace
  • Hide word diff
-// Convert any file to binary string.
-// By Tomas Sinkunas. www.rendertom.com
-// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
-
-
-buildUI();
-
-function buildUI() {
-
-	var win = new Window("palette", "Select file to convert to binary", undefined);
-		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
-
-		btnSelect.onClick = function() {
-			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
-			if (targetFile) {
-				for (var i = 0, il = targetFile.length; i < il; i ++) {
-
-					var binString = convertToBinary(targetFile[i]);
-
-					var fullFileName = File(targetFile[i]).displayName;
-					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
-					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
-
-					saveFile(pathToFile, binString);
-
-				}
-				alert("Done.");
-			}
-		}
-
-	win.show();
-}
-
-function convertToBinary(fileObj) {
-	fileObj.encoding = "BINARY";
-	fileObj.open('r');
-	var fileData = fileObj.read();
-		fileObj.close();
-	var binaryString = fileData.toSource();
-
-		binaryString = binaryString.replace(/^\(new String\(/, "[");
-		binaryString = binaryString.replace(/\)\)$/, "];");
-
-	return "var myBin = " + binaryString;
-}
-
-function saveFile(filePath, fileContents) {
-	var myFileObject = File(filePath);
-		myFileObject.open("W");
-		myFileObject.encoding = "utf-8";
-		myFileObject.write(fileContents);
-		myFileObject.close();
-}
Updated by renderTom -

File Export To Binary String.jsx Modified

  • Ignore whitespace
  • Hide word diff
 		}
 
 	win.show();
-
 }
 
 function convertToBinary(fileObj) {
Updated by renderTom -

File Export To Binary String.js Deleted

  • Ignore whitespace
  • Hide word diff
-// Convert any file to binary string.
-// By Tomas Sinkunas. www.rendertom.com
-// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
-
-
-buildUI();
-
-function buildUI() {
-
-	var win = new Window("palette", "Select file to convert to binary", undefined);
-		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
-
-		btnSelect.onClick = function() {
-			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
-			if (targetFile) {
-				for (var i = 0, il = targetFile.length; i < il; i ++) {
-
-					var binString = convertToBinary(targetFile[i]);
-
-					var fullFileName = File(targetFile[i]).displayName;
-					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
-					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
-
-					saveFile(pathToFile, binString);
-
-				}
-				alert("Done.");
-			}
-		}
-
-	win.show();
-
-}
-
-function convertToBinary(fileObj) {
-	fileObj.encoding = "BINARY";
-	fileObj.open('r');
-	var fileData = fileObj.read();
-		fileObj.close();
-	var binaryString = fileData.toSource();
-
-		binaryString = binaryString.replace(/^\(new String\(/, "[");
-		binaryString = binaryString.replace(/\)\)$/, "];");
-
-	return "var myBin = " + binaryString;
-}
-
-function saveFile(filePath, fileContents) {
-	var myFileObject = File(filePath);
-		myFileObject.open("W");
-		myFileObject.encoding = "utf-8";
-		myFileObject.write(fileContents);
-		myFileObject.close();
-}

File Export To Binary String.jsx Added

  • Ignore whitespace
  • Hide word diff
+// Convert any file to binary string.
+// By Tomas Sinkunas. www.rendertom.com
+// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
+
+
+buildUI();
+
+function buildUI() {
+
+	var win = new Window("palette", "Select file to convert to binary", undefined);
+		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
+
+		btnSelect.onClick = function() {
+			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
+			if (targetFile) {
+				for (var i = 0, il = targetFile.length; i < il; i ++) {
+
+					var binString = convertToBinary(targetFile[i]);
+
+					var fullFileName = File(targetFile[i]).displayName;
+					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
+					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
+
+					saveFile(pathToFile, binString);
+
+				}
+				alert("Done.");
+			}
+		}
+
+	win.show();
+
+}
+
+function convertToBinary(fileObj) {
+	fileObj.encoding = "BINARY";
+	fileObj.open('r');
+	var fileData = fileObj.read();
+		fileObj.close();
+	var binaryString = fileData.toSource();
+
+		binaryString = binaryString.replace(/^\(new String\(/, "[");
+		binaryString = binaryString.replace(/\)\)$/, "];");
+
+	return "var myBin = " + binaryString;
+}
+
+function saveFile(filePath, fileContents) {
+	var myFileObject = File(filePath);
+		myFileObject.open("W");
+		myFileObject.encoding = "utf-8";
+		myFileObject.write(fileContents);
+		myFileObject.close();
+}
Updated by renderTom -

File Export To Binary String.js Deleted

  • Ignore whitespace
  • Hide word diff
-// Convert any file to binary string.
-// By Tomas Sinkunas. www.rendertom.com
-// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
-
-
-buildUI();
-
-function buildUI() {
-
-	var win = new Window("palette", "Select file to convert to binary", undefined);
-		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
-
-		btnSelect.onClick = function() {
-			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
-			if (targetFile) {
-				for (var i = 0, il = targetFile.length; i < il; i ++) {
-
-					var binString = convertToBinary(targetFile[i]);
-
-					var fullFileName = File(targetFile[i]).displayName;
-					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
-					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
-
-					saveFile(pathToFile, binString);
-
-				}
-				alert("Done.");
-			}
-		}
-
-	win.show();
-
-}
-
-function convertToBinary(fileObj) {
-	fileObj.encoding = "BINARY";
-	fileObj.open('r');
-	var fileData = fileObj.read();
-		fileObj.close();
-	var binaryString = fileData.toSource();
-
-		binaryString = binaryString.replace(/^\(new String\(/, "[");
-		binaryString = binaryString.replace(/\)\)$/, "];");
-
-	return "var myBin = " + binaryString;
-}
-
-function saveFile(filePath, fileContents) {
-	var myFileObject = File(filePath);
-		myFileObject.open("W");
-		myFileObject.encoding = "utf-8";
-		myFileObject.write(fileContents);
-		myFileObject.close();
-}

File Export To Binary String.jsx Added

  • Ignore whitespace
  • Hide word diff
+// Convert any file to binary string.
+// By Tomas Sinkunas. www.rendertom.com
+// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
+
+
+buildUI();
+
+function buildUI() {
+
+	var win = new Window("palette", "Select file to convert to binary", undefined);
+		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
+
+		btnSelect.onClick = function() {
+			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
+			if (targetFile) {
+				for (var i = 0, il = targetFile.length; i < il; i ++) {
+
+					var binString = convertToBinary(targetFile[i]);
+
+					var fullFileName = File(targetFile[i]).displayName;
+					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
+					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
+
+					saveFile(pathToFile, binString);
+
+				}
+				alert("Done.");
+			}
+		}
+
+	win.show();
+
+}
+
+function convertToBinary(fileObj) {
+	fileObj.encoding = "BINARY";
+	fileObj.open('r');
+	var fileData = fileObj.read();
+		fileObj.close();
+	var binaryString = fileData.toSource();
+
+		binaryString = binaryString.replace(/^\(new String\(/, "[");
+		binaryString = binaryString.replace(/\)\)$/, "];");
+
+	return "var myBin = " + binaryString;
+}
+
+function saveFile(filePath, fileContents) {
+	var myFileObject = File(filePath);
+		myFileObject.open("W");
+		myFileObject.encoding = "utf-8";
+		myFileObject.write(fileContents);
+		myFileObject.close();
+}
Updated by renderTom -

File Export To Binary String.js Added

  • Ignore whitespace
  • Hide word diff
+// Convert any file to binary string.
+// By Tomas Sinkunas. www.rendertom.com
+// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
+
+
+buildUI();
+
+function buildUI() {
+
+	var win = new Window("palette", "Select file to convert to binary", undefined);
+		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
+
+		btnSelect.onClick = function() {
+			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
+			if (targetFile) {
+				for (var i = 0, il = targetFile.length; i < il; i ++) {
+
+					var binString = convertToBinary(targetFile[i]);
+
+					var fullFileName = File(targetFile[i]).displayName;
+					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
+					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
+
+					saveFile(pathToFile, binString);
+
+				}
+				alert("Done.");
+			}
+		}
+
+	win.show();
+
+}
+
+function convertToBinary(fileObj) {
+	fileObj.encoding = "BINARY";
+	fileObj.open('r');
+	var fileData = fileObj.read();
+		fileObj.close();
+	var binaryString = fileData.toSource();
+
+		binaryString = binaryString.replace(/^\(new String\(/, "[");
+		binaryString = binaryString.replace(/\)\)$/, "];");
+
+	return "var myBin = " + binaryString;
+}
+
+function saveFile(filePath, fileContents) {
+	var myFileObject = File(filePath);
+		myFileObject.open("W");
+		myFileObject.encoding = "utf-8";
+		myFileObject.write(fileContents);
+		myFileObject.close();
+}

File exportToBinaryString.jsx Deleted

  • Ignore whitespace
  • Hide word diff
-// Convert any file to binary string.
-// By Tomas Sinkunas. www.rendertom.com
-// Select some files and they will be converted to binary string. The output files will be saved to same location as original files, but will have .txt extension.
-
-
-buildUI();
-
-function buildUI() {
-
-	var win = new Window("palette", "Select file to convert to binary", undefined);
-		var btnSelect = win.add("button", undefined, "Select file and I'll convert it to binary");
-
-		btnSelect.onClick = function() {
-			var targetFile = File.openDialog("Please select file", undefined, {multiSelect:true});
-			if (targetFile) {
-				for (var i = 0, il = targetFile.length; i < il; i ++) {
-
-					var binString = convertToBinary(targetFile[i]);
-
-					var fullFileName = File(targetFile[i]).displayName;
-					var FileName = fullFileName.substr(0, fullFileName.lastIndexOf("."));
-					var pathToFile = File.decode(targetFile[i].path) + "/" + FileName + ".txt";
-
-					saveFile(pathToFile, binString);
-
-				}
-				alert("Done.");
-			}
-		}
-
-	win.show();
-
-}
-
-function convertToBinary(fileObj) {
-	fileObj.encoding = "BINARY";
-	fileObj.open('r');
-	var fileData = fileObj.read();
-		fileObj.close();
-	var binaryString = fileData.toSource();
-
-		binaryString = binaryString.replace(/^\(new String\(/, "[");
-		binaryString = binaryString.replace(/\)\)$/, "];");
-
-	return "var myBin = " + binaryString;
-}
-
-function saveFile(filePath, fileContents) {
-	var myFileObject = File(filePath);
-		myFileObject.open("W");
-		myFileObject.encoding = "utf-8";
-		myFileObject.write(fileContents);
-		myFileObject.close();
-}
  1. 1
  2. 2
HTTPS SSH

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