photoshop-script-api
中文
About
When develop CEP Panels in Photoshop, we use Javascript DOM API to communicate with the host, It's easily understood and used, but insufficient interfaces offered to accomplish complex works. There is another way call "Action Descriptor" which is more powerful and comprehensive, while extremely confusing to learn and use.
This project wraps the AM codes and build a friendly use js api to offer powerful and sufficient interfaces for Photoshop plugin development.
install
This project is written in TypeScript. If you use ts in your codes, just clone this project and import the source code as you want.
Typescript
import the index.ts file in your project
import { Document } from "./photoshop-script-api/src/index";
const doc = new Document();
$.writeln(doc.name());
Javascript
In plain javascript way, you can install the npm module which has been compiled and published.
install the module from npm
npm install photoshop-script-api
include the main.js file in your code
#include "./node_modules/photoshop-script-api/dist/main.js"
var a = new $.Application();
alert(a.version());
usages
Classes
This module includes some Classes listed below
- Application
- Document
- Layer
- Artboard
- History
- Rect
- Selection
- Shape
- Size
- Color
- Stroke
- MetaData
- ...
more will be added in future
in order to avoid namespace conflict, all class names are prefixed with "$", so you can use them like this
var app = new $.Application();
each Class offers some useful api to make things happen, you can check out the code to get more detail.
below snippets are some simple examples to demonstrate how to use the api
Application
Application indicates the Photoshop application itself, it offers some useful api to get the application information
var theApp = new $.Application();
theApp.open("/path/to/a/file");
theApp.getHostVersion();
theApp.getApplicationPath();
Documents
Document indicates the opened document in Photoshop, it offers some useful api to get the document information
var doc = $.Document.activeDocument();
if (doc == null) {
alert("no doucment opened");
return;
}
alert(doc.name());
$.writeln(doc.length());
doc.trim();
Layer
Layer indicates the layer in Photoshop, it helps you to manipulate the layer easily.
var layers = $.Layer.getSelectedLayers();
for (var i=0; i<layers.length; i++) {
var layer = layers[i];
$.writeln(layer.name());
$.writeln(layer.index());
}
var layer = $.Layer.getLayerByIndex(10);
layer.setName('an awesome name');
var bounds = layer.getBounds();
var size = bounds.size();
$.writeln(size.toString());
layer.hide();
layer.show();
layer.select();
layer.toSelection();
layer.rasterize();
layer.selct().toSelection().hide();
Selection
Selection indicates the selection in Photoshop. You can create or get the selection and manipulate it.
var bounds = new $.Rect(100, 100, 100, 100);
var selection = new $.Selection(bounds);
selection.create();
Canvas
Canvas makes easy to draw shapes in document
var canvas = new $.Canvas();
var circle = new $.Circle(new Point(100, 100), 50);
var rect = new $.Rect(100, 100, 100, 100);
var line = new $.Line(new Point(100, 100), new Point(200, 200));
canvas.add(circle);
canvas.add(rect);
canvas.add(line);
canvas.setFillColor($.SolidColor.fromHexString("#ff5c5c"));
canvas.paint();
Guide
Guide wraps the guide api in Photoshop, it helps you to create or remove guides easily.
$.Guide.add({position: 10, direction: 'horizontal'});
var guides = $.Guide.all();
for (var i=0; i<guides.length; i++) {
var guide = guides[i];
$.writeln(guide.position());
$.writeln(guide.direction());
}
History
History offers some useful api to manipulate the history stack in Photoshop.
History.previous();
History.go(3);
History.saveState();
History.restoreState();
other classes are similar to use, you can check out the code to get more detail.
if any questions, please post an issue.
About Me
Ten years Software Engineer from China, former Senior Engineer of Baidu Inc. Head in web, mobile, media development, in love with design. Have several welcome products for designers in China.
Design Mirror - The best preview tool
Cutterman - Assets expoter