如何制作" some_command"在Applescript中工作?

时间:2014-10-17 19:17:34

标签: bash shell command sh which

我正在编写一个使用sox的Applescript,默认情况下在MacOS X中不可用。我的所有内容都在我的系统上工作,但我希望该脚本适用于其他任何人。因为sox命令不是Applescript可以自动定位它的地方,所以我的硬编码'做shell脚本'字符串必须以" / usr / local / bin / sox"开头。而不只是" sox。"

所以,我需要一种方法来通过Applescript在任何系统上找到sox。在终端," sox"返回" / usr / local / bin / sox"没有问题。在Applescript," sox"返回" sh:找不到哪个sox:命令。"对于任何命令都是如此,而不仅仅是sox。

我尝试过所有我能想到的变化,但没有成功。我将列出我使用的每个语法和我得到的结果。如果有人知道从Applescript中获取sox路径的另一条/成功路线,请告诉我。

请注意,我在脚本中有其他shell命令(例如rm,mv等)。

返回" sh的行:找不到哪个sox:命令" :(注意:我还尝试了每个" sox"由which_sox替换,以及前一行定义which_sox为" sox")

do shell script quoted form of "which sox"
do shell script (quoted form of "which sox") as text
do shell script (quoted form of "which sox") as string
tell current application to do shell script quoted form of "which sox"
tell current application to do shell script (quoted form of "which sox") as text
tell current application to do shell script (quoted form of "which sox") as string

返回" sh:/ bin / sh的行sox:没有这样的文件或目录"

do shell script quoted form of "/bin/sh which sox"
do shell script (quoted form of "/bin/sh which sox") as text
do shell script (quoted form of "/bin/sh which sox") as string
tell current application to do shell script quoted form of "/bin/sh which sox"
tell current application to do shell script (quoted form of "/bin/sh which sox") as text
tell current application to do shell script (quoted form of "/bin/sh which sox") as string

(注意:我还尝试过/ bin / bash,/ usr / local / bin / sh和/ usr / local / bin / bash,以及#34;引用"以及定义的which_sox变量。 )

是的,这是一个有条不紊的试验和(全部)错误,只需简单地获取已安装命令的路径。请放弃Applescript之外的方法的任何答案/建议,因为这必须在iTunes中作为脚本运行。此外,请在发布之前尝试任何提案。我看到很多答案'在这个被回复的网站上,你的建议没有成功。'

2 个答案:

答案 0 :(得分:1)

我终于找到了解决方案。以下命令返回已安装的BASH命令的路径(在本例中为sox):

tell me to set sox_path to (do shell script "eval $(/usr/libexec/path_helper -s); which sox")

如果您想知道为什么会这样,请阅读AppleScript : error "sh: lame: command not found" number 127

答案 1 :(得分:0)

quoted form of仅适用于变量。由于您有一个文字命令要执行,您只需要

do shell script "which sox"