在Applescript中执行终端命令

时间:2012-09-06 05:06:42

标签: applescript

我在AppleScript中执行了terrminal commnand“system_profiler SPApplicationsDataType”以获取MAC中安装的应用程序,我需要解析输出,请解释如何通过Apple脚本或Python实现它。

1 个答案:

答案 0 :(得分:0)

在Python中,您可以使用子流程 -

http://docs.python.org/library/subprocess.html

具体来说,check_output方法:http://docs.python.org/library/subprocess.html#subprocess.check_output

所以你只需要使用:

output = subprocess.check_output(["system_profiler", "SPApplicationsDataType"])
print output # or do something else with it

您的命令将被分解为一个数组,每个参数都有一个元素。

在Applescript中,您可以执行以下操作:

set textReturned to (do shell script "system_profiler SPApplicationsDataType")

tell application "TextEdit"
    activate
    make new document at the front
    set the text of the front document to textReturned
end tell

这将运行命令并将输出转储到TextEdit。

相关问题