loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
598 stars 68 forks source link

feat: Explicitly flag COM methods #298

Closed feisuzhu closed 9 months ago

feisuzhu commented 10 months ago

Fixing https://github.com/loonghao/photoshop-python-api/issues/247

loonghao commented 9 months ago

Thank you for your code contribution:

I ran some tests and found a lot of errors and I think the reason is because we used CreateObject(self._program_name, dynamic=True)

Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\active_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\add_metadata.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\add_slate.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\add_start_application_event.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\apply_crystallize_filter_action.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\apply_filters.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\change_color_of_background_and_foreground.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\color.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\compare_colors.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\convert_smartobject_to_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\copy_and_paste.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\create_new_document.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\create_thumbnail.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\creating_a_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\cropping.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\current_tool.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\do_photoshop_action.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\emboss_action.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\enable_generator.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\eval_javascript.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\export_document.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\export_layers_as_png.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\export_layers_use_export_options_saveforweb.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\fill_selection.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\fit_on_screen.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\get_document_by_name.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\get_layer_by_name.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\hello_world.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\import_image_as_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\link_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\list_documents.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\load_selection.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\move_to_end.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\new_document.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\open_psd.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\operate_channels.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\operate_layerSet.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\photoshop_session.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\replace_images.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\rotate_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\run_batch.py [WinError 3] 系统找不到指定的路径。: 'your/images/root'
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\save_as_pdf.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\save_as_tga.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\save_to_psd.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\selection_stroke.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\session_callback.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\session_document_duplicate.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\session_hello_world.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\session_new_document.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\session_smart_sharpen.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\set_active_layer.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\smart_sharpen.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\toggle_proof_colors.py Name _FlagAsMethod not found
Test failed: C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\examples\trim.py Name _FlagAsMethod not found
Traceback (most recent call last):
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\venv\lib\site-packages\comtypes\client\lazybind.py", line 123, in __bind
    return self._tdesc[(name, invkind)]
KeyError: ('_FlagAsMethod', 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\test\manual_test\manual_test_all_examples.py", line 18, in <module>
    app = Application()
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\photoshop\api\application.py", line 44, in __init__
    self._flag_as_method(
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\photoshop\api\_core.py", line 58, in _flag_as_method
    if hasattr(self.app, '_FlagAsMethod'):
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\venv\lib\site-packages\comtypes\client\lazybind.py", line 161, in __getattr__
    descr = self.__bind(name, DISPATCH_METHOD | DISPATCH_PROPERTYGET)
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\venv\lib\site-packages\comtypes\client\lazybind.py", line 126, in __bind
    descr = self._tcomp.Bind(name, invkind)[1]
  File "C:\Users\longhao.DESKTOP-LILAK2P\PycharmProjects\photoshop-python-api\venv\lib\site-packages\comtypes\typeinfo.py", line 395, in Bind
    raise NameError("Name %s not found" % name)
NameError: Name _FlagAsMethod not found
loonghao commented 9 months ago

@feisuzhu My test environment is:

python version: 3.10 photoshop version: 23.5.2

feisuzhu commented 9 months ago

@loonghao

I can't reproduce the result but I can see the cause of the failure above, please help test the modified code again, thanks~

feisuzhu commented 9 months ago

Windows 11 PS 24.7.0 Python 3.11.5

Confirmed to work

loonghao commented 9 months ago

@all-contributors please add @feisuzhu for Code contributors.

allcontributors[bot] commented 9 months ago

@loonghao

I've put up a pull request to add @feisuzhu! :tada: