- close() / quit()

Added functionality
- setlayervisibility()

Made SaveOptions into an enum for better readibility
This commit is contained in:
Unknown
2018-03-08 14:24:22 -05:00
parent 9cbf0e9b92
commit 17a7f8ccd1
8 changed files with 117 additions and 51 deletions

25
ps.go
View File

@@ -23,6 +23,19 @@ 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)
@@ -42,8 +55,8 @@ func Start() error {
} }
// Close closes the active document. // Close closes the active document.
func Close() error { func Close(save PSSaveOptions) error {
_, err := run("close") _, err := run("close", save.String())
return err return err
} }
@@ -54,11 +67,8 @@ func Open(path string) error {
} }
// Quit exits Photoshop. // Quit exits Photoshop.
// func Quit(save PSSaveOptions) error {
// There are 3 valid values for save: 1 (psSaveChanges), 2 (psDoNotSaveChanges), _, err := run("quit", save.String())
// 3 (psPromptToSaveChanges).
func Quit(save int) error {
_, err := run("quit", string(save))
return err return err
} }
@@ -96,7 +106,6 @@ func DoJs(path string, args ...string) ([]byte, error) {
// Useful for when you need to do something by hand in the middle of an // Useful for when you need to do something by hand in the middle of an
// otherwise automated process. // otherwise automated process.
func Wait(msg string) { func Wait(msg string) {
fmt.Println()
fmt.Print(msg) fmt.Print(msg)
var input string var input string
fmt.Scanln(&input) fmt.Scanln(&input)

View File

@@ -2,10 +2,8 @@ package ps
import ( import (
"fmt" "fmt"
// "log"
"os" "os"
"path/filepath" "path/filepath"
_ "strings"
"testing" "testing"
) )
@@ -17,7 +15,10 @@ func TestPkgPath(t *testing.T) {
} }
func TestStart(t *testing.T) { func TestStart(t *testing.T) {
Start() err := Start()
if err != nil {
t.Fatal(err)
}
} }
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
@@ -31,14 +32,23 @@ func TestOpen(t *testing.T) {
} }
func TestClose(t *testing.T) { func TestClose(t *testing.T) {
Close() if testing.Short() {
t.Skip("Skipping \"TestClose\"")
}
err := Close(2)
if err != nil {
t.Fatal(err)
}
} }
func TestQuit(t *testing.T) { func TestQuit(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("Skipping \"TestQuit\"") t.Skip("Skipping \"TestQuit\"")
} }
Quit(2) err := Quit(2)
if err != nil {
t.Fatal(err)
}
} }
func TestDoJs(t *testing.T) { func TestDoJs(t *testing.T) {
@@ -109,14 +119,14 @@ func TestLayers(t *testing.T) {
} }
func TestLayer(t *testing.T) { func TestLayer(t *testing.T) {
// lyr, err := Layer("Areas/TitleBackground") lyr, err := Layer("Areas/TitleBackground")
_, err := Layer("Areas/TitleBackground") // _, err := Layer("Areas/TitleBackground")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
/* fmt.Println(lyr.Name) fmt.Println(lyr.Name)
fmt.Println(lyr.Bounds) fmt.Println(lyr.Bounds)
*/
} }
func TestApplyDataset(t *testing.T) { func TestApplyDataset(t *testing.T) {
@@ -129,4 +139,15 @@ func TestApplyDataset(t *testing.T) {
fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", ret, out) fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", ret, out)
t.Fatal(fail) t.Fatal(fail)
} }
err = Quit(2)
if err != nil {
t.Fatal(err)
}
}
func TestDoJs_HideLayer(t *testing.T) {
_, err := DoJs("hideLayers.jsx", "Areas/TitleBackground")
if err != nil {
t.Fatal(err)
}
} }

View File

@@ -1,3 +1,3 @@
set App = CreateObject("Photoshop.Application") set App = CreateObject("Photoshop.Application")
set Doc = App.activeDocument set Doc = App.activeDocument
Doc.Close Doc.Close(CInt(wScript.Arguments(0)))

View File

@@ -1,32 +1,18 @@
//arguments = [ 'F:\\TEMP\\js_out.txt\r\narg\r\nargs\r\n', 'Areas/TitleBackground'] #include lib.js
var saveFile = File(arguments[0]) var stdout = newFile(arguments[0])
if(saveFile.exists) var set = getLayers(arguments[1])
saveFile.remove()
saveFile.encoding = "UTF8" stdout.writeln('[');
saveFile.open("e", "TEXT", "????") for (var l = 0; l < set.layers.length; l++) {
try { var lyr = set.layers[l]
var doc = app.activeDocument var lyrset = arguments[1].replace(lyr.name, "")
var splitPath = arguments[1].split('/') stdout.write(('{"Name":"' + lyr.name + '", "Bounds": [[' + lyr.bounds[0] + ',' +
var bottomLayerSet = doc.layerSets.getByName(splitPath[0]) lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
for (var i = 1; i < splitPath.length; i++) { lyr.bounds[3] + ']], "LayerSet": "' + lyrset + '"}').replace(/ px/g, ""));
try {bottomLayerSet = bottomLayerSet.layerSets.getByName(splitPath[i])} if (l != set.layers.length - 1)
catch (e) {bottomLayerSet = { layers: [bottomLayerSet.layers.getByName(splitPath[i])] }} stdout.write(',');
} stdout.writeln();
saveFile.writeln('[');
for (var l = 0; l < bottomLayerSet.layers.length; l++) {
var lyr = bottomLayerSet.layers[l]
saveFile.write('{"Name":"' + lyr.name + '", "Bounds": [["' + lyr.bounds[0] + '","' +
lyr.bounds[1] + '","' + lyr.bounds[2] + '"],["' + lyr.bounds[3] + '"]]}');
if (l != bottomLayerSet.layers.length - 1)
saveFile.write(',');
saveFile.writeln();
}
saveFile.writeln(']');
} catch (e) {
if (e.message.indexOf('User') == -1)
alert('ERROR: ' + e.message + ' at ' + e.fileName + ':' + e.line);
else
throw new Exception('User cancelled the operation');
} }
saveFile.close(); stdout.writeln(']');
stdout.close()

34
scripts/lib.js Normal file
View File

@@ -0,0 +1,34 @@
// Opens and returns a file, overwriting new data.
function newFile(path) {
var f = File(path)
if(f.exists)
f.remove()
f.encoding = "UTF8"
f.open("e", "TEXT", "????")
return f
}
// Returns an array of ArtLayers from a layerSet or an ArtLayer.
function getLayers(path) {
try {
var doc = app.activeDocument
var path = path.split('/')
var lyrs = doc.layerSets.getByName(path[0])
for (var i = 1; i < path.length; i++) {
try {
lyrs = lyrs.layerSets.getByName(path[i])
} catch (e) {
lyrs = { layers: [lyrs.layers.getByName(path[i])] } }
}
return lyrs
} catch (e) {
if (e.message.indexOf('User') == -1)
alert(err(e));
else
throw new Exception('User cancelled the operation');
}
}
function err(e) {
return 'ERROR: ' + e.message + ' at ' + e.fileName + ':' + e.line;
}

View File

@@ -2,7 +2,7 @@
Set appRef = CreateObject("Photoshop.Application") Set appRef = CreateObject("Photoshop.Application")
Do While appRef.Documents.Count > 0 Do While appRef.Documents.Count > 0
appRef.ActiveDocument.Close(wScript.Arguments(0)) appRef.ActiveDocument.Close(CInt(wScript.Arguments(0)))
Loop Loop
appRef.Quit() appRef.Quit()

View File

@@ -0,0 +1,11 @@
#include lib.js
var stdout = newFile(arguments[0])
var lyrs = getLayers(arguments[1])
var vis = arguments[2] == "true"
try {
for (var i = 0; i < lyrs.layers.length; i++)
lyrs.layers[i].visible = vis
} catch (e) {
stdout.writeln(err(e))
}
stdout.close()

View File

@@ -8,7 +8,12 @@ package ps
type ArtLayer struct { type ArtLayer struct {
Name string Name string
TextItem string TextItem string
Bounds [2][2]string Bounds [2][2]int
LayerSet string
}
func (a *ArtLayer) SetVisible() {
DoJs("setLayerVisibility.jsx", a.LayerSet+"/"+a.Name, "true")
} }
// func (a *ArtLayer) Name() string { // func (a *ArtLayer) Name() string {