Python调用AppleScript模块报错:'str'对象无'_unpackresult'属性
问题解决:Python applescript模块调用报错AttributeError: 'str' object has no attribute '_unpackresult'
问题原因
你出错的核心原因是错误地直接以类方法的方式调用applescript.AppleScript.run(),但run()是AppleScript类的实例方法,需要先创建AppleScript实例后再调用。你注释掉的两行代码是正确的实例调用方式,而报错的代码把字符串直接传给了run()类方法,导致方法内部把传入的字符串当作了self参数,自然找不到_unpackresult属性。
解决方案
修正调用逻辑,先创建AppleScript实例,再调用其run()方法,以下是修正后的完整代码:
#!/opt/local/bin/python3 import applescript,xattr,os #This works: applescript.AppleScript('say "Hello AppleScript"').run() #This works: applescript.AppleScript('display dialog "Hello AppleScript"').run() AScmd = ''' tell application "Finder" set TmpLoc to POSIX file "/Volumes/vData/Users/pkearns/tmp" as alias get comment of TmpLoc end tell ''' print("Running this: %s" % AScmd) ; # 修正:先实例化AppleScript对象,再调用run方法 CurrComment = applescript.AppleScript(AScmd).run().out print("Comments: %s" % CurrComment)
也可以拆分成更清晰的写法:
# 拆分写法 script = applescript.AppleScript(AScmd) run_result = script.run() CurrComment = run_result.out
这样修改后,代码会和你注释掉的测试代码逻辑一致,能够正确执行Finder的AppleScript代码并获取文件夹的注释信息。
内容的提问来源于stack exchange,提问作者Philip Kearns
相关产品推荐
相关产品推荐

