//20201206-Photoshop jsx常用模块封装

ps_key_class = {/*

Application
    notifiter
    preferences
    document
        layers
            art layers  
                text item(Object)
            layer sets
                layer set
                art layer
        selection
        path item   路径
            sub path item
            path point
        channel
        layer comp
        count item
        color sampler
        measurement scale
        document info
        history state
        save options
            Photoshop
            BMP
            DIF
            EPS
            JPEG
            PDF
            Pict File
            PNG
            TIFF
            ...
        solid color
            RGB color
            CMYK color
            Gray  color
            HSB color
            Lab color
            No color


    

*/}

// macOS类
Mac = function () {

    this.font = app.fonts
    this.systemInformation = app.systemInformation
    this.answer = function answer() {
        answer = confirm("Set the foreground and background to my favorite colors?")    //确认弹框(选择yes or none)

        if (answer) {
            //command
        }
    }


}

//Photoshop类
Ps = function () {

    // 属性

    //ps自身相关信息
    this.name = app.name
    this.version = app.version
    this.memorySize = parseInt(app.freeMemory / (1024 * 1024)) + "MB\r\r"   //可用内存
    this.path = app.path.fsName //不使用fsName会有乱码

    this.language = app.language
    this.rulerUnits = app.preferences.rulerUnits    //ps标尺
    this.isQuicktimeAvailable = app.isQuicktimeAvailable    //是否支持quciktime
    this.colorSettings = app.colorSettings
    this.displayDialogs = app.displayDialogs  //警告弹窗
    this.macintoshFileTypes = app.macintoshFileTypes //ps支持打开的文档类型列表

    //文件
    this.open = function openFile(filePath) {
        // var filePath = "/Users/luomingyang/Luo_MingYang/Example/eg_jpg.jpg"
        var file = new File(filePath)   //文件夹是Folder
        open(file)
        return true
    }
    // app.openDialog()  //文件选择弹框


    //文档
    this.recentFiles = app.recentFiles   //列表
    this.docs = app.documents
    this.activeDocument = app.activeDocument    //如果没有打开的文件,则会报异常
    this.length = app.documents.length  //当前ps的文档数目

    //工具栏
    this.currentTool = app.currentTool  //当前选中的工具


    //拾色器
    this.foregroundColor = app.foregroundColor
    this.backgroundColor = app.backgroundColor  //返回 solodcolor



    //方法

    //rgb 颜色模式的色值
    this.rgbColor = function rgbColor(red, green, blue) {

        red = typeof red !== 'undefined' ? red : app.foregroundColor.rgb.red
        green = typeof green !== 'undefined' ? green : app.foregroundColor.rgb.green
        blue = typeof blue !== 'undefined' ? blue : app.foregroundColor.rgb.blue

        var rgbColor = new SolidColor().rgb // 实例化颜色对象
        rgbColor.red = red
        rgbColor.green = green
        rgbColor.blue = blue

        // return { 'red': red, 'green': green, 'blue': blue }
        return [red, green, blue]
    }

    //hrex 颜色模式
    this.hrexColor = function hrexColor(hrexColor) {

        hrexColor = typeof hrexColor !== 'undefined' ? hrexColor : app.foregroundColor.rgb.hexValue
        app.foregroundColor.rgb.hexValue = hrexColor
        return hrexColor
    }

    //窗口类
    this.window = function window(type, title) {
        type = typeof type !== 'undefined' ? type : 'dialog'
        title = typeof title !== 'undefined' ? title : 'Alert Box Builder'//标题
        var dlg = new Window(type, title);
        dlg.frameLocation = [1000, 1000];
        dlg.btnPnl = dlg.add('panel', undefined, 'Build it');//组
        dlg.btnPnl.testBtn = dlg.btnPnl.add('button', undefined, 'Test');//按钮
        dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Build', { name: 'ok' })  //相当于返回ok

        // dlg.msgPnl = dlg.add('panel');
        // dlg.children[0].text = 'Messages'
        // dlg.btnPnl.children['ok'].text = "Build";
        // dlg.btnPnl.ok.text = "Build";
        // var myOkButton = dlg.findElement("ok");
        dlg.show();
    }

    //创建PDF的demo
    // use all the files in the Samples folder 
    // var inputFolder = new Folder(app.path + "/Samples/")
    // // see if we have something interesting 
    // if (inputFolder != null) {
    //     // get all the files found in this folder that are Adobe Photoshop CS6 (.psd format) 
    //     var inputFiles = inputFolder.getFiles("*.psd")
    //     // output to the desktop 
    //     var outputFile = File("~/Desktop/JavaScriptPresentation.pdf")
    //     // there are defaults but I like to set the options myself 
    //     var options = new PresentationOptions
    //     options.presentation = true
    //     options.view = true
    //     options.autoAdvance = true
    //     options.interval = 5
    //     options.loop = true
    //     options.transition = TransitionType.RANDOM
    //     // create the presentation 
    //     makePDFPresentation(inputFiles, outputFile, options)

    //     alert("Presentation file saved to: " + outputFile.fsName)
    // }


    //系统
    // beep()
    // app.bringToFront()
    // app.doAction('导出ipg到源文件', '保存文件')  //执行动作,动作名,动作组名
    // app.makePDFPresentation (inputFiles, outputFiles , options) //option 是可选项
    // app.showColorPicker () //拾色器
    // app.togglePalettes() //切换操作面板的可见性

}

//document文档类
Doc = function (doc_sort_number) {


    this.name = app.activeDocument.name
    this.width = app.activeDocument.width
    this.height = app.activeDocument.height
    this.parent = app.activeDocument.parent //Adobe Photoshop 对象

    //属性
    this.layers = activeDocument.layers //返回值为artlayers或者layerset
    this.activeLayer = activeDocument.activeLayer //返回值为已激活的图层
    this.artLayers = activeDocument.artLayers   //返回非图层组的图层集合
    this.activeHistoryState = activeDocument.activeHistoryState   //返回历史记录
    this.backgroundLayer = activeDocument.backgroundLayer    //返回背景图层对象
    this.colorSamplers = activeDocument.colorSamplers   //与此文档关联的当前颜色采样器。
    this.componentChannels = activeDocument.componentChannels    //只读。组成文档的颜色通道;例如,RGB文档的红色、绿色和蓝色通道。
    this.fullName = activeDocument.fullName //文档的完整路径,返回的是文件对象
    // this.guides = activeDocument.guides
    // this.info = activeDocument.info
    // this.layerComps = activeDocument.layerComps

    this.layerSets = activeDocument.layerSets//图层组,返回列表
    this.mode = activeDocument.mode//RGB或者CMYK
    this.path = activeDocument.path//RGB或者CMYK
    this.pathItems = activeDocument.pathItems//RGB或者CMYK
    this.pixelAspectRatio = activeDocument.pixelAspectRatio //像素比例 1
    this.saved = activeDocument.saved //当前是否保存
    this.selection = activeDocument.selection  //返回选区对象
    this.typename = activeDocument.typename  //返回类名document


    //方法
    this.changeMode = function (destinationMode, options) { activeDocument.changeMode(destinationMode, options) }
    this.close = function (saving) { activeDocument.close(saving) }//SaveOptions.DONOTSAVECHANGES不保存退出
    this.crop = function (bounds, angle, width, height) {
        activeDocument.crop(bounds, angle, width, height)
    }  //bound是一个4个元素的数组
    this.duplicate = function (name, mergeLayersOnly) { activeDocument.duplicate(name, mergeLayersOnly) }//文档名称和是否复制合并的图层
    this.flatten = function () { activeDocument.flatten() }//合并文档中的所有图层
    this.mergeVisibleLayers = function () { activeDocument.mergeVisibleLayers() }//合并文档中的可见图层,隐藏图层不能被选中
    this.paste = function () { activeDocument.paste() }//选择性粘贴 > 贴入图层
    this.rasterizeAllLayers = function () { activeDocument.rasterizeAllLayers() }//栅格化所有图层
    this.resizeCanvas = function (width, height) { activeDocument.resizeCanvas(width, height) }//更改画布大小,实际文件大小不变,视野变小
    this.resizeImage = function (width, height, resolution, resampleMethod) { activeDocument.resizeImage(width, height, resolution, resampleMethod) }//调整图片大小 宽度、高度、分辨率和重采样方法,宽度必填参数
    // this.revealAll() = function () { activeDocument.revealAll()}//展示文档细节
    this.rotateCanvas = function () { activeDocument.rotateCanvas(number) }//展示文档细节
    this.save = function () { activeDocument.save() }//展示文档细节
    this.saveAs = function (saveIn, options, asCopy, extensionType) {
        activeDocument.saveAs(saveIn, options, asCopy, extensionType)
    }//文件保存地址,对象:jpg,png,副本保存
    this.splitChannels = function () { activeDocument.splitChannels() }//将通道进行分离并单独拆成psd文件打开 


}

//documents多文档类
Docs = function () {

    //属性
    this.length = documents.length
    this.parent = documents.parent
    this.typename = documents.typename


    //方法
    this.add = function
        (width, height, resolution, name, mode, initialFill, pixelAspectRatio, colorProfileName) { documents.add(width, height, resolution, name, mode, initialFill, pixelAspectRatio, colorProfileName) }//宽度为必填
    this.getByName = function (name) { documents.getByName(name) }


}


//图层集合类
ArtLayers = function () {

    // 属性
    this.len = activeDocument.layers.length     //ps 图层面板总项,不统计组内图层
    this.parent = activeDocument.layers.parent      //图层的父级object doc
    this.typename = activeDocument.layers.typename       //类是layers


    // layer = new activeDocument.layers()
    // 方法
    this.add = function () { activeDocument.artLayers.add(); return this }
    this.getByName = function (name) { activeDocument.artLayers.getByName(name); return this }
    this.removeAll = function () { activeDocument.artLayers.removeAll() }   //移除不是图层组的项目
}


//单图层类
ArtLayer = function (artLayerName) {

    artLayerName = typeof artLayerName !== 'undefined' ? artLayerName : app.activeDocument.artLayers[0].name
    // artLayerName = app.activeDocument.artLayers[0].name

    this.layerRef = app.activeDocument.artLayers.getByName(artLayerName)

    //图层属性
    this.kind = this.layerRef.kind
    this.allLocked = this.layerRef.allLocked    //锁定状态
    this.blendMode = this.layerRef.blendMode    //混合模式
    this.bounds = this.layerRef.bounds    //图层的左上角坐标和右下角坐标
    this.opacity = this.layerRef.opacity
    this.textItem = this.layerRef.opacity//只有当图层是文字呈时可以使用


    // 图层方法
    this.fillOpacity = function (opacity) { this.layerRef.fillOpacity = opacity }  //图层的填充透明度
    this.clear = function () { this.layerRef.clear() }  //图层的填充透明度
    this.copy = function (merge) { this.layerRef.copy(merge) }  //拷贝图层,当参数为true时,将合并可见图层并拷贝
    this.cut = function () { this.layerRef.cut() }  //剪切选区,当前图层有选区时,才可以使用此命令
    this.desaturate = function () { this.layerRef.desaturate() }  //去色
    this.duplicate = function (relativeObject, insertionLocation) { this.layerRef.duplicate(relativeObject, insertionLocation) }  //拷贝图层,参数有([relativeObject] [, insertionLocation]
    this.link = function (withLayer) { this.layerRef.link(withLayer) }  //with图层或者图层组
    this.merge = function () { this.layerRef.merge() }  //向下合并图层
    this.move = function (relativeObject, insertionLocation) { this.layerRef.move(relativeObject, insertionLocation) }  //向下合并图层
    this.remove = function () { this.layerRefre.remove() }  //删除图层
    this.resize = function (horizontal, vertical) { this.layerRefre.resize(horizontal, vertical) }  //缩放图层,水平参数是必须的
    this.rotate = function (angle) { this.layerRef.rotate(angle) }  //旋转,默认 锚点是中心
    this.translate = function (deltaX, deltaY) { this.layerRef.translate(deltaX, deltaY) }  //相对当前位置平移相对距离

    this.unlink = function () { this.layerRefre.unlink() }  //解除链接



    /*
    ArtLayerLists = 'typename,vectorMaskDensity,visible'
 
    //图层的方法
    methods = '{
    adjustBrightnessContrast(brightness, contrast),
        adjustColorBalance([shadows][, midtones][, highlights][, preserveLuminosity],
            adjustCurves(curveShape),
            adjustLevels
                (inputRangeStart, inputRangeEnd, inputRangeGamma, outputRangeStart, outputRangeEnd),
            applyAddNoise(amount,
                distribution, monochromatic),
            applyAverage(),
            applyBlur(),
            applyBlurMore(), applyClouds(), applyCustomFilter(characteristics, scale,
                offset), applyDeInterlace(eliminateFields, createFields), applyDespeckle(), applyDifferenceClouds(), applyDiffuseGlow(graininess, glowAmount, clearAmount) 
}'
*/


}


//历史记录
HistoryStates = function () {

    //属性
    this.length = activeDocument.historyStates.length
    this.parent = activeDocument.historyStates.parent
    this.typename = activeDocument.historyStates.typename


    //方法
    this.geiByname = function (name) { activeDocument.historyStates.getByName; return this }
}


// a = new Doc()

// alert(a.crop())

// activeDocument.splitChannels()
alert(app.activeDocument.historyStates[2])
// alert(1)
// alert('1')


版权声明:本文为weixin_43090804原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。