当前位置: 首页 > 工具软件 > JXA > 使用案例 >

关于Photoshop的JXA、UXP文档整理

田化
2023-12-01

JXA

Photoshop

const ps=Application('Adobe Photoshop 2021')

属性

doJavascript

ps.doJavascript('alert(1)', [1, 2, 3], 'before running')

backgroundColor(set)

ps.backgroundColor.properties() //get
// =>{red: 0, green: 0, blue: 0}

ps.backgroundColor.red = 10 //set

foregroundColor(set)

ps.foregroundColor.properties() //get
// =>{red: 0, green: 0, blue: 0}

ps.foregroundColor.red = 10 //set

currentDocument(set)

ps.currentDocument.properties()
// {backgroundLayer: function, bitsPerChannel: "eight", colorProfileKind: "working", colorProfileName: "sRGB IEC61966-2.1", mode: "RGB", …}

currentTool(set)

ps.currentTool()
// sliceTool

ps.currentTool = 'moveTool' //set

displayDialogs(set)

ps.displayDialogs

ps.displayDialogs = 'always' //erro dialogs / never / always

macintoshFileTypes

ps 支持的文件类型数组

ps.macintoshFileTypes()
// ["8BPS", "8BPB", "8BIM", "8BIM", "8BIM", "8BIM", "8BIM", "8BIM", "8BIM", "8BIM", …] (46)

systemInformation()

ps.systemInformation()
// "Adobe Photoshop 版本: 22.0.0 20201006.r.35 2020/10/06: 4587a1caa63  x64...

menu

const se = Application('System Events')
se.processes
  .byName(this.app.name())
  .menuBars[0].menuBarItems[2].menus[0].menuItems.name()
//  => ["新建...", "打开...", "在 Bridge 中浏览...", "打开为智能对象...", "最近打开文

settings(set)

fonts

获取字体,不能使用中文搜索

ps.fonts.whose({ name: { _contains: 'Noto Sans S Chinese' } }).name()
// ["Noto Sans S Chinese Thin", "Noto Sans S Chinese Light", "Noto Sans S Chinese DemiLight", "Noto Sans S Chinese Regular", "Noto Sans S Chinese Medium", "Noto Sans S Chinese Bold Bold", "Noto Sans S Chinese Black Bold"] (7)

photoshopOpenDialog

打开文件选择界面,返回文件列表
ps.photoshopOpenDialog()

batch

  • bath

batch text : the name of the action to play (note that the case of letters in the Action name is important and must match the case of the name in the Actions palette)

ps.batch(‘test’,[],‘默认动作’)
fromFiles: list of alias : list of input files to operate on

from: text : the name of the action set containing the action being played (note that the case of letters in the Action Set name is important and must match the case of the name in the Actions palette)

[withOptions: BatchOptions] : options for Batch

→ text : run the batch automation routine

changeMode

changeMode Document : the document object or objects to be operated upon
to: “bitmap”/‌"CMYK"/‌"grayscale"/‌"indexed color"/‌"Lab"/‌"multichannel"/‌"RGB" : name of mode to change to
[withOptions: ChangeModeOptions] : options for changing the mode

ps.currentDocument.changeMode({to:'CMYK'})

convertToShape

convertToShape method : converts the text object and its containing layer to a fill layer with the text changed to a clipping path

convertToShape Text-object : the text-object object or objects to be operated upon

ps.currentDocument.layers.whose({kind:'=text'})[0].convertToShape()

crop

失败
ps.currentDocument.crop({bounds:[100,100,100,100]})

export

export method
export Document : the document object or objects to be operated upon
in: file specification : the file to export to
[as: “Illustrator paths”/‌"save for web"] : the type of export ( default: Illustrator paths )
[withOptions: ExportOptions] : options for the export type specifie

ps.currentDocument.export({in:'/Users/luomingyang/Desktop/test.png',as:'save for web'})

flipCanvas

翻转画布,参数: “horizontal”/‌"vertical"

ps.currentDocument.flipCanvas({direction:'horizontal'})

注意

当使用ps.settings.properties()时,ps 会崩溃,尽量避免使用该命令

objects

currentDocument

const doc=ps.currentDocument()

link

ps.currentDocument.layers[0].link({with:ps.currentDocument.layers[1]})

mergeVisibleLayers(合并可见图层)

ps.currentDocument.mergeVisibleLayers()

属性

backgroundLayer

currentLayer(set)

filePath

height

width

info

doc.info.properties()
// {title: "", author: "", authorPosition: "", caption: "", captionWriter: "", …} = $3

selection

获取选区信息

doc.selection.properties()
// {bounds: [56, 62, 141, 190], solid: true, container: function, bestType: "specifier", defaultType: "specifier", …}
objects

以下对象都具有(get/make/delete)的方法

artLayer
channel
colorSampler
countItem
historyState
layer
layerComp
layerSet
pathItem

documents

artLayers

ps.currentDocument.artLayers.properties()
// {fillOpacity: 100, grouped: false, backgroundLayer: false, pixelsLocked: false, positionLocked: false, …}
// 1 {fillOpacity: 100, grouped: false, backgroundLayer: false, pixelsLocked: true, positionLocked: false, …}
// ...

相比与 layers 的区别是没有图层组的信息

ps.currentDocument.layers.properties()
// => {enabledChannels: Array, name: "组 1", allLocked: false, blendMode: "pass through", linkedLayers: [], …}
// 1 {fillOpacity: 100, grouped: false, backgroundLayer: false,

fonts

settings

coreSuite

commands

  • close

ps.currentDocument.close({saving:'no'})//ask,yes,no

  • duplicate

duplicate specifier : the object(s) to duplicate
[to: location specifier] : the new location for the object(s)
[withProperties: record] : new values for the properties of the duplicated element

ps.currentDocument.duplicate()
ps.currentDocument.duplicate({})

  • location specifier 位置说明符]

initialFill (“transparent”/‌"use background color"/‌"white", r/o) : initial fill of the document. Only valid when used as an option with the ‘make new document’ command

UXP

Photoshop API

photoshop

const app=require('photoshop').app

actionTree

app.actionTree.forEach((item) => {
  console.log(item.name)
})
// => 默认动作 \(index):1 裁剪图片\设计助理预设动作库

documents

activeDocument

const doc=app.activeDocument

backgroundColor

foregroundColor

currentTool

batchPlay

batchPlay(commands: any, options: any)

bringToFront

createDocument

let newDoc1 = app.createDocument() // creates a 2100px * 1500px document
let newDoc2 = app.createDocument({
  width: 800,
  height: 600,
  resolution: 300,
  mode: 'RGBColorMode',
  fill: 'transparent',
})

let newDoc3 = app.createDocument({ preset: 'My Default Size 1' })

open

ø

// Open a file given entry
let entry = await require('uxp').storage.localFileSystem.getFileForOpening()
const document = await app.open(entry)

// Show open file dialog
const document = await app.open()

showAlert

需要注意:传入参数必须是字符串,否则不会生效,也不会返回错误信息

const ps = require('photoshop').app
ps.showAlert('1')

action

app.actionTrees[0].actions[0].play()

UXP API

全局属性

clipboard

setContent(data)

navigator.clipboard.setContent({"text/plain": "Hello!"});

writeText(text)

navigator.clipboard.writeText('text');

clearContent()

navigator.clipboard.clearContent();

getContent() => text

const res_obj = await navigator.clipboard.getContent()
const res = res_obj['text/plain']
eval(res)

localStorage(数据缓存)

插件重新加载数据依然存在

setItem(key, value)

window.localStorage.setItem('name','uxp')

getItem(key)

window.localStorage.getItem('name')

removeItem(key)

window.localStorage.removeItem('name')

clear()

window.localStorage.clear()

window.sessionStorage(插件数据存储)

在插件生命周期内存在,插件卸载之后,数据会一同被清理,使用方法同 localStorage 一致

reload()

window.location.reload()

重新加载窗口

模块

OS

const os = require('os')
os.platform()

platform()

获得当前操作系统的名称,如:“win10”, “darwin”

release()

获得系统版本号,如:20.2.0

arch()

获得系统的架构,如:x64

shell

openExternal(url)

用于打开链接(url)

require('uxp').shell
shell.openExternal('https://www.baidu.com')

uxp

video

Adobe UXP: Things you need to know! #4 Panels vs. Commands and the manifest.json - YouTube

Installing Photoshop 2021 UXP Plugins - YouTube

 类似资料: