Calling AppleScript from Python without using osascript or appscript?

前端 未结 2 945
鱼传尺愫
鱼传尺愫 2021-01-31 11:42

Is there any way to execute (and obtain the results of) AppleScript code from python without using the osascript command-line utility or appscript (which I don\'t r

相关标签:
2条回答
  • 2021-01-31 12:21

    You can use the PyObjC bridge:

    >>> from Foundation import *
    >>> s = NSAppleScript.alloc().initWithSource_("tell app \"Finder\" to activate")
    >>> s.executeAndReturnError_(None)
    
    0 讨论(0)
  • 2021-01-31 12:21

    PyPI is your friend...

    http://pypi.python.org/pypi/py-applescript

    Example:

    import applescript
    
    scpt = applescript.AppleScript('''
        on run {arg1, arg2}
            say arg1 & " " & arg2
        end run
    
        on foo()
            return "bar"
        end foo
    
        on Baz(x, y)
            return x * y
        end bar
    ''')
    
    print(scpt.run('Hello', 'World')) #-> None
    print(scpt.call('foo')) #-> "bar"
    print(scpt.call('Baz', 3, 5)) #-> 15
    
    0 讨论(0)
提交回复
热议问题