photoshop-script-api
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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

  1. Application
  2. Document
  3. Layer
  4. Artboard
  5. History
  6. Rect
  7. Selection
  8. Shape
  9. Size
  10. Color
  11. Stroke
  12. MetaData
  13. ...

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();

// open a file
theApp.open("/path/to/a/file");

// get the host version
theApp.getHostVersion();    // CC2022

// get the host installation path
theApp.getApplicationPath();       // /Applications/Adobe Photoshop 2022/Adobe Photoshop 2022.app

Documents

Document indicates the opened document in Photoshop, it offers some useful api to get the document information

// get current active document
var doc = $.Document.activeDocument();
if (doc == null) {
    alert("no doucment opened");
    return;
}
alert(doc.name());  // alert document name
$.writeln(doc.length());    // document size in bytes
doc.trim(); // trim document transparent area

Layer

Layer indicates the layer in Photoshop, it helps you to manipulate the layer easily.

// get selected layers
var layers = $.Layer.getSelectedLayers();
for (var i=0; i<layers.length; i++) {
    var layer = layers[i];
    $.writeln(layer.name());    // layer name
    $.writeln(layer.index());    // layer index
}

var layer = $.Layer.getLayerByIndex(10);
layer.setName('an awesome name');   // set a new name for layer
var bounds = layer.getBounds();   // get the postion & size of the layer
var size = bounds.size();
$.writeln(size.toString());  // 200 x 100  ( output layer size)

layer.hide();   // hide the layer
layer.show();   // show the layer
layer.select(); // set select the layer
layer.toSelection();    // create a selection with the layer bounds
layer.rasterize();    // rasterize the layer

// you can also use codes like jQuery
layer.selct().toSelection().hide();

Selection

Selection indicates the selection in Photoshop. You can create or get the selection and manipulate it.

// create a selection
var bounds = new $.Rect(100, 100, 100, 100);
var selection = new $.Selection(bounds);
selection.create();

Canvas

Canvas makes easy to draw shapes in document

// we create a canvas to hold shapes
var canvas = new $.Canvas();

// create a circle
var circle = new $.Circle(new Point(100, 100), 50);
// create a rectangle
var rect = new $.Rect(100, 100, 100, 100);
// create a line
var line = new $.Line(new Point(100, 100), new Point(200, 200));
// add shappe to canvas
canvas.add(circle);
canvas.add(rect);
canvas.add(line);

// we set the color of the shape
canvas.setFillColor($.SolidColor.fromHexString("#ff5c5c"));
// we paint
canvas.paint();

Guide

Guide wraps the guide api in Photoshop, it helps you to create or remove guides easily.

// create a guide
$.Guide.add({position: 10, direction: 'horizontal'});
// get all guides
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.

// step backword
History.previous();
// go to the history state
History.go(3);

// save the current history state
// and do your stuff, then restore the history state
History.saveState();
// do your stuff here...
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

Readme

Keywords

none

Package Sidebar

Install

npm i photoshop-script-api

Weekly Downloads

0

Version

1.0.4

License

ISC

Unpacked Size

460 kB

Total Files

37

Last publish

Collaborators

  • cutterman