16

Python のスクリプト ブリッジで問題が発生しています

iTunes オブジェクトの属性を一覧表示しようとしています

iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")

使用して

>>> from pprint import pprint
>>> from Foundation import *
>>> from ScriptingBridge import *
>>> iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
>>> pprint (vars(iTunes))

私は戻ってきます

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute

誰もこれを回避する方法を知っていますか?

4

4 に答える 4

16

試してみてくださいdir(iTunes)。に似てvarsいますが、オブジェクトでより直接的に使用されます。

于 2013-01-19T04:16:21.880 に答える
9

vars(obj) に似たものについては、obj が dict としてアクセスできない場合、次のような kludge を使用します。

>>> obj = open('/tmp/test.tmp')
>>> print vars(obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_'))

{'softspace': 0, 'encoding': None, 'flush': <built-in method flush of file object at 0xf7472b20>, 'readlines': <built-in method readlines of file object at 0xf7472b20>, 'xreadlines': <built-in method xreadlines of file object at 0xf7472b20>, 'close': <built-in method close of file object at 0xf7472b20>, 'seek': <built-in method seek of file object at 0xf7472b20>, 'newlines': None, 'errors': None, 'readinto': <built-in method readinto of file object at 0xf7472b20>, 'next': <method-wrapper 'next' of file object at 0xf7472b20>, 'write': <built-in method write of file object at 0xf7472b20>, 'closed': False, 'tell': <built-in method tell of file object at 0xf7472b20>, 'isatty': <built-in method isatty of file object at 0xf7472b20>, 'truncate': <built-in method truncate of file object at 0xf7472b20>, 'read': <built-in method read of file object at 0xf7472b20>, 'readline': <built-in method readline of file object at 0xf7472b20>, 'fileno': <built-in method fileno of file object at 0xf7472b20>, 'writelines': <built-in method writelines of file object at 0xf7472b20>, 'name': '/tmp/test.tmp', 'mode': 'r'}

次のように関数を除外するなど、これを改善できると確信していますif not callable(getattr(obj, attr)

>>> print dict([attr, getattr(obj, attr)] for attr in dir(obj) if not attr.startswith('_') and not callable(getattr(obj, attr)))
{'errors': None, 'name': '/tmp/test.tmp', 'encoding': None, 'softspace': 0, 'mode': 'r', 'closed': False, 'newlines': None}
于 2015-07-05T02:59:46.267 に答える
0

これはかなり遅れていますが、別の問題(ただし同じエラー)の場​​合、次のことがうまくいきました:

json.dumps(your_variable)

この前に、スクリプトに JSON をインポートしていることを確認してください。

import json

JSON をきれいな形式で読み取る方法を見つける必要があります。

于 2013-12-26T09:22:26.183 に答える