Skip to content

Commit 3e35274

Browse files
committed
fix(action_manager,examples): fix support for Path(AliasType) and fix import picture example
Signed-off-by: TsXor <[email protected]>
1 parent 405f8dd commit 3e35274

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

examples/import_image_as_layer.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88
import photoshop.api.action_manager as am
99

1010

11-
with Session(action="new_document") as ps:
12-
# replace it with your own path here
13-
import_dict = {"_classID": None, "null": pathlib.Path("your/image/path")}
11+
def importfile(app, path: pathlib.WindowsPath, position: (float, float), size: (float, float)): # noqa
12+
px, py = position
13+
sx, sy = size
14+
import_dict = {
15+
"null": path,
16+
"freeTransformCenterState": am.Enumerated(type="quadCenterState", value="QCSAverage"),
17+
"offset": {
18+
"horizontal": am.UnitDouble(unit="distanceUnit", double=px),
19+
"vertical": am.UnitDouble(unit="distanceUnit", double=py),
20+
"_classID": "offset",
21+
},
22+
"width": am.UnitDouble(unit="percentUnit", double=sx),
23+
"height": am.UnitDouble(unit="percentUnit", double=sy),
24+
"_classID": None,
25+
}
1426
import_desc = ps.ActionDescriptor.load(import_dict)
15-
ps.app.executeAction(am.str2id("Plc "), import_desc)
16-
# length of charID should always be 4, if not, pad with spaces
27+
app.executeAction(am.str2id("placeEvent"), import_desc)
28+
29+
30+
with Session(action="new_document") as ps:
31+
importfile(ps.app, pathlib.Path("your/image/path"), (-100, 456), (4470, 4463))

photoshop/api/action_descriptor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def getObjectValue(self, key):
129129
"""Implemented in _actionmanager_type_binder.ActionDescriptor"""
130130
pass
131131

132-
def getPath(self, key: int) -> pathlib.Path:
132+
def getPath(self, key: int) -> pathlib.WindowsPath:
133133
"""Gets the value of a key of type File."""
134134
return pathlib.Path(self.app.getPath(key))
135135

@@ -200,7 +200,7 @@ def putObject(self, key: int, classID: int, value):
200200
assert value.typename == "ActionDescriptor"
201201
self.app.putObject(key, classID, value)
202202

203-
def putPath(self, key: int, value: pathlib.Path):
203+
def putPath(self, key: int, value: pathlib.WindowsPath):
204204
"""Sets the value for a key whose type is path."""
205205
self.app.putPath(key, str(value))
206206

photoshop/api/action_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def getObjectValue(self, index):
9191
"""Implemented in _actionmanager_type_binder.ActionList"""
9292
pass
9393

94-
def getPath(self, index: int) -> pathlib.Path:
94+
def getPath(self, index: int) -> pathlib.WindowsPath:
9595
"""Gets the value of a list element of type File."""
9696
return pathlib.Path(self.app.getPath(index))
9797

@@ -162,7 +162,7 @@ def putObject(self, classID: int, value):
162162
assert value.typename == "ActionDescriptor"
163163
self.app.putObject(classID, value)
164164

165-
def putPath(self, value: pathlib.Path):
165+
def putPath(self, value: pathlib.WindowsPath):
166166
"""Appends a new value, a path."""
167167
self.app.putPath(str(value))
168168

photoshop/api/action_manager/_main_types/_type_mapper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Handles almost all type mappings. (Some else are in ReferenceKey.)
33
This module is INTERNAL. You should not import functions from it."""
44

5+
# Import built-in modules
6+
import pathlib
7+
58
from ..desc_value_types import Enumerated
69
from ..desc_value_types import TypeID
710
from ..desc_value_types import UnitDouble
@@ -15,6 +18,7 @@
1518
int: "Integer",
1619
float: "Double",
1720
str: "String",
21+
pathlib.WindowsPath: "Path",
1822
Enumerated: "Enumerated",
1923
UnitDouble: "UnitDouble",
2024
TypeID: "Class",
@@ -43,6 +47,7 @@ def unpack(val):
4347
def pack(obj, index): # "index" means id of key string or list index.
4448
valtype = obj.getType(index)
4549
typestr = str(valtype)[14:-4]
50+
typestr = "Path" if typestr == "Alias" else typestr
4651
if typestr == "Data":
4752
# No plan to support RawType because it seldom runs successfully
4853
# and is seldom used in regular scripting.

photoshop/api/action_manager/jprint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ def jformat(astr, indent=4, prefix=None):
5858
return nstr
5959

6060

61-
def jprint(obj, indent=4):
61+
def jprint(obj, indent=4, prefix=None):
6262
"""Print formatted repr of an object."""
63-
print(jformat(repr(obj), indent=indent))
63+
print(jformat(repr(obj), indent=indent, prefix=prefix))

photoshop/api/action_manager/js_converter/convert.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# Import built-in modules
77
import json
8+
import pathlib
89

910
from ..desc_value_types import Enumerated
1011
from ..desc_value_types import TypeID
@@ -45,6 +46,7 @@ def unhead(string):
4546
"UnitDouble": lambda x: UnitDouble(unhead(x["unit"]), x["double"]),
4647
"Enumerated": lambda x: Enumerated(unhead(x["enumtype"]), unhead(x["enumval"])),
4748
"TypeID": lambda x: toid(x["string"]),
49+
"File": lambda x: pathlib.Path(x["string"]),
4850
"ActionDescriptor": lambda x: parsedict(x),
4951
"ActionReference": lambda x: parseref(x),
5052
"ActionList": lambda x: parselist(x),

photoshop/api/action_manager/js_converter/injection_js.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class TypeID {
3030
}
3131
}
3232
33+
class File {
34+
constructor(string) {
35+
this.type = 'File'
36+
this.string = string
37+
}
38+
}
39+
3340
function charIDToTypeID(chr) {
3441
return 'CharID_'+chr
3542
}
@@ -49,6 +56,7 @@ class ActionDescriptor {
4956
putReference(key,val) {this[key] = val}
5057
putList(key,val) {this[key] = val}
5158
putClass(key,val) {this[key] = new TypeID(val)}
59+
putPath(key,val) {this[key] = val}
5260
}
5361
5462
class ActionList {
@@ -63,6 +71,7 @@ class ActionList {
6371
putReference(val) {this.len += 1; this[this.len-1] = val}
6472
putList(val) {this.len += 1; this[this.len-1] = val}
6573
putClass(val) {this.len += 1; this[this.len-1] = new TypeID(val)}
74+
putPath(val) {this.len += 1; this[this.len-1] = val}
6675
}
6776
6877
class ActionReference {

0 commit comments

Comments
 (0)