【库】Autohotkey写的Photoshop辅助脚本-虚荣

修图过程中,一些基础性的操作、常用的调整模式等,基本都是一些重复操作,ahk这样就可以帮大家一定程度上解放自己的双手了,下面这个脚本中,包含了ps相关的基本函数,有使用编码经验的用户可以尝试使用,如果没呢,去社区群联系作者。

修图过程中,一些基础性的操作、常用的调整模式等,基本都是一些重复操作,ahk这样就可以帮大家一定程度上解放自己的双手了,下面这个脚本中,包含了ps相关的基本函数,有使用编码经验的用户可以尝试使用,如果没呢,去社区群联系作者。

#SingleInstance, force


main:
    ps := Photoshop.Init()
    doc := ps.NewDoc()
    ps.ExecJs("alert('hello world')")
    alText := doc.NewAl("psTextLayer")
    ;msgbox, % alText.kind
    alText.Size := 27
    alText.Font := "MicrosoftYaHei"

    loop, parse, % s, `n, `r
    {
        alText.Text := A_loopField
        outfile := A_ScriptDir "output" A_Now A_MSec ".png"
        if (A_Index = 1) 
            alText.MoveRel(0, 50)
        doc.SaveAs(outfile)
    }
    keywait, numpad0, D
    doc.close("不保存更改")
return

class PSError
{
    __Call(params*) {
        msgbox, % params[1]
        ; if (this.OnInit or (params[1] = "OnInit")) 
        ;     return
        ; throw this.__Class "无有" . params[1]
    }
    __Get(params*) {
        msgbox, % params[1]
        ; if (this.OnInit or (params[1] = "OnInit")) 
        ;     return
        ; throw this.__Class "无有" . params[1]
    }
}

class Photoshop extends PSError
{
    static _PS := ""
    static _COM_True := ComObject(0xb, -1, 1)
    static _COM_False := ComObject(0xb, 0, 1)

    Init() {

        this._PS := ComObjCreate("Photoshop.Application")
        return this
    }

    GetFontList()
    {
        ret := []
        loop, % this._PS.Fonts.Count
            ;~ 参数从1起
            ret.push(this._PS.Fonts.Item(A_Index).PostScriptName)
        return ret
    }

    GetFontList2Clipboard()
    {
        ret := ""
        for _, fontName in this.GetFontList()
            ret .= fontName "`n"
        Clipboard := ret
    }

    ;----------------------------------------------
    ;~ 获取当前打开的文档的数量
    DocCount {
        get {
            return this._PS.Documents.Count
        }
    }
    ;~ 打开一张图片到文档
    OpenFile(imgPath) {
        this._PS.Open(imgPath)
    }

    ;~ 获取当前激活的文档工程
    ActiveDoc()
    {
        return new Photoshop_Document(this._PS.ActiveDocument)
    }

    ;~ 获取已经打开的ps文档工程
    GetDoc(index)
    {
        return new Photoshop_Document(this._Ps.Documents.Item(index))
    }
    ;~ 新建一个ps文档工程
    NewDoc(width := 800, height := 300) {
        static unit := {"像素": 1, "英寸":2,"厘米": 3, "毫米": 4, "点":5, "派卡":6, "百分之": 7}
        this._PS.Preferences.RulerUnits := 1    ;度量单位[单位]
        return new Photoshop_Document(this._PS.Documents.Add(width, height)) 
    }

    ExecJs(js)
    {
        return this._ps.DoJavaScript(js)
    }  
}   

class Photoshop_Document extends PsError     ;~ app.Documents 对象
{
    __New(doc)
    {
        this.OnInit := true
        this._doc := doc
        this.OnInit := False
    }
    ;~ 返回ps对象
    App {
        get {
            return Photoshop
        }
    }

    ;~ 尺寸
    Width {
        get {
            return this._doc.Width
        }
        set {
            this._doc.Width := value
        }
    }

    Height {
        get {
            return this._doc.Height
        }
        set {
            this._doc.Height := value
        }
    }

    ;~ 文档路径
    Path {
        get {
            return this._doc.Path
        }
    }
    AlCount {
        get {
            return this._doc.ArtLayers.Count
        }
    }
    ;~ 获取或创建图层
    GetAlByName(name)
    {
        loop, % this.AlCount
        {   
            al := this.GetAlByIndex(A_Index)
            if (al.Name == name)
                return al
        }
        return ""
    }

    GetAlByIndex(index)
    {
        return this._doc.ArtLayers.Item(index)
    }

    NewAl(kind := "psNormalLayer")
    {
        return Photoshop_ArtLayer.Factory(this, kind)
    }

    ;~ 
    Close(option)
    {
        static SaveOption := { "psSaveChanges": 1   ;` 保存所有更改
            ,"保存更改": 1
            ,"psDoNotSaveChanges": 2            ;~ 不保存任何更改
            ,"不保存更改":2
            ,"psPromptToSaveChanges":3
            ,"提示更改":3}         ;~ 显示提示保存
        this._doc.Close(SaveOption[option])
    }

    ;~ 保存
    Save()
    {
        this._doc.Save()
    }
    ;~ 保存到
    SaveAs(filePath)
    {
        filePath := strReplace(filePath, "", "\")
        js =
        (
            var pngFile = new File('%filePath%');
            pngSaveOption = new PNGSaveOptions();
            pngSaveOption.compression = 0;
            pngSaveOption.interlaced = false;
            activeDocument.saveAs(pngFile, pngSaveOption, true, Extension.LOWERCASE)
        )
        this.App.ExecJs(js)
    }
}

class Photoshop_ArtLayer extends PsError
{
    static LayerKindDict := { "psNormalLayer": 1
        ,"psTextLayer":2
        ,"psSolidFillLayer":3
        ,"psGradientFillLayer":4
        ,"psPatternfillLayer":5
        ,"psLevelLayer":6
        ,"psCurvesLayer":7
        ,"psColorBalanceLayer":8
        ,"psBrightnessContrastLayer":9
        ,"psHueSaturationLayer":10
        ,"psSelectiveColorLayer":11
        ,"psChannelMixerLayer":12
        ,"psGradientMapLayer":13
        ,"psInversionLayer":14
        ,"psThresholdLayer":15
        ,"psPosterizeLayer":16
        ,"psSmartObjectLayer":17
        ,"psPhotoFilterLayer":18
        ,"psExposureLayer":19
        ,"psLayer3D":20
        ,"psVideoLayer":21
        ,"psBlackAndWhiteLayer":22
        ,"psVibrance":23}

    Factory(doc, kind := "psNormalLayer")
    {
        if not this.LayerKindDict.HasKey(kind)
            throw "此图层类型不存在: " kind
        
        cls := "Photoshop_" kind

        if IsObject(%cls%) {
            al := doc._doc.ArtLayers.Add
            al.kind := this.LayerKindDict[kind]
            return new %cls%(al)
        }
        else
        {
            al := doc._doc.ArtLayers.Add
            al.kind := this.LayerKindDict[kind]
            return new Photoshop_Layer(al)
        }
    }
    __New(al)
    {

        this.OnInit := true
        this._al := al
        this.OnInit := False
    }


    TypeName 
    {
        get  {
            return this._al.typename
        }
    }
    Name {
        get {
            return this._al.Name
        }
        set {
            this._al.Name := value
        }
    }
    Kind {
        get {
            return this._al.Kind
        }
    }

    ;~ 是否隐藏
    Visible {
        get {
            return this._al.Visible
        }
        
        set {
            value := (value) ? -1 : 0
            this._al.Visible := (value) ? Photoshop._COM_True : Photoshop._COM_False
            ;DllCall("oleaut32SafeArrayUnaccessData", "ptr", _comarr)
        }
    }

    Doc {
        get {
            return new Photoshop_Document(this._al.Parent)
        }
    }
    App {
        get {
            return PhotoShop
        }
    }
    
    不透明度 {  ;~ 双精度浮点
        get {
            return this._al.Opacity
        }
        set {
            this._al.Opacity := value
        }
    }

    MoveRel(x, y)   ;~ 双精度浮点数
    {
        this._al.TransLate(x, y)
    }
}

class Photoshop_psTextLayer extends Photoshop_ArtLayer
{
    __New(al)
    {
        this.OnInit := true
        this._al := al
        this.OnInit := False
    }

    Font {  ;~ 字体
        get {
            return this._al.TextItem.Font
        }
        set {

            this._al.TextItem.Font := value
        }
    }

    Text {  ;~ 文本内容
        get {
            return this._al.TextItem.Contents
        }
        set {
            
            this._al.TextItem.Contents := value
        }    
    }

    Size {  ;~ 字号
        get {
            return this._al.TextItem.Size
        }
        set {
            this._al.TextItem.Size := value
        }         
    }
}

使用方法等我有时间后补吧!

为TA充电
共{{data.count}}人
人已赞赏
AHKV2办公

【AHKv2】文件夹监控-大白

2021-5-22 21:48:57

AHKV1学习

输出调试信息到控制台-feiyue

2021-5-25 16:34:33

2 条回复 A文章作者 M管理员
  1. huajian628

    大佬 nb

  2. Antony

    没有功能介绍呢

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索