要解决Applescript无法获取Python调用的返回结果的问题,可以尝试使用以下方法:
print
语句将结果输出到标准输出。然后,在Applescript中使用do shell script
命令来调用Python脚本,并将结果赋值给一个变量。以下是一个示例:Python脚本(test.py):
result = "Hello from Python!"
print(result)
Applescript代码:
set result to do shell script "python /path/to/test.py"
display dialog result
Python脚本(test.py):
result = "Hello from Python!"
with open("/path/to/result.txt", "w") as file:
file.write(result)
Applescript代码:
set filePath to "/path/to/result.txt"
set fileContents to read file filePath
display dialog fileContents
do script
命令:在Applescript中直接调用Python脚本,并将结果赋值给一个变量。以下是一个示例:Applescript代码:
set result to do script "python -c \"print('Hello from Python!')\""
display dialog (result as string)
这些方法中的每一种都有其优缺点,具体使用哪种方法取决于你的具体需求和环境。