mirror of
https://github.com/sbrow/ps.git
synced 2025-12-29 18:47:38 -05:00
Got text working.
This commit is contained in:
43
Variables.go
Normal file
43
Variables.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package ps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Colors map[string]Color = map[string]Color{
|
||||||
|
"Gray": &RGB{128, 128, 128},
|
||||||
|
"White": &RGB{255, 255, 255},
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModeEnum determines how aggressively the package will attempt to sync with Photoshop.
|
||||||
|
type ModeEnum int
|
||||||
|
|
||||||
|
// Holds the current mode.
|
||||||
|
var Mode ModeEnum
|
||||||
|
|
||||||
|
// Fast mode never checks layers before returning.
|
||||||
|
const Fast ModeEnum = 2
|
||||||
|
|
||||||
|
// Normal Mode Always checks to see if layers are up to date
|
||||||
|
// before returning them.
|
||||||
|
const Normal ModeEnum = 0
|
||||||
|
|
||||||
|
// Safe Mode Always loads the document from scratch. (Very Slow)
|
||||||
|
const Safe ModeEnum = 1
|
||||||
|
|
||||||
|
// PSSaveOptions is an enum for options when closing a document.
|
||||||
|
type PSSaveOptions int
|
||||||
|
|
||||||
|
func (p *PSSaveOptions) String() string {
|
||||||
|
return fmt.Sprint("", *p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PSSaveChanges saves changes before closing documents.
|
||||||
|
const PSSaveChanges PSSaveOptions = 1
|
||||||
|
|
||||||
|
// PSDoNotSaveChanges closes documents without saving.
|
||||||
|
const PSDoNotSaveChanges PSSaveOptions = 2
|
||||||
|
|
||||||
|
// PSPromptToSaveChanges prompts the user whether to save each
|
||||||
|
// document before closing it.
|
||||||
|
const PSPromptToSaveChanges PSSaveOptions = 3
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/sbrow/ps"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := []string{}
|
|
||||||
cmd := ""
|
|
||||||
switch {
|
|
||||||
case len(os.Args) > 1:
|
|
||||||
args = os.Args[2:]
|
|
||||||
fallthrough
|
|
||||||
case len(os.Args) > 0:
|
|
||||||
cmd = os.Args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(os.Args, cmd, args)
|
|
||||||
if cmd == "action" {
|
|
||||||
err := ps.DoAction(args[0], args[1])
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,11 +4,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Colors map[string]Color = map[string]Color{
|
|
||||||
"Gray": &RGB{128, 128, 128},
|
|
||||||
"White": &RGB{255, 255, 255},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Color is an interface for color objects, allowing colors to be
|
// Color is an interface for color objects, allowing colors to be
|
||||||
// used in various formats.
|
// used in various formats.
|
||||||
//
|
//
|
||||||
|
|||||||
13
ps.go
13
ps.go
@@ -23,19 +23,6 @@ var Cmd string
|
|||||||
var Opts string
|
var Opts string
|
||||||
var pkgpath string
|
var pkgpath string
|
||||||
|
|
||||||
// PSSaveOptions is an enum for options when closing a document.
|
|
||||||
type PSSaveOptions int
|
|
||||||
|
|
||||||
func (p *PSSaveOptions) String() string {
|
|
||||||
return fmt.Sprint("", *p)
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
PSSaveChanges PSSaveOptions = 1
|
|
||||||
PSDoNotSaveChanges PSSaveOptions = 2
|
|
||||||
PSPromptToSaveChanges PSSaveOptions = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, file, _, _ := runtime.Caller(0)
|
_, file, _, _ := runtime.Caller(0)
|
||||||
pkgpath = filepath.Dir(file)
|
pkgpath = filepath.Dir(file)
|
||||||
|
|||||||
@@ -137,6 +137,7 @@ func TestMove(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestActiveDocument(t *testing.T) {
|
func TestActiveDocument(t *testing.T) {
|
||||||
|
Mode = Safe
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("Skipping \"TestDocument\"")
|
t.Skip("Skipping \"TestDocument\"")
|
||||||
}
|
}
|
||||||
@@ -167,6 +168,7 @@ func TestActiveDocument(t *testing.T) {
|
|||||||
}
|
}
|
||||||
s := Stroke{Size: 4, Color: &RGB{0, 0, 0}}
|
s := Stroke{Size: 4, Color: &RGB{0, 0, 0}}
|
||||||
lyr.SetStroke(s, &RGB{128, 128, 128})
|
lyr.SetStroke(s, &RGB{128, 128, 128})
|
||||||
|
d.Dump()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestColor(t *testing.T) {
|
func TestColor(t *testing.T) {
|
||||||
|
|||||||
@@ -10,10 +10,15 @@ function layers(lyrs) {
|
|||||||
var lyr = lyrs[i];
|
var lyr = lyrs[i];
|
||||||
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
|
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
|
||||||
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
||||||
lyr.bounds[3] + ']], "Visible": ' + lyr.visible + '}').replace(/ px/g, ""));
|
lyr.bounds[3] + ']], "Visible": ' + lyr.visible+',"Text":').replace(/ px/g, ""));
|
||||||
if (i+1 != lyrs.length)
|
if (lyr.kind == LayerKind.TEXT)
|
||||||
stdout.write(',');
|
stdout.write('"'+lyr.textItem.contents+'"');
|
||||||
stdout.writeln();
|
else
|
||||||
|
stdout.write("null");
|
||||||
|
stdout.write("}")
|
||||||
|
if (i+1 != lyrs.length)
|
||||||
|
stdout.write(',');
|
||||||
|
stdout.writeln();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
layers(doc.artLayers)
|
layers(doc.artLayers)
|
||||||
|
|||||||
@@ -2,7 +2,13 @@
|
|||||||
|
|
||||||
var stdout = newFile(arguments[0]);
|
var stdout = newFile(arguments[0]);
|
||||||
var lyr = eval(arguments[1]);
|
var lyr = eval(arguments[1]);
|
||||||
stdout.writeln(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' +
|
stdout.write(('{"Name":"' + lyr.name + '","Bounds":[[' + lyr.bounds[0] + ',' +
|
||||||
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
||||||
lyr.bounds[3] + ']],"Visible":' + lyr.visible + '}').replace(/ px/g, ""));
|
lyr.bounds[3] + ']],"Visible":' + lyr.visible+',"Text":').replace(/ px/g, ""));
|
||||||
|
if (lyr.kind == LayerKind.TEXT) {
|
||||||
|
stdout.write('"'+lyr.textItem.contents.replace(/\r/g, "\\r")+'"');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
stdout.write(null)
|
||||||
|
stdout.writeln('}')
|
||||||
stdout.close();
|
stdout.close();
|
||||||
@@ -7,7 +7,12 @@ for (var i = 0; i < set.artLayers.length; i++) {
|
|||||||
var lyr = set.artLayers[i];
|
var lyr = set.artLayers[i];
|
||||||
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
|
stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
|
||||||
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
||||||
lyr.bounds[3] + ']], "Visible": ' + lyr.visible + '}').replace(/ px/g, ""));
|
lyr.bounds[3] + ']], "Visible": ' + lyr.visible + ',"Text":').replace(/ px/g, ""));
|
||||||
|
if (lyr.kind == LayerKind.TEXT)
|
||||||
|
stdout.write('"'+lyr.textItem.contents.replace(/\r/g, "\\r")+'"');
|
||||||
|
else
|
||||||
|
stdout.write("null");
|
||||||
|
stdout.write("}")
|
||||||
if (i != set.artLayers.length - 1)
|
if (i != set.artLayers.length - 1)
|
||||||
stdout.writeln(",");
|
stdout.writeln(",");
|
||||||
}
|
}
|
||||||
|
|||||||
38
structs.go
38
structs.go
@@ -12,21 +12,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModeEnum int
|
|
||||||
|
|
||||||
// Mode determines how aggressively ps will attempt to sync with Photoshop.
|
|
||||||
var Mode ModeEnum
|
|
||||||
|
|
||||||
// Normal Mode Always checks to see if layers are updated
|
|
||||||
// before returning them.
|
|
||||||
const Normal ModeEnum = 0
|
|
||||||
|
|
||||||
// Safe Mode Always loads the document from scratch. (Slow)
|
|
||||||
const Safe ModeEnum = 1
|
|
||||||
|
|
||||||
// Fast mode never checks layers before returning.
|
|
||||||
const Fast ModeEnum = 2
|
|
||||||
|
|
||||||
// Group represents a Document or LayerSet.
|
// Group represents a Document or LayerSet.
|
||||||
type Group interface {
|
type Group interface {
|
||||||
Name() string
|
Name() string
|
||||||
@@ -182,8 +167,8 @@ func (d *Document) Dump() {
|
|||||||
|
|
||||||
// ArtLayer reflects certain values from an Art Layer in a Photoshop document.
|
// ArtLayer reflects certain values from an Art Layer in a Photoshop document.
|
||||||
type ArtLayer struct {
|
type ArtLayer struct {
|
||||||
name string // The layer's name.
|
name string // The layer's name.
|
||||||
// TextItem string
|
Text *string // The contents of a text layer.
|
||||||
bounds [2][2]int // The layers' corners.
|
bounds [2][2]int // The layers' corners.
|
||||||
parent Group // The LayerSet/Document this layer is in.
|
parent Group // The LayerSet/Document this layer is in.
|
||||||
visible bool // Whether or not the layer is visible.
|
visible bool // Whether or not the layer is visible.
|
||||||
@@ -207,11 +192,13 @@ type ArtLayerJSON struct {
|
|||||||
Color [3]int
|
Color [3]int
|
||||||
Stroke [3]int
|
Stroke [3]int
|
||||||
StrokeAmt float32
|
StrokeAmt float32
|
||||||
|
Text *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON fufills the json.Marshaler interface, allowing the ArtLayer to be
|
// MarshalJSON fulfills the json.Marshaler interface, allowing the ArtLayer to be
|
||||||
// saved to disk in JSON format.
|
// saved to disk in JSON format.
|
||||||
func (a *ArtLayer) MarshalJSON() ([]byte, error) {
|
func (a *ArtLayer) MarshalJSON() ([]byte, error) {
|
||||||
|
// txt := strings.Replace(*a.Text, "\r", "\\r", -1)
|
||||||
return json.Marshal(&ArtLayerJSON{
|
return json.Marshal(&ArtLayerJSON{
|
||||||
Name: a.name,
|
Name: a.name,
|
||||||
Bounds: a.bounds,
|
Bounds: a.bounds,
|
||||||
@@ -219,6 +206,7 @@ func (a *ArtLayer) MarshalJSON() ([]byte, error) {
|
|||||||
Color: a.Color.RGB(),
|
Color: a.Color.RGB(),
|
||||||
Stroke: a.Stroke.RGB(),
|
Stroke: a.Stroke.RGB(),
|
||||||
StrokeAmt: a.Stroke.Size,
|
StrokeAmt: a.Stroke.Size,
|
||||||
|
Text: a.Text,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,6 +220,10 @@ func (a *ArtLayer) UnmarshalJSON(b []byte) error {
|
|||||||
a.Color = RGB{tmp.Color[0], tmp.Color[1], tmp.Color[2]}
|
a.Color = RGB{tmp.Color[0], tmp.Color[1], tmp.Color[2]}
|
||||||
a.Stroke = &Stroke{tmp.StrokeAmt, RGB{tmp.Stroke[0], tmp.Stroke[1], tmp.Stroke[2]}}
|
a.Stroke = &Stroke{tmp.StrokeAmt, RGB{tmp.Stroke[0], tmp.Stroke[1], tmp.Stroke[2]}}
|
||||||
a.visible = tmp.Visible
|
a.visible = tmp.Visible
|
||||||
|
if tmp.Text != nil {
|
||||||
|
// s := strings.Replace(*tmp.Text, "\\r", "\r", -1)
|
||||||
|
a.Text = tmp.Text
|
||||||
|
}
|
||||||
a.current = false
|
a.current = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -264,7 +256,7 @@ func (a *ArtLayer) SetParent(c Group) {
|
|||||||
a.parent = c
|
a.parent = c
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetActive makes this layer active in photoshop.
|
// SetActive makes this layer active in Photoshop.
|
||||||
// Layers need to be active to perform certain operations
|
// Layers need to be active to perform certain operations
|
||||||
func (a *ArtLayer) SetActive() ([]byte, error) {
|
func (a *ArtLayer) SetActive() ([]byte, error) {
|
||||||
js := fmt.Sprintf("app.activeDocument.activeLayer=%s", JSLayer(a.Path()))
|
js := fmt.Sprintf("app.activeDocument.activeLayer=%s", JSLayer(a.Path()))
|
||||||
@@ -473,10 +465,14 @@ func (l *LayerSet) ArtLayer(name string) *ArtLayer {
|
|||||||
}
|
}
|
||||||
var lyr2 *ArtLayer
|
var lyr2 *ArtLayer
|
||||||
err = json.Unmarshal(byt, &lyr2)
|
err = json.Unmarshal(byt, &lyr2)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
lyr.name = lyr2.name
|
lyr.name = lyr2.name
|
||||||
lyr.bounds = lyr2.bounds
|
lyr.bounds = lyr2.bounds
|
||||||
lyr.visible = lyr2.visible
|
lyr.visible = lyr2.visible
|
||||||
lyr.current = true
|
lyr.current = true
|
||||||
|
lyr.Text = lyr2.Text
|
||||||
}
|
}
|
||||||
return lyr
|
return lyr
|
||||||
}
|
}
|
||||||
@@ -519,6 +515,10 @@ func NewLayerSet(path string, g Group) (*LayerSet, error) {
|
|||||||
byt, err := DoJs("getLayerSet.jsx", JSLayer(path))
|
byt, err := DoJs("getLayerSet.jsx", JSLayer(path))
|
||||||
var out *LayerSet
|
var out *LayerSet
|
||||||
err = json.Unmarshal(byt, &out)
|
err = json.Unmarshal(byt, &out)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(string(byt))
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
if flag.Lookup("test.v") != nil {
|
if flag.Lookup("test.v") != nil {
|
||||||
// log.Println(path)
|
// log.Println(path)
|
||||||
// log.Println(out)
|
// log.Println(out)
|
||||||
|
|||||||
Reference in New Issue
Block a user