ps

package module
v2.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2018 License: GPL-3.0 Imports: 12 Imported by: 1

README

logo

ps

GoDoc Build Status Coverage Status Go Report Card

import "github.com/sbrow/ps"

Overview

Package ps is a rudimentary API between Adobe Photoshop CS5 and Golang. The interaction between the two is implemented using Javascript/VBScript.

Use it to control Photoshop, edit documents, and perform batch operations.

Currently only supports Photoshop CS5 Windows x86_64.

Installation

$ go get -u github.com/sbrow/ps

TODO

sbrow: (2) Make TextLayer a subclass of ArtLayer.

sbrow: Reduce cyclomatic complexity of ActiveDocument().

sbrow: refactor Close to Document.Close

Documentation

For full Documentation please visit https://godoc.org/github.com/sbrow/ps


Generated by godoc2md

Documentation

Overview

Package ps is a rudimentary API between Adobe Photoshop CS5 and Golang. The interaction between the two is implemented using Javascript/VBScript.

Use it to control Photoshop, edit documents, and perform batch operations.

Currently only supports Photoshop CS5 Windows x86_64.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDataset

func ApplyDataset(name string) error

ApplyDataset fills out a template file with information from a given dataset (csv) file. It's important to note that running this function will change data in the Photoshop document, but will not update data in the Go Document struct- you will have to implement syncing them yourself.

func Close

func Close(save SaveOption) error

Close closes the active document in Photoshop, using the given save option. TODO(sbrow): refactor Close to Document.Close

func DoAction

func DoAction(set, action string) error

DoAction runs the Photoshop Action with name 'action' from ActionSet 'set'.

func DoJS

func DoJS(path string, args ...string) ([]byte, error)

DoJS runs a Photoshop Javascript script file (.jsx) from the specified location. The script can't directly return output, so instead it writes output to a temporary file ($TEMP/js_out.txt), whose contents is then read and returned.

func Init

func Init() error

Init opens Photoshop if it is not open already.

Init should be called before all other

func JSLayer

func JSLayer(path string) string

JSLayer "compiles" Javascript code to get an ArtLayer with the given path. The output always ends with a semicolon, so if you want to access a specific property of the layer, you'll have to trim the output before concatenating.

Example
// The path of a layer inside a top level group.
path := "Group 1/Layer 1"
fmt.Println(JSLayer(path))
Output:

app.activeDocument.layerSets.getByName('Group 1').artLayers.getByName('Layer 1')

func JSLayerMerge

func JSLayerMerge(path string) string

JSLayerMerge gets the Javascript code to get the Layer or LayerSet with this path and returns the result if you were to merge the bottom-most LayerSet.

If the bottom-most Object in the path is not a LayerSet, it will returns the same results as JSLayer.

func Quit

func Quit(save SaveOption) error

Quit exits Photoshop, closing all open documents using the given save option.

func SaveAs

func SaveAs(path string) error

SaveAs saves the Photoshop document to the given location. // TODO(sbrow): doesn't return error on non-existant path.

func Wait

func Wait(msg string)

Wait prints a message to the console and halts operation until the user signals that they are ready to continue (by pushing enter).

Useful for when you need to do something by hand in the middle of an otherwise automated process, (e.g. importing a dataset).

Types

type ArtLayer

type ArtLayer struct {
	Color     Color   // The layer's color overlay effect (if any).
	Stroke    *Stroke // The layer's stroke effect (if any).
	*TextItem         // The layer's text, if it's a text layer.
	// contains filtered or unexported fields
}

ArtLayer reflects some values from an Art Layer in a Photoshop document.

TODO(sbrow): (2) Make TextLayer a subclass of ArtLayer.

func (*ArtLayer) Bounds

func (a *ArtLayer) Bounds() [2][2]int

Bounds returns the coordinates of the corners of the ArtLayer's bounding box.

func (*ArtLayer) MarshalJSON

func (a *ArtLayer) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface, allowing the ArtLayer to be saved to disk in JSON format.

func (*ArtLayer) Name

func (a *ArtLayer) Name() string

Name returns the layer's name.

func (*ArtLayer) Parent

func (a *ArtLayer) Parent() Group

Parent returns the Document or LayerSet this layer is contained in.

func (*ArtLayer) Path

func (a *ArtLayer) Path() string

Path returns the Path to this layer, through all of its parents.

func (*ArtLayer) Refresh

func (a *ArtLayer) Refresh() error

Refresh syncs the layer with Photoshop.

func (*ArtLayer) SetActive

func (a *ArtLayer) SetActive() ([]byte, error)

SetActive makes this layer active in Photoshop. Layers need to be active to perform certain operations

func (*ArtLayer) SetColor

func (a *ArtLayer) SetColor(c Color)

SetColor creates a color overlay for the layer

func (*ArtLayer) SetParent

func (a *ArtLayer) SetParent(c Group)

SetParent sets Group c to be the group that holds this layer.

func (*ArtLayer) SetPos

func (a *ArtLayer) SetPos(x, y int, bound string)

SetPos snaps the given layer boundary to the given point. Valid options for bound are: "TL", "TR", "BL", "BR"

func (*ArtLayer) SetStroke

func (a *ArtLayer) SetStroke(stk Stroke, fill Color)

SetStroke edits the "aura" around the layer. If a nil stroke is given, The current stroke is removed. If a non-nil stroke is given and the current stroke is nil, a stroke is added.

func (*ArtLayer) SetVisible

func (a *ArtLayer) SetVisible(b bool) error

SetVisible makes the layer visible.

func (*ArtLayer) UnmarshalJSON

func (a *ArtLayer) UnmarshalJSON(b []byte) error

UnmarshalJSON loads json data into the object.

func (*ArtLayer) Visible

func (a *ArtLayer) Visible() bool

Visible returns whether or not the layer is currently hidden.

func (*ArtLayer) X1

func (a *ArtLayer) X1() int

X1 returns the layer's leftmost x value.

func (*ArtLayer) X2

func (a *ArtLayer) X2() int

X2 returns the layer's rightmost x value.

func (*ArtLayer) Y1

func (a *ArtLayer) Y1() int

Y1 returns the layer's topmost y value.

func (*ArtLayer) Y2

func (a *ArtLayer) Y2() int

Y2 returns the layer's bottommost y value.

type ArtLayerJSON

type ArtLayerJSON struct {
	Name      string
	Bounds    [2][2]int
	Color     [3]int
	Stroke    [3]int
	StrokeAmt float32
	Visible   bool
	TextItem  *TextItem
}

ArtLayerJSON is a bridge between the ArtLayer struct and the encoding/json package, allowing ArtLayer's unexported fields to ber written to and read from by the json package.

type Color

type Color interface {
	RGB() [3]int  // The color in RGB format.
	Hex() []uint8 // The color in hexadecimal format.
}

Color is an interface for color objects, allowing colors to be used in various formats.

RGB is the default format for everything.

var (
	ColorBlack Color = RGB{0, 0, 0}
	ColorGray  Color = RGB{128, 128, 128}
	ColorWhite Color = RGB{255, 255, 255}
)

Some basic colors.

func Compare

func Compare(a, b Color) Color

Compare returns the brighter of a and b.

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document represents a Photoshop document (PSD file).

func ActiveDocument

func ActiveDocument() (*Document, error)

ActiveDocument returns document currently focused in Photoshop.

TODO(sbrow): Reduce cyclomatic complexity of ActiveDocument().

func Open

func Open(path string) (*Document, error)

Open opens a Photoshop document with the specified path. If Photoshop is not currently running, it is started before opening the document.

func (*Document) ArtLayer

func (d *Document) ArtLayer(name string) *ArtLayer

ArtLayer returns the first top level ArtLayer matching the given name.

func (*Document) ArtLayers

func (d *Document) ArtLayers() []*ArtLayer

ArtLayers returns this document's ArtLayers, if any.

func (*Document) Dump

func (d *Document) Dump()

Dump saves the document to disk in JSON format.

func (*Document) DumpFile

func (d *Document) DumpFile() string

DumpFile returns the path to the json file where this document's data gets dumped. See Document.Dump

func (*Document) FullName

func (d *Document) FullName() string

FullName returns the absolute path to the current document file.

func (*Document) Height

func (d *Document) Height() int

Height returns the height of the document, in pixels.

func (*Document) LayerSet

func (d *Document) LayerSet(name string) *LayerSet

LayerSet returns the first top level LayerSet matching the given name.

func (*Document) LayerSets

func (d *Document) LayerSets() []*LayerSet

LayerSets returns all the document's top level LayerSets.

func (*Document) MarshalJSON

func (d *Document) MarshalJSON() ([]byte, error)

MarshalJSON returns the Document in JSON format.

func (*Document) MustExist

func (d *Document) MustExist(name string) Layer

MustExist returns a Layer from the set with the given name, and panics if it doesn't exist.

If there is a LayerSet and an ArtLayer with the same name, it will return the LayerSet.

func (*Document) Name

func (d *Document) Name() string

Name returns the document's title. This fulfills the Group interface.

func (*Document) Parent

func (d *Document) Parent() Group

Parent returns the Group that contains d.

func (*Document) Path

func (d *Document) Path() string

Path returns the root path ("") for all the layers.

func (*Document) Restore

func (d *Document) Restore(path string) error

Restore loads document data from a JSON file.

func (*Document) Save

func (d *Document) Save() error

Save saves the Document in place.

func (*Document) SetParent

func (d *Document) SetParent(g Group)

SetParent does nothing, as the document is a top-level object and therefore can't have a parent group. The function is needed to implement the group interface.

func (*Document) UnmarshalJSON

func (d *Document) UnmarshalJSON(b []byte) error

UnmarshalJSON loads JSON data into this Document.

type DocumentJSON

type DocumentJSON struct {
	Name      string
	FullName  string
	Height    int
	Width     int
	ArtLayers []*ArtLayer
	LayerSets []*LayerSet
}

DocumentJSON is an exported version of Document that allows Documents to be saved to and loaded from JSON.

type Group

type Group interface {
	Name() string
	Parent() Group
	SetParent(Group)
	Path() string
	ArtLayer(name string) *ArtLayer
	LayerSet(name string) *LayerSet
	ArtLayers() []*ArtLayer
	LayerSets() []*LayerSet
	MustExist(name string) Layer
	MarshalJSON() ([]byte, error)
	UnmarshalJSON(b []byte) error
}

Group represents a Document or LayerSet.

type Hex

type Hex []uint8

Hex is a color in hexadecimal format. It satisfies the Color interface.

func (Hex) Hex

func (h Hex) Hex() []uint8

Hex returns the hex value of the number, to satisfy the Color interface.

func (Hex) RGB

func (h Hex) RGB() [3]int

RGB returns the Hex value converted to RGB

type Layer

type Layer interface {
	Bounds() [2][2]int
	MarshalJSON() ([]byte, error)
	Name() string
	Parent() Group
	Path() string
	Refresh() error
	SetParent(g Group)
	SetPos(x, y int, bound string)
	SetVisible(b bool) error
	UnmarshalJSON(b []byte) error
	Visible() bool
}

Layer represents an ArtLayer or LayerSet Object.

type LayerSet

type LayerSet struct {
	// contains filtered or unexported fields
}

LayerSet holds a group of Layer objects and a group of LayerSet objects.

func NewLayerSet

func NewLayerSet(path string, g Group) (*LayerSet, error)

NewLayerSet grabs the LayerSet with the given path and returns it.

func (*LayerSet) ArtLayer

func (l *LayerSet) ArtLayer(name string) *ArtLayer

ArtLayer returns the first top level ArtLayer matching the given name.

func (*LayerSet) ArtLayers

func (l *LayerSet) ArtLayers() []*ArtLayer

ArtLayers returns the LayerSet's ArtLayers.

func (LayerSet) Bounds

func (l LayerSet) Bounds() [2][2]int

Bounds returns the furthest corners of the LayerSet.

func (*LayerSet) LayerSet

func (l *LayerSet) LayerSet(name string) *LayerSet

LayerSet returns the first top level LayerSet matching the given name.

func (*LayerSet) LayerSets

func (l *LayerSet) LayerSets() []*LayerSet

LayerSets returns the LayerSets contained within this set.

func (*LayerSet) MarshalJSON

func (l *LayerSet) MarshalJSON() ([]byte, error)

MarshalJSON returns the LayerSet in JSON form.

func (*LayerSet) MustExist

func (l *LayerSet) MustExist(name string) Layer

MustExist returns a Layer from the set with the given name, and panics if it doesn't exist.

If there is a LayerSet and an ArtLayer with the same name, it will return the LayerSet.

func (LayerSet) Name

func (l LayerSet) Name() string

Name returns the name of the LayerSet

func (*LayerSet) Parent

func (l *LayerSet) Parent() Group

Parent returns this layerSet's parent.

func (*LayerSet) Path

func (l *LayerSet) Path() string

Path returns the layer path to this Set.

func (*LayerSet) Refresh

func (l *LayerSet) Refresh() error

Refresh syncs the LayerSet with Photoshop.

func (*LayerSet) SetParent

func (l *LayerSet) SetParent(g Group)

SetParent puts this LayerSet into the given group.

func (*LayerSet) SetPos

func (l *LayerSet) SetPos(x, y int, bound string)

SetPos snaps the given layerset boundary to the given point. Valid options for bound are: "TL", "TR", "BL", "BR"

func (*LayerSet) SetVisible

func (l *LayerSet) SetVisible(b bool) error

SetVisible makes the LayerSet visible.

func (*LayerSet) UnmarshalJSON

func (l *LayerSet) UnmarshalJSON(b []byte) error

UnmarshalJSON loads the json data into this LayerSet.

func (LayerSet) Visible

func (l LayerSet) Visible() bool

Visible returns whether or not the LayerSet is currently visible.

type LayerSetJSON

type LayerSetJSON struct {
	Name      string
	Bounds    [2][2]int
	Visible   bool
	ArtLayers []*ArtLayer
	LayerSets []*LayerSet
}

LayerSetJSON is an exported version of LayerSet, that allows LayerSets to be saved to and loaded from JSON.

type ModeEnum

type ModeEnum int

ModeEnum determines how aggressively the package will attempt to sync with Photoshop. Loading Photoshop files from scratch takes a long time, so the package saves the state of the document in a JSON file in the /data folder whenever you call Document.Dump(). ModeEnum tells the program how trustworthy that file is.

const Fast ModeEnum = 2

Fast mode skips all verification. Use Fast mode only when certain that the .psd file hasn't changed since the last time Document.Dump() was called.

const Normal ModeEnum = 0

Normal Mode only verifies layers as they are operated on. The first time a layer's properties would be checked, it first overwrites the data from the Dump with data pulled directly from Photoshop. This allows you to quickly load documents in their current form.

const Safe ModeEnum = 1

Safe Mode always loads the document from scratch, ignoring any dumped data. (Very Slow). If a function panics due to outdated data, often times re-running the function in safe mode is enough to remediate it.

var Mode ModeEnum

Mode holds the current mode.

type RGB

type RGB struct {
	Red   int
	Green int
	Blue  int
}

RGB is a color format. It implements the Color interface.

func (RGB) Hex

func (r RGB) Hex() []uint8

Hex returns the color converted to hexadecimal format.

func (RGB) RGB

func (r RGB) RGB() [3]int

RGB returns the color in RGB format.

type SaveOption

type SaveOption int

SaveOption is an enum for options when closing a document.

const DoNotSaveChanges SaveOption = 2

DoNotSaveChanges Closes documents without saving.

const PromptToSaveChanges SaveOption = 3

PromptToSaveChanges prompts the user whether the file should be saved before closing.

const SaveChanges SaveOption = 1

SaveChanges Saves changes before closing documents.

type Stroke

type Stroke struct {
	Size float32
	Color
}

Stroke represents a layer stroke effect.

type TextItem

type TextItem struct {
	// contains filtered or unexported fields
}

TextItem holds the text element of a TextLayer.

func (TextItem) Contents

func (t TextItem) Contents() string

Contents returns the raw text of the TextItem.

func (*TextItem) Fmt

func (t *TextItem) Fmt(start, end int, font, style string)

Fmt applies the given font and style to all characters in the range [start, end].

func (*TextItem) MarshalJSON

func (t *TextItem) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface, allowing the TextItem to be saved to disk in JSON format.

func (*TextItem) SetSize

func (t *TextItem) SetSize(s float64)

SetSize sets the size of the TextItem's font.

func (*TextItem) SetText

func (t *TextItem) SetText(txt string)

SetText sets the text to the given string.

func (TextItem) Size

func (t TextItem) Size() float64

Size returns the font size of the TextItem

func (*TextItem) UnmarshalJSON

func (t *TextItem) UnmarshalJSON(data []byte) error

UnmarshalJSON loads the JSON data into the TextItem

type TextItemJSON

type TextItemJSON struct {
	Contents string
	Size     float64
	Font     string
}

TextItemJSON is the exported version of TextItem that allows it to be marshaled and unmarshaled into JSON.

Directories

Path Synopsis
Package runner runs the non-go code that Photoshop understands, and passes it to back to the go program.
Package runner runs the non-go code that Photoshop understands, and passes it to back to the go program.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL