/**
* @description 获取文件二进制数据源码
* @param {Object} file 目标文件
*/
function getBinary(file) {
var file = isFile(file) ? file : new File(file);
file.open("r");
file.encoding = "BINARY";
var binaryBuffer = file.read();
file.close();
var binaryString = binaryBuffer.toSource();
binaryString = binaryString.slice(13, -3);
return binaryString
}
/**
* @description 将文件二进制数据以对象形式写入桌面json文件
*/
function writeBinary() {
var desktopFolder = new Folder(Folder.desktop);
var files = File.openDialog("Select a file.", "All files:*.*", true);
var result = [];
var obj = {};
files.map(function (file, index) {
obj.id = index;
obj.name = file.name;
obj.data = getBinary(file);
var content = obj.stringifys();
result.push(content);
})
var outFile = [desktopFolder.fsName, files[0].name.split(".")[0] + ".json"].join("/");
writeFile(result.stringifys(), outFile);
File(outFile).execute();
}
// 对象转换为字符串
Object.prototype.stringifys = function () {
var str = "";
str += "{";
for (var p in this) {
if (this.hasOwnProperty(p)) {
if (typeof this[p] === 'number') {
str += "\"" + p.toString() + "\"" + ":" + this[p] + ",";
} else {
str += "\"" + p.toString() + "\"" + ":" + "\"" + this[p].toString() + "\"" + ",";
}
}
}
str += "}";
return str
}
// 数组转换为字符串
Array.prototype.stringifys = function () {
var str = "";
str += "[";
for (var i = 0, il = this.length; i < il; i++) {
str += this[i] + ",";
}
str += "]";
return str
}
/**
* @description 是否为文件
*/
function isFile(item) {
return type(item) === 'file'
}
/**
* @description 判断变量类型
* @param {Object} o 目标对象
*/
function type(o) {
var s = Object.prototype.toString.call(o);
return s.match(/\[object (.*?)\]/)[1].toLowerCase();
}