pythonpython-3.xapplescript

Python AppleScript module fails: AttributeError: 'str' object has no attribute '_unpackresult'


I have been using the AppleScript module in Python for some time now, but its latest version is refusing to work on any but the most trivial code. This is the Python script:

#!/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) ;
CurrComment = applescript.AppleScript.run(AScmd).out
print("Comments: %s" % CurrComment)

The commented-out code works fine. The result:

Running this: 
tell application "Finder"
    set TmpLoc to POSIX file "/Volumes/vData/Users/pkearns/tmp" as alias
    get comment of TmpLoc
end tell

Traceback (most recent call last):
  File "/Volumes/vData/Users/pkearns/tmp/./test-get-comments.py", line 13, in <module>
    CurrComment = applescript.AppleScript.run(AScmd).out
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/applescript/__init__.py", line 90, in run
    return self._unpackresult(*self._script.executeAndReturnError_(None))
           ^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_unpackresult'

The AppleScript code works fine when run in Script Editor.

Has anyone seen this problem and found a solution?


Solution

  • AppleScript.run() expects self to be an AppleScript object. But you're calling AppleScript.run(AScmd) as a class method, not an instance method.

    import applescript
    
    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:", AScmd)
    script = applescript.AppleScript(AScmd)  # create instance
    CurrComment = script.run()               # run instance
    print("Comments:", CurrComment)