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 ¶
- func ApplyDataset(name string) error
- func Close(save SaveOption) error
- func DoAction(set, action string) error
- func DoJS(path string, args ...string) ([]byte, error)
- func Init() error
- func JSLayer(path string) string
- func JSLayerMerge(path string) string
- func Quit(save SaveOption) error
- func SaveAs(path string) error
- func Wait(msg string)
- type ArtLayer
- func (a *ArtLayer) Bounds() [2][2]int
- func (a *ArtLayer) MarshalJSON() ([]byte, error)
- func (a *ArtLayer) Name() string
- func (a *ArtLayer) Parent() Group
- func (a *ArtLayer) Path() string
- func (a *ArtLayer) Refresh() error
- func (a *ArtLayer) SetActive() ([]byte, error)
- func (a *ArtLayer) SetColor(c Color)
- func (a *ArtLayer) SetParent(c Group)
- func (a *ArtLayer) SetPos(x, y int, bound string)
- func (a *ArtLayer) SetStroke(stk Stroke, fill Color)
- func (a *ArtLayer) SetVisible(b bool) error
- func (a *ArtLayer) UnmarshalJSON(b []byte) error
- func (a *ArtLayer) Visible() bool
- func (a *ArtLayer) X1() int
- func (a *ArtLayer) X2() int
- func (a *ArtLayer) Y1() int
- func (a *ArtLayer) Y2() int
- type ArtLayerJSON
- type Color
- type Document
- func (d *Document) ArtLayer(name string) *ArtLayer
- func (d *Document) ArtLayers() []*ArtLayer
- func (d *Document) Dump()
- func (d *Document) DumpFile() string
- func (d *Document) FullName() string
- func (d *Document) Height() int
- func (d *Document) LayerSet(name string) *LayerSet
- func (d *Document) LayerSets() []*LayerSet
- func (d *Document) MarshalJSON() ([]byte, error)
- func (d *Document) MustExist(name string) Layer
- func (d *Document) Name() string
- func (d *Document) Parent() Group
- func (d *Document) Path() string
- func (d *Document) Restore(path string) error
- func (d *Document) Save() error
- func (d *Document) SetParent(g Group)
- func (d *Document) UnmarshalJSON(b []byte) error
- type DocumentJSON
- type Group
- type Hex
- type Layer
- type LayerSet
- func (l *LayerSet) ArtLayer(name string) *ArtLayer
- func (l *LayerSet) ArtLayers() []*ArtLayer
- func (l LayerSet) Bounds() [2][2]int
- func (l *LayerSet) LayerSet(name string) *LayerSet
- func (l *LayerSet) LayerSets() []*LayerSet
- func (l *LayerSet) MarshalJSON() ([]byte, error)
- func (l *LayerSet) MustExist(name string) Layer
- func (l LayerSet) Name() string
- func (l *LayerSet) Parent() Group
- func (l *LayerSet) Path() string
- func (l *LayerSet) Refresh() error
- func (l *LayerSet) SetParent(g Group)
- func (l *LayerSet) SetPos(x, y int, bound string)
- func (l *LayerSet) SetVisible(b bool) error
- func (l *LayerSet) UnmarshalJSON(b []byte) error
- func (l LayerSet) Visible() bool
- type LayerSetJSON
- type ModeEnum
- type RGB
- type SaveOption
- type Stroke
- type TextItem
- func (t TextItem) Contents() string
- func (t *TextItem) Fmt(start, end int, font, style string)
- func (t *TextItem) MarshalJSON() ([]byte, error)
- func (t *TextItem) SetSize(s float64)
- func (t *TextItem) SetText(txt string)
- func (t TextItem) Size() float64
- func (t *TextItem) UnmarshalJSON(data []byte) error
- type TextItemJSON
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyDataset ¶
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 DoJS ¶
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 ¶
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 ¶
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.
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 ¶
Bounds returns the coordinates of the corners of the ArtLayer's bounding box.
func (*ArtLayer) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, allowing the ArtLayer to be saved to disk in JSON format.
func (*ArtLayer) SetActive ¶
SetActive makes this layer active in Photoshop. Layers need to be active to perform certain operations
func (*ArtLayer) SetPos ¶
SetPos snaps the given layer boundary to the given point. Valid options for bound are: "TL", "TR", "BL", "BR"
func (*ArtLayer) SetStroke ¶
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 ¶
SetVisible makes the layer visible.
func (*ArtLayer) UnmarshalJSON ¶
UnmarshalJSON loads json data into the object.
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.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a Photoshop document (PSD file).
func ActiveDocument ¶
ActiveDocument returns document currently focused in Photoshop.
TODO(sbrow): Reduce cyclomatic complexity of ActiveDocument().
func Open ¶
Open opens a Photoshop document with the specified path. If Photoshop is not currently running, it is started before opening the document.
func (*Document) DumpFile ¶
DumpFile returns the path to the json file where this document's data gets dumped. See Document.Dump
func (*Document) MarshalJSON ¶
MarshalJSON returns the Document in JSON format.
func (*Document) MustExist ¶
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) SetParent ¶
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 ¶
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.
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 ¶
NewLayerSet grabs the LayerSet with the given path and returns it.
func (*LayerSet) MarshalJSON ¶
MarshalJSON returns the LayerSet in JSON form.
func (*LayerSet) MustExist ¶
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) SetPos ¶
SetPos snaps the given layerset boundary to the given point. Valid options for bound are: "TL", "TR", "BL", "BR"
func (*LayerSet) SetVisible ¶
SetVisible makes the LayerSet visible.
func (*LayerSet) UnmarshalJSON ¶
UnmarshalJSON loads the json data into this LayerSet.
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 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 TextItem ¶
type TextItem struct {
// contains filtered or unexported fields
}
TextItem holds the text element of a TextLayer.
func (*TextItem) Fmt ¶
Fmt applies the given font and style to all characters in the range [start, end].
func (*TextItem) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface, allowing the TextItem to be saved to disk in JSON format.
func (*TextItem) UnmarshalJSON ¶
UnmarshalJSON loads the JSON data into the TextItem
type TextItemJSON ¶
TextItemJSON is the exported version of TextItem that allows it to be marshaled and unmarshaled into JSON.