|
19 | 19 | from typing import NoReturn |
20 | 20 | from typing import Optional |
21 | 21 | from typing import TypeVar |
| 22 | +from typing import Union |
22 | 23 |
|
23 | 24 | # Import third-party modules |
24 | 25 | from comtypes import COMError |
|
34 | 35 | from photoshop.api._layerSets import LayerSets |
35 | 36 | from photoshop.api._layers import Layers |
36 | 37 | from photoshop.api._selection import Selection |
| 38 | +from photoshop.api.enumerations import ExportType |
37 | 39 | from photoshop.api.enumerations import ExtensionType |
38 | 40 | from photoshop.api.enumerations import SaveOptions |
39 | 41 | from photoshop.api.enumerations import TrimType |
| 42 | +from photoshop.api.save_options import ExportOptionsSaveForWeb |
40 | 43 |
|
41 | 44 |
|
42 | 45 | # Custom types. |
@@ -345,9 +348,26 @@ def crop( |
345 | 348 | """ |
346 | 349 | return self.app.crop(bounds, angle, width, height) |
347 | 350 |
|
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) |
351 | 371 |
|
352 | 372 | def duplicate(self, name=None, merge_layers_only=False): |
353 | 373 | return Document(self.app.duplicate(name, merge_layers_only)) |
|
0 commit comments