mirror of
https://github.com/sbrow/ps.git
synced 2025-12-29 18:47:38 -05:00
improved robustness
more tests added.
This commit is contained in:
52
ps.go
52
ps.go
@@ -1,5 +1,3 @@
|
||||
// +build windows
|
||||
|
||||
// Package ps lets you manipulate Adobe Photoshop (CS5) from go.
|
||||
// This is primarily done by calling VBS/Applescript files.
|
||||
//
|
||||
@@ -8,9 +6,12 @@ package ps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ var pkgpath string
|
||||
|
||||
func init() {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
pkgpath = path.Dir(file)
|
||||
pkgpath = filepath.Dir(file)
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
Cmd = "cscript.exe"
|
||||
@@ -53,8 +54,29 @@ func Quit(save int) ([]byte, error) {
|
||||
return run("quit", string(save))
|
||||
}
|
||||
|
||||
func Js(args ...string) ([]byte, error) {
|
||||
return run("dojs", args...)
|
||||
func Js(path string, args ...string) ([]byte, error) {
|
||||
// Temp file for js to output to.
|
||||
outpath := filepath.Join(os.Getenv("TEMP"), "js_out.txt")
|
||||
args = append([]string{outpath}, args...)
|
||||
|
||||
// If passed a script by name, assume it's in the default folder.
|
||||
if filepath.Dir(path) == "." {
|
||||
path = filepath.Join(pkgpath, "scripts", path)
|
||||
}
|
||||
|
||||
args = append([]string{path}, args...)
|
||||
cmd, err := run("dojs", args...)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
file, err := ioutil.ReadFile(outpath)
|
||||
if err != nil {
|
||||
// fmt.Println(cmd)
|
||||
return cmd, err
|
||||
}
|
||||
cmd = append(cmd, file...)
|
||||
// os.Remove(outpath)
|
||||
return cmd, err
|
||||
}
|
||||
|
||||
func Wait(msg string) {
|
||||
@@ -66,7 +88,6 @@ func Wait(msg string) {
|
||||
func run(name string, args ...string) ([]byte, error) {
|
||||
var ext string
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
@@ -77,10 +98,23 @@ func run(name string, args ...string) ([]byte, error) {
|
||||
if !strings.HasSuffix(name, ext) {
|
||||
name += ext
|
||||
}
|
||||
args = append([]string{Opts, path.Join(pkgpath, "scripts", name)}, args...)
|
||||
|
||||
if strings.Contains(name, "dojs") {
|
||||
args = append([]string{Opts, filepath.Join(pkgpath, "scripts", name)},
|
||||
args[0],
|
||||
fmt.Sprintf("%s", strings.Join(args[1:], ",")),
|
||||
)
|
||||
} else {
|
||||
args = append([]string{Opts, filepath.Join(pkgpath, "scripts", name)}, args...)
|
||||
}
|
||||
|
||||
cmd := exec.Command(Cmd, args...)
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
cmd.Stderr = &out
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return []byte{}, errors.New(string(out.Bytes()))
|
||||
} else {
|
||||
return out.Bytes(), err
|
||||
}
|
||||
}
|
||||
|
||||
45
ps_test.go
45
ps_test.go
@@ -2,7 +2,6 @@ package ps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
_ "strings"
|
||||
@@ -20,24 +19,24 @@ func TestOpen(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping \"TestOpen\"")
|
||||
}
|
||||
Open("F:\\GitLab\\dreamkeepers-psd\\Template009.1.psd")
|
||||
}
|
||||
|
||||
// TODO: Comparison borked
|
||||
/*func TestRun(t *testing.T) {
|
||||
out := []byte("Testing...\n")
|
||||
msg, err := run("test")
|
||||
_, err := Open("F:\\GitLab\\dreamkeepers-psd\\Template009.1.psd")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(msg) == string(out) {
|
||||
fail := fmt.Sprintf("run(test)\ngot:\t\"%s\"\nwant:\t\"%s\"\n", msg, out)
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
out := []byte("hello,\r\nworld!\r\n")
|
||||
msg, err := run("test", "hello,", "world!")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(msg) != string(out) {
|
||||
fail := fmt.Sprintf("TestRun faild.\ngot:\n\"%s\"\nwant:\n\"%s\"\n", msg, out)
|
||||
t.Fatal(fail)
|
||||
} else {
|
||||
os.Remove(path.Join(pkgpath, "scripts", "test.txt"))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func TestQuit(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping \"TestQuit\"")
|
||||
@@ -50,21 +49,15 @@ func TestWait(t *testing.T) {
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// TODO: Comparison borked
|
||||
/*func TestJS(t *testing.T) {
|
||||
out := "Testing...\n"
|
||||
_, err := Js(path.Join(Folder, "test.jsx"), Folder)
|
||||
func TestJS(t *testing.T) {
|
||||
out := []byte("F:\\TEMP\\js_out.txt\r\narg\r\nargs\r\n")
|
||||
script := "test.jsx"
|
||||
ret, err := Js(script, "arg", "args")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := ioutil.ReadFile(path.Join(Folder, "test.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Compare(string(f), string(out)) != 0 {
|
||||
fmt.Println(f)
|
||||
fmt.Println([]byte(out))
|
||||
fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", f, out)
|
||||
if string(ret) != string(out) {
|
||||
fail := fmt.Sprintf("TestJS failed.\ngot:\t\"%s\"\nwant:\t\"%s\"", ret, out)
|
||||
t.Fatal(fail)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
|
||||
Dim app
|
||||
Set app = CreateObject("Photoshop.Application")
|
||||
if WScript.Arguments.Count = 0 then
|
||||
WScript.Echo "Missing parameters"
|
||||
Dim appRef
|
||||
Set appRef = CreateObject("Photoshop.Application")
|
||||
if wScript.Arguments.Count = 0 then
|
||||
wScript.Echo "Missing parameters"
|
||||
else
|
||||
path = wScript.Arguments(0)
|
||||
folder = wScript.Arguments(1)
|
||||
app.DoJavaScriptFile path, Array(Folder)
|
||||
args = wScript.Arguments(1)
|
||||
appRef.DoJavaScriptFile path, Split(args, ",")
|
||||
end if
|
||||
@@ -1,11 +1,10 @@
|
||||
var Path = arguments[0];
|
||||
alert(Path)
|
||||
var saveFile = File(Path + "/test.txt");
|
||||
|
||||
var saveFile = File(arguments[0]);
|
||||
if(saveFile.exists)
|
||||
saveFile.remove();
|
||||
|
||||
saveFile.encoding = "UTF8";
|
||||
saveFile.open("e", "TEXT", "????");
|
||||
saveFile.writeln("Testing...");
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
saveFile.writeln(arguments[i])
|
||||
}
|
||||
saveFile.close();
|
||||
@@ -1 +1,3 @@
|
||||
wScript.echo "Testing..."
|
||||
for i=0 to wScript.Arguments.length-1
|
||||
wScript.echo wScript.Arguments(i)
|
||||
next
|
||||
Reference in New Issue
Block a user