mirror of
https://github.com/sbrow/ps.git
synced 2025-12-29 18:47:38 -05:00
Added functionality
lib - Save - DoAction - ApplyDataset - GetLayer(s) main - do actions from commandline Improved test cases!
This commit is contained in:
90
ps.go
90
ps.go
@@ -1,11 +1,14 @@
|
||||
// Package ps creates an interface between Adobe Photoshop (CS5) and go.
|
||||
// This is primarily done by calling VBS/Applescript files.
|
||||
// Package ps is a rudimentary API between Adobe Photoshop and go.
|
||||
//
|
||||
// Currently only works on windows.
|
||||
// Most of the interaction between the two is implemented via
|
||||
// javascript and/or VBS/Applescript.
|
||||
//
|
||||
// Currently only works with CS5 on Windows.
|
||||
package ps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -32,25 +35,25 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// Open photoshop.
|
||||
// Start opens Photoshop.
|
||||
func Start() error {
|
||||
_, err := run("start")
|
||||
return err
|
||||
}
|
||||
|
||||
// Open a file.
|
||||
func Open(path string) error {
|
||||
_, err := run("open", path)
|
||||
return err
|
||||
}
|
||||
|
||||
// Close the active document.
|
||||
// Close closes the active document.
|
||||
func Close() error {
|
||||
_, err := run("close")
|
||||
return err
|
||||
}
|
||||
|
||||
// Quits photoshop.
|
||||
// Open opens a file with the specified path.
|
||||
func Open(path string) error {
|
||||
_, err := run("open", path)
|
||||
return err
|
||||
}
|
||||
|
||||
// Quit exits Photoshop.
|
||||
//
|
||||
// There are 3 valid values for save: 1 (psSaveChanges), 2 (psDoNotSaveChanges),
|
||||
// 3 (psPromptToSaveChanges).
|
||||
@@ -59,9 +62,14 @@ func Quit(save int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func Js(path string, args ...string) ([]byte, error) {
|
||||
// DoJs runs a Photoshop javascript script file (.jsx) from the specified location.
|
||||
// It can't directly return output, so instead the scripts write their output to
|
||||
// a temporary file.
|
||||
func DoJs(path string, args ...string) ([]byte, error) {
|
||||
// Temp file for js to output to.
|
||||
outpath := filepath.Join(os.Getenv("TEMP"), "js_out.txt")
|
||||
defer os.Remove(outpath)
|
||||
|
||||
args = append([]string{outpath}, args...)
|
||||
|
||||
// If passed a script by name, assume it's in the default folder.
|
||||
@@ -76,28 +84,30 @@ func Js(path string, args ...string) ([]byte, error) {
|
||||
}
|
||||
file, err := ioutil.ReadFile(outpath)
|
||||
if err != nil {
|
||||
// fmt.Println(cmd)
|
||||
return cmd, err
|
||||
}
|
||||
cmd = append(cmd, file...)
|
||||
// os.Remove(outpath)
|
||||
return cmd, err
|
||||
}
|
||||
|
||||
// Wait provides the user a message, and halts operation until the user
|
||||
// Wait prints a message to the console and halts operation until the user
|
||||
// signals that they are ready (by pushing enter).
|
||||
//
|
||||
// Useful for when you need to do something by hand in the middle of an
|
||||
// automated process.
|
||||
// otherwise automated process.
|
||||
func Wait(msg string) {
|
||||
fmt.Println()
|
||||
fmt.Print(msg)
|
||||
var input string
|
||||
fmt.Scanln(&input)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// run handles running the script files, returning output, and displaying errors.
|
||||
func run(name string, args ...string) ([]byte, error) {
|
||||
var ext string
|
||||
var out bytes.Buffer
|
||||
var errs bytes.Buffer
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
@@ -117,14 +127,52 @@ func run(name string, args ...string) ([]byte, error) {
|
||||
} else {
|
||||
args = append([]string{Opts, filepath.Join(pkgpath, "scripts", name)}, args...)
|
||||
}
|
||||
|
||||
cmd := exec.Command(Cmd, args...)
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
cmd.Stderr = &errs
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return []byte{}, errors.New(string(out.Bytes()))
|
||||
} else {
|
||||
return out.Bytes(), err
|
||||
// return append(out.Bytes(), errs.Bytes()...), err
|
||||
}
|
||||
if len(errs.Bytes()) != 0 {
|
||||
return out.Bytes(), errors.New(string(errs.Bytes()))
|
||||
}
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
// DoAction runs a Photoshop action with name from set.
|
||||
func DoAction(set, name string) error {
|
||||
_, err := run("action", set, name)
|
||||
return err
|
||||
}
|
||||
|
||||
// SaveAs saves the Photoshop document file to the given location.
|
||||
func SaveAs(path string) error {
|
||||
_, err := run("save", path)
|
||||
return err
|
||||
}
|
||||
|
||||
// Layers returns an array of ArtLayers from the active document
|
||||
// based on the given path string.
|
||||
func Layers(path string) ([]ArtLayer, error) {
|
||||
byt, err := DoJs("getLayers.jsx", path)
|
||||
var out []ArtLayer
|
||||
err = json.Unmarshal(byt, &out)
|
||||
if err != nil {
|
||||
return []ArtLayer{}, err
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Layer returns an ArtLayer from the active document given a specified
|
||||
// path string. Layer calls Layers() and returns the first result.
|
||||
func Layer(path string) (ArtLayer, error) {
|
||||
lyrs, err := Layers(path)
|
||||
return lyrs[0], err
|
||||
}
|
||||
|
||||
// ApplyDataset fills out a template file with information from a given dataset (csv) file.
|
||||
func ApplyDataset(name string) ([]byte, error) {
|
||||
return DoJs("applyDataset.jsx", name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user