From 04e936238687a65f2c29698e6c998c6548fb5e47 Mon Sep 17 00:00:00 2001 From: TsXor Date: Thu, 8 Sep 2022 21:44:41 +0800 Subject: [PATCH] fix(action_manager,examples): fix support for Path(AliasType) and fix import picture example Signed-off-by: TsXor --- examples/import_image_as_layer.py | 25 +++++++++++++++---- photoshop/api/action_descriptor.py | 4 +-- photoshop/api/action_list.py | 4 +-- .../_main_types/_type_mapper.py | 5 ++++ photoshop/api/action_manager/jprint.py | 4 +-- .../action_manager/js_converter/convert.py | 2 ++ .../js_converter/injection_js.py | 9 +++++++ 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/examples/import_image_as_layer.py b/examples/import_image_as_layer.py index 45468a73..c841eac4 100644 --- a/examples/import_image_as_layer.py +++ b/examples/import_image_as_layer.py @@ -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)) diff --git a/photoshop/api/action_descriptor.py b/photoshop/api/action_descriptor.py index 14423e4b..281575fd 100644 --- a/photoshop/api/action_descriptor.py +++ b/photoshop/api/action_descriptor.py @@ -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)) @@ -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)) diff --git a/photoshop/api/action_list.py b/photoshop/api/action_list.py index 72b18b38..0c116ed9 100644 --- a/photoshop/api/action_list.py +++ b/photoshop/api/action_list.py @@ -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)) @@ -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)) diff --git a/photoshop/api/action_manager/_main_types/_type_mapper.py b/photoshop/api/action_manager/_main_types/_type_mapper.py index 37016520..36ff6346 100644 --- a/photoshop/api/action_manager/_main_types/_type_mapper.py +++ b/photoshop/api/action_manager/_main_types/_type_mapper.py @@ -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 @@ -15,6 +18,7 @@ int: "Integer", float: "Double", str: "String", + pathlib.WindowsPath: "Path", Enumerated: "Enumerated", UnitDouble: "UnitDouble", TypeID: "Class", @@ -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. diff --git a/photoshop/api/action_manager/jprint.py b/photoshop/api/action_manager/jprint.py index fe1daf4f..6ba6a506 100644 --- a/photoshop/api/action_manager/jprint.py +++ b/photoshop/api/action_manager/jprint.py @@ -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)) diff --git a/photoshop/api/action_manager/js_converter/convert.py b/photoshop/api/action_manager/js_converter/convert.py index 9d1ccba5..e0b56958 100644 --- a/photoshop/api/action_manager/js_converter/convert.py +++ b/photoshop/api/action_manager/js_converter/convert.py @@ -5,6 +5,7 @@ # Import built-in modules import json +import pathlib from ..desc_value_types import Enumerated from ..desc_value_types import TypeID @@ -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), diff --git a/photoshop/api/action_manager/js_converter/injection_js.py b/photoshop/api/action_manager/js_converter/injection_js.py index 6d2c07a4..234d0d1f 100644 --- a/photoshop/api/action_manager/js_converter/injection_js.py +++ b/photoshop/api/action_manager/js_converter/injection_js.py @@ -30,6 +30,13 @@ class TypeID { } } +class File { + constructor(string) { + this.type = 'File' + this.string = string + } +} + function charIDToTypeID(chr) { return 'CharID_'+chr } @@ -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 { @@ -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 {