enthought / comtypes

A pure Python, lightweight COM client and server framework, based on the ctypes Python FFI package.
Other
290 stars 97 forks source link

Comtypes call Photoshop, I don't know how to control the sub window content #496

Closed monkeycc closed 8 months ago

monkeycc commented 1 year ago

// =======================================================
var idOpn = charIDToTypeID( "Opn " );
    var desc225 = new ActionDescriptor();
    var iddontRecord = stringIDToTypeID( "dontRecord" );
    desc225.putBoolean( iddontRecord, false );
    var idforceNotify = stringIDToTypeID( "forceNotify" );
    desc225.putBoolean( idforceNotify, true );
    var idnull = charIDToTypeID( "null" );
    desc225.putPath( idnull, new File( "E:\\2023_ShuJu\\X\\group1\\1.tiff" ) );
    var idDocI = charIDToTypeID( "DocI" );
    desc225.putInteger( idDocI, 59 );
    var idtemplate = stringIDToTypeID( "template" );
    desc225.putBoolean( idtemplate, false );
executeAction( idOpn, desc225, DialogModes.NO );

// =======================================================
var idAdobeScriptAutomationScripts = stringIDToTypeID( "AdobeScriptAutomation Scripts" );
    var desc309 = new ActionDescriptor();
    var idjsCt = charIDToTypeID( "jsCt" );
    desc309.putPath( idjsCt, new File( "C:\\Program Files\\Adobe\\Adobe Photoshop 2023\\Required\\HDRToning.jsx" ) );
    var idjsMs = charIDToTypeID( "jsMs" );
    desc309.putString( idjsMs, """undefined""" );
executeAction( idAdobeScriptAutomationScripts, desc309, DialogModes.NO );

import comtypes.client

# Create a reference to the Photoshop application
ps_app = comtypes.client.CreateObject("Photoshop.Application")

# Open a file
idOpn = ps_app.CharIDToTypeID("Opn ")
desc225 = comtypes.client.CreateObject("Photoshop.ActionDescriptor")
iddontRecord = ps_app.StringIDToTypeID("dontRecord")
desc225.PutBoolean(iddontRecord, False)
idforceNotify = ps_app.StringIDToTypeID("forceNotify")
desc225.PutBoolean(idforceNotify, True)
idnull = ps_app.CharIDToTypeID("null")
desc225.PutPath(idnull, "E:\\1.tiff")
idDocI = ps_app.CharIDToTypeID("DocI")
desc225.PutInteger(idDocI, 59)
idtemplate = ps_app.StringIDToTypeID("template")
desc225.PutBoolean(idtemplate, False)
ps_app.ExecuteAction(idOpn, desc225, 2)  # DialogModes.NO = 2

# Run a script
idAdobeScriptAutomationScripts = ps_app.StringIDToTypeID("AdobeScriptAutomation Scripts")
desc231 = comtypes.client.CreateObject("Photoshop.ActionDescriptor")
idjsCt = ps_app.CharIDToTypeID("jsCt")
desc231.PutPath(idjsCt, "C:\\Program Files\\Adobe\\Adobe Photoshop 2023\\Required\\HDRToning.jsx")
idjsMs = ps_app.CharIDToTypeID("jsMs")
desc231.PutString(idjsMs, "undefined")
ps_app.ExecuteAction(idAdobeScriptAutomationScripts, desc231, 2)  # DialogModes.NO = 2

微信截图_20231004222630

"Image->Adjustments->HDR Toning"

The HDRToning window appears

How to obtain the data inside

And press OK to save

junkmd commented 1 year ago

Please provide the version of Python you used to run the script. There might be a regression in ctypes that affected comtypes for Python versions such as Python==3.8.x, Python==3.9.x, Python<3.10.10, and Python<3.11.2.

Please let me know the source of information you referred to regarding the Photoshop COM interface.

Also, you're writing code snippets that are not in Python. What is the meaning of this?

Additionally, if an error was raised, please provide details about the error.

junkmd commented 8 months ago

I found https://github.com/loonghao/photoshop-python-api/ that manipulates Photoshop using comtypes.

Within it, there were intriguing descriptions like the following:

from https://github.com/loonghao/photoshop-python-api/blob/9eb4fe858c661da6f66961ba4c4976e22d8d4413/photoshop/api/_core.py#L152-L160,

        """
        * This is a hack for Photoshop's broken COM implementation.
        * Photoshop does not implement 'IDispatch::GetTypeInfo', so when
        getting a field from the COM object, comtypes will first try
        to fetch it as a property, then treat it as a method if it fails.
        * In this case, Photoshop does not return the proper error code, since it
        blindly treats the property getter as a method call.
        * Fortunately, comtypes provides a way to explicitly flag methods.
        """

It seems that the problems of this nature are not due to a bug in comtypes, nor are they caused by an unknown regression in ctypes. Instead, it appears that the root cause lies in Photoshop's COM implementation.

I am going to close this issue as there have been no replies since it was posted.

If you would like to discuss this issue again, feel free to reopen this.