PhotoShop By 虚荣.ahk

#SingleInstance, force

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("oleaut32\SafeArrayUnaccessData", "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
    }     
  }
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。