Calling AppleScript from Python without using osascript or appscript?

前端 未结 2 981
鱼传尺愫
鱼传尺愫 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

    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
    

提交回复
热议问题