使用命令 `osascript -e 'quit app "Quicktime Player 7"'` 和 python
use command `osascript -e 'quit app "Quicktime Player 7"'` with python
我在 OSX 终端中使用 osascript -e 'quit app "Quicktime Player 7"'
关闭 Quicktime Player 7 应用程序,该应用程序运行良好,但无法使用 python 使同一命令正常工作。我做错了什么?
这只是运行,但什么都不做:
command = ['osascript', '-e', 'quit app', 'Quicktime Player 7']
p = subprocess.Popen(command)
将 quit app
和 Quicktime Player 7
作为两个列表元素会将命令 subprocess.Popen
执行转换为如下内容:
osascript -e 'quit app' 'Quicktime Player 7'
osascript
期望 -e
之后的参数为 "one line of a script"(参见 osascript
的 man-page)。
拆分参数会导致 osascript
执行 quit app
并将 Quicktime Player 7
解释为参数,因此可能会忽略它。
一个简单的修复如下:
command = ['osascript', '-e', 'quit app "Quicktime Player 7"']
p = subprocess.Popen(command)
如果您一开始不想使用 lists/splitting 命令,您可以使用 shlex.split
为您完成工作:
import shlex
command = shlex.split("osascript -e 'quit app \"Quicktime Player 7\"'")
p = subprocess.Popen(command)
我在 OSX 终端中使用 osascript -e 'quit app "Quicktime Player 7"'
关闭 Quicktime Player 7 应用程序,该应用程序运行良好,但无法使用 python 使同一命令正常工作。我做错了什么?
这只是运行,但什么都不做:
command = ['osascript', '-e', 'quit app', 'Quicktime Player 7']
p = subprocess.Popen(command)
将 quit app
和 Quicktime Player 7
作为两个列表元素会将命令 subprocess.Popen
执行转换为如下内容:
osascript -e 'quit app' 'Quicktime Player 7'
osascript
期望 -e
之后的参数为 "one line of a script"(参见 osascript
的 man-page)。
拆分参数会导致 osascript
执行 quit app
并将 Quicktime Player 7
解释为参数,因此可能会忽略它。
一个简单的修复如下:
command = ['osascript', '-e', 'quit app "Quicktime Player 7"']
p = subprocess.Popen(command)
如果您一开始不想使用 lists/splitting 命令,您可以使用 shlex.split
为您完成工作:
import shlex
command = shlex.split("osascript -e 'quit app \"Quicktime Player 7\"'")
p = subprocess.Popen(command)