Snippets

renderTom AE Script: Export to Binary String

You are viewing an old version of this snippet. View the current version.
Revised by renderTom - 3b31e1f
// 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();
}
HTTPS SSH

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