将多个文件二进制数据以对象形式写入桌面

/**
     * @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();
}

 

给TA充电
共{{data.count}}人
人已充电
AE开发脚本开发

【AE脚本开发】颜色转换

2022-6-8 12:12:47

脚本开发

[AE脚本开发]使用curl 获取网页信息

2022-7-13 16:00:14

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索