Skip to content

Commit 1a74b83

Browse files
committed
fix: fix export document
1 parent d01358c commit 1a74b83

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

photoshop/api/_document.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import NoReturn
2020
from typing import Optional
2121
from typing import TypeVar
22+
from typing import Union
2223

2324
# Import third-party modules
2425
from comtypes import COMError
@@ -34,9 +35,11 @@
3435
from photoshop.api._layerSets import LayerSets
3536
from photoshop.api._layers import Layers
3637
from photoshop.api._selection import Selection
38+
from photoshop.api.enumerations import ExportType
3739
from photoshop.api.enumerations import ExtensionType
3840
from photoshop.api.enumerations import SaveOptions
3941
from photoshop.api.enumerations import TrimType
42+
from photoshop.api.save_options import ExportOptionsSaveForWeb
4043

4144

4245
# Custom types.
@@ -345,9 +348,26 @@ def crop(
345348
"""
346349
return self.app.crop(bounds, angle, width, height)
347350

348-
def exportDocument(self, file_path: str, exportAs=None, options=None):
349-
"""Exports the Document."""
350-
return self.app.exportDocument(file_path, exportAs, options)
351+
def exportDocument(
352+
self, file_path: str, exportAs: ExportType = None, options: Union[ExportOptionsSaveForWeb] = None
353+
):
354+
"""Exports the Document.
355+
356+
Note:
357+
This is a patched version, Due to the problem of dynamic binding,
358+
we cannot call it directly, so this command is executed by javascript.
359+
360+
References:
361+
- https://stackoverflow.com/questions/12286761/saving-a-png-with-photoshop-script-not-working
362+
363+
"""
364+
file_path = file_path.replace("\\", "/")
365+
scripts = f"""
366+
var file = new File("{file_path}");
367+
{options.as_javascript()}
368+
app.activeDocument.exportDocument(file, ExportType.{exportAs.name.upper()}, opts)
369+
"""
370+
return self.eval_javascript(scripts)
351371

352372
def duplicate(self, name=None, merge_layers_only=False):
353373
return Document(self.app.duplicate(name, merge_layers_only))

0 commit comments

Comments
 (0)