python: win32com.client.GetActiveObject("iTunes.Application") failing - python

I've been launching itunes with
try:
itapp= win32com.client.Dispatch('iTunes.Application')
except:
itapp= win32com.client.GetActiveObject('iTunes.Application')
Today, whenever I run this, if itunes is already running, it fails with
Traceback (most recent call last):
File "fix_m4a.py", line 63, in <module>
change_itunes_path(s,d)
File "fix_m4a.py", line 45, in change_itunes_path
itapp = win32com.client.GetActiveObject('iTunes.Application')
File "C:\program files\python27\lib\site-packages\win32com\client\__init__.py"
, line 79, in GetActiveObject
dispatch = pythoncom.GetActiveObject(resultCLSID)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
Works fine if itunes is not running
The only recent change I've made is going from 2.7.5 to 2.7.6.
Why is this failing?

Try using the comtypes Python package - it has better support for custom COM objects.
Instead of
try:
itapp= win32com.client.Dispatch('iTunes.Application')
except:
itapp= win32com.client.GetActiveObject('iTunes.Application')
Try this:
from comtypes.client import CreateObject, GetActiveObject
try:
itapp = CreateObject('iTunes.Application')
except:
itapp = GetActiveObject('iTunes.Application')
comtypes: http://sourceforge.net/projects/comtypes/

Related

NI multisim COM interface not accessible

I am trying to open and run a multisim simulation from python using the multisim COM interface.
from research I understand multisim has a COM interface that can be accessed from any other program using any language.
here's an example of it working (partially) with MATLAB
however, I cannot get it to work with python. when I try to open the API I get an error.
here's the Python code, I am using the win32com module:
import win32com.client as win
win.Dispatch("MultisimInterface.MultisimApp")
however, I get a "class not registered" error. the full message is:
Traceback (most recent call last):
File "C:\Users\mizra\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
win.Dispatch("MultisimInterface.MultisimApp")
File "C:\Users\mizra\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
File "C:\Users\mizra\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\mizra\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
pywintypes.com_error: (-2147221164, 'Class not registered', None, None)
It appears for some reason multisim is not opening its API. I have tested the code and tried to open LABVIEW and Excel using the same code and it worked, so I don't know why it doesn't work with multisim.
is there a way to see which interfaces are registered and can be accessed on a given time? will it work better using another language? am I missing something on a much more basic level?
I would appreciate any help. thanks in advance

win32com.client.Dispatch("Photoshop.Application") not working. win32com doesn't detect the installed Photoshop when it runs

When I try to run a script that opens up Adobe Photoshop 2022, I'm met with following errors:
Traceback (most recent call last):
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\XXXXX\XXXXX\XXXXX\XXXXX\Python\Autophoto\Autophoto.py", line 39, in <module>
psApp = win32com.client.Dispatch("Photoshop.Application")
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
IDispatch, None, clsctx, pythoncom.IID_IDispatch
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
The win32com.client.Dispatch("Photoshop.Application") call used to work when an older version of Photoshop was installed, but since I've updated it to the latest version, the code no longer seems to work.
I've tried using the win32com tool to list all COM interfaces but this program seems to crash after a few seconds every time.
Any tips?
(The code is accessible via following link: https://github.com/samualon/Autophoto)

invalid class string using win32com library python

I wrote the flowing to dispatch an excel application. I used the win32 library in python and wrote this:
import win32com.client as win32
import pywinauto
excel = win32.gencache.EnsureDispatch('Excel.Application'
Because of this line i got this error:
K:\venv\Scripts\python.exe K:/test_results.efrat.py
Traceback (most recent call last):
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
win32.types.com_error: (-2147221005, 'Invalid class string', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "K:/test_results.efrat.py", line 8, in <module>
excel = win32.gencache.EnsureDispatch('Excel.Application')
File "K:\venv\lib\site-packages\win32com\client\gencache.py", line 527, in EnsureDispatch
disp = win32com.client.Dispatch(prog_id)
File "K:\venv\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "K:\venv\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
win32.types.com_error: (-2147221005, 'Invalid class string', None, None)
Process finished with exit code 1
Does anyone know what could be the problem? I have pywin32 and pypiwin32 installed in my library packages.
It works fine for me. Remove your pywin32 from site-packages and try installing the pywin32.exe from the following URL :
https://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/

dbm 35 Resource temporarily unavailable on OSX python 2.7

I am using the Flask-WeRobot plugin and I am seeing an error being thrown here:
class FileStorage(SessionStorage):
"""
FileStorage 会把你的 Session 数据以 dbm 形式储存在文件中。
:param filename: 文件名, 默认为 ``werobot_session``
"""
def __init__(self, filename='werobot_session'):
print filename
try:
self.db = dbm.open(filename, "c")
except Exception, e:
print e
raise e
This is what is being outputted:
Traceback (most recent call last):
File "application.py", line 18, in <module>
another_robot = WeRoBot(token='abcdefg', enable_session=True)
File "/Users/vng/Dropbox/Code/Paw/venv/lib/python2.7/site-packages/flask_werobot.py", line 42, in __init__
super(WeRoBot, self).__init__(*args, **kwargs)
File "/Users/vng/Dropbox/Code/Paw/venv/lib/python2.7/site-packages/werobot/robot.py", line 47, in __init__
filename=os.path.abspath("werobot_session")
File "/Users/vng/Dropbox/Code/Paw/venv/lib/python2.7/site-packages/werobot/session/filestorage.py", line 20, in __init__
self.db = dbm.open(filename, "c")
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/anydbm.py", line 85, in open
return mod.open(file, flag, mode)
gdbm.error: (35, 'Resource temporarily unavailable')
At first I thought it would have something to do with permissions, but changing the file, werobot_session didn't work.
I am on OSX running python 2.7. This code works on my ubuntu production server.
Any ideas what's causing this?
We find this bug too and we are working on it now.
It seems that it's a bug of gdbm. And everything works fine on linux.
You can check this issue for further information.
https://github.com/whtsky/WeRoBot/issues/102

Python / ADOX: 'The specified module could not be found.' (win32 extensions)

I'm running pywin32 for python 2.5.
I'm following the instructions for python ADO given at http://www.ecp.cc/pyado.html.
Creating an ADODB.Recordset object works fine. But when I try to create an ADOX.Catalog object I get an error:
>>> cat=win32com.client.Dispatch(r'ADOX.Catalog')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 98, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 78, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
com_error: (-2147024770, 'The specified module could not be found.', None, None)
Any ideas what I might be missing?
Solution: even though ADOX was showing up in the COM browser as an available library, it wasn't "registered" properly. Following the instructions here, I executed the following at the Start->Run prompt:
regsvr32 "C:\Program Files\Common Files\System\ado\msadox.dll"
Note that this is on a WinXP SP2 machine. I guess the registry had become corrupt somehow? In any case, I'm new to Windows programming so my explanation may be off, but maybe the fix will help someone.

Categories

Resources