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