Skip to content

Commit

Permalink
fix(action_manager,examples): fix support for Path(AliasType) and fix…
Browse files Browse the repository at this point in the history
… import picture example

Signed-off-by: TsXor <[email protected]>
  • Loading branch information
TsXor committed Sep 8, 2022
1 parent b2b2b0e commit 04e9362
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
25 changes: 20 additions & 5 deletions examples/import_image_as_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
import photoshop.api.action_manager as am


with Session(action="new_document") as ps:
# replace it with your own path here
import_dict = {"_classID": None, "null": pathlib.Path("your/image/path")}
def importfile(app, path: pathlib.WindowsPath, position: (float, float), size: (float, float)): # noqa
px, py = position
sx, sy = size
import_dict = {
"null": path,
"freeTransformCenterState": am.Enumerated(type="quadCenterState", value="QCSAverage"),
"offset": {
"horizontal": am.UnitDouble(unit="distanceUnit", double=px),
"vertical": am.UnitDouble(unit="distanceUnit", double=py),
"_classID": "offset",
},
"width": am.UnitDouble(unit="percentUnit", double=sx),
"height": am.UnitDouble(unit="percentUnit", double=sy),
"_classID": None,
}
import_desc = ps.ActionDescriptor.load(import_dict)
ps.app.executeAction(am.str2id("Plc "), import_desc)
# length of charID should always be 4, if not, pad with spaces
app.executeAction(am.str2id("placeEvent"), import_desc)


with Session(action="new_document") as ps:
importfile(ps.app, pathlib.Path("your/image/path"), (-100, 456), (4470, 4463))
4 changes: 2 additions & 2 deletions photoshop/api/action_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def getObjectValue(self, key):
"""Implemented in _actionmanager_type_binder.ActionDescriptor"""
pass

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

Expand Down Expand Up @@ -200,7 +200,7 @@ def putObject(self, key: int, classID: int, value):
assert value.typename == "ActionDescriptor"
self.app.putObject(key, classID, value)

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

Expand Down
4 changes: 2 additions & 2 deletions photoshop/api/action_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def getObjectValue(self, index):
"""Implemented in _actionmanager_type_binder.ActionList"""
pass

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

Expand Down Expand Up @@ -162,7 +162,7 @@ def putObject(self, classID: int, value):
assert value.typename == "ActionDescriptor"
self.app.putObject(classID, value)

def putPath(self, value: pathlib.Path):
def putPath(self, value: pathlib.WindowsPath):
"""Appends a new value, a path."""
self.app.putPath(str(value))

Expand Down
5 changes: 5 additions & 0 deletions photoshop/api/action_manager/_main_types/_type_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Handles almost all type mappings. (Some else are in ReferenceKey.)
This module is INTERNAL. You should not import functions from it."""

# Import built-in modules
import pathlib

from ..desc_value_types import Enumerated
from ..desc_value_types import TypeID
from ..desc_value_types import UnitDouble
Expand All @@ -15,6 +18,7 @@
int: "Integer",
float: "Double",
str: "String",
pathlib.WindowsPath: "Path",
Enumerated: "Enumerated",
UnitDouble: "UnitDouble",
TypeID: "Class",
Expand Down Expand Up @@ -43,6 +47,7 @@ def unpack(val):
def pack(obj, index): # "index" means id of key string or list index.
valtype = obj.getType(index)
typestr = str(valtype)[14:-4]
typestr = "Path" if typestr == "Alias" else typestr
if typestr == "Data":
# No plan to support RawType because it seldom runs successfully
# and is seldom used in regular scripting.
Expand Down
4 changes: 2 additions & 2 deletions photoshop/api/action_manager/jprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def jformat(astr, indent=4, prefix=None):
return nstr


def jprint(obj, indent=4):
def jprint(obj, indent=4, prefix=None):
"""Print formatted repr of an object."""
print(jformat(repr(obj), indent=indent))
print(jformat(repr(obj), indent=indent, prefix=prefix))
2 changes: 2 additions & 0 deletions photoshop/api/action_manager/js_converter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Import built-in modules
import json
import pathlib

from ..desc_value_types import Enumerated
from ..desc_value_types import TypeID
Expand Down Expand Up @@ -45,6 +46,7 @@ def unhead(string):
"UnitDouble": lambda x: UnitDouble(unhead(x["unit"]), x["double"]),
"Enumerated": lambda x: Enumerated(unhead(x["enumtype"]), unhead(x["enumval"])),
"TypeID": lambda x: toid(x["string"]),
"File": lambda x: pathlib.Path(x["string"]),
"ActionDescriptor": lambda x: parsedict(x),
"ActionReference": lambda x: parseref(x),
"ActionList": lambda x: parselist(x),
Expand Down
9 changes: 9 additions & 0 deletions photoshop/api/action_manager/js_converter/injection_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class TypeID {
}
}
class File {
constructor(string) {
this.type = 'File'
this.string = string
}
}
function charIDToTypeID(chr) {
return 'CharID_'+chr
}
Expand All @@ -49,6 +56,7 @@ class ActionDescriptor {
putReference(key,val) {this[key] = val}
putList(key,val) {this[key] = val}
putClass(key,val) {this[key] = new TypeID(val)}
putPath(key,val) {this[key] = val}
}
class ActionList {
Expand All @@ -63,6 +71,7 @@ class ActionList {
putReference(val) {this.len += 1; this[this.len-1] = val}
putList(val) {this.len += 1; this[this.len-1] = val}
putClass(val) {this.len += 1; this[this.len-1] = new TypeID(val)}
putPath(val) {this.len += 1; this[this.len-1] = val}
}
class ActionReference {
Expand Down

0 comments on commit 04e9362

Please sign in to comment.