有没有简单的方法可以使用像Apple的那样:
set theText to text returned of (display dialog "Please insert Text here:" default answer "" with title "exchange to python" with icon 1)
并在python中使用“theText”变量?
答案 0 :(得分:1)
您还可以使用AppleScript的命令行输入运行python脚本:
--make sure to escape properly if needed
set pythonvar to "whatever"
set outputvar to (do shell script "python '/path/to/script' '" & pythonvar & "'")
Ned的例子让python调用AppleScript,然后将控制权返回到python,这是另一种方式。然后在Python访问参数列表中:
import sys
var_from_as = sys.argv[1] # for 1rst parameter cause argv[0] is file name
print 'this gets returned to AppleScript' # this gets set to outputvar
答案 1 :(得分:0)
有很多方法可以做到这一点。可能最简单的方法是使用OS X osascript
command line utility在子进程中运行脚本,因为它不依赖于任何第三方Python模块。默认情况下,osascript
将AppleScript执行的任何输出返回到stdout,然后可以在Python中读取。您可以在Python交互式解释器中试用它。
使用Python 3.4.1:
>>> import subprocess
>>> theText = subprocess.check_output(['osascript', '-e', \
r'''set theText to text returned of (display dialog "Please insert Text here:" default answer "" with title "exchange to python" with icon 1)'''])
>>> theText
b'Hell\xc3\xb6 W\xc3\xb2rld!\n'
>>> print(theText.decode('UTF-8'))
Hellö Wòrld!
使用Python 2.7.7:
>>> theText
'Hell\xc3\xb6 W\xc3\xb2rld!\n'
>>> print(theText.decode('UTF-8'))
Hellö Wòrld!
对于真实世界的应用程序,您可能希望进行一些错误检查和/或异常捕获。