python - 从 Applescript 运行 Python 脚本时出错

标签 python shell applescript

为什么这不起作用?

苹果脚本:

set script_path to "$HOME/Desktop"
do shell script "python & script_path & hello_world.py"

Python 脚本:

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
 <head>
  <title>Python - Hello World</title>
 </head>
 <body>
  Hello World
 </body>
</html>

“”“

错误:

tell current application do shell script "python & script_path & hello_world.py" --> error "sh: script_path: command not found sh: hello_world.py: command not found" number 127 Result: error "sh: script_path: command not found sh: hello_world.py: command not found" number 127

最佳答案

& 符号连接字符串文字和变量替换。就像这样:

set script_path to "$HOME/Desktop"
do shell script "python " & script_path & "/hello_world.py"

您还需要在 hello_world.py 之前或 $HOME/Desktop 末尾添加斜杠。上面的示例将其显示在 hello_world.py 之前。

关于python - 从 Applescript 运行 Python 脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16126058/

相关文章:

cocoa - 以编程方式更改 macOS Mojave 强调色

python - 使用 OpenSSL 发送下载页面 - Python

python - 如何访问 python 包?

python - python中带有SyncManager类的多进程优先级队列的队列函数

python - 我的pip正在安装Python3.4中的所有模块

shell - 如何在 crystal-lang 中获取 Process.exec 的输出?

wordpress - 如何从Shell脚本向docker运行函数和变量?

linux - linux shell 中 case 的复杂表达式

Swift:使用国际格式的字符串执行 Bash 命令

performance - Applescript - 迭代库中所有 iTunes 轨道的性能不佳