2015-03-03 201 views
0

我一直在想如何将多个参数从Applescript传递给终端命令脚本。例如运行终端命令文件时,您可以以编程方式接收参数,像这样:从AppleScript传递多个参数到终端命令脚本

#!/bin/bash 

var=$1 

var=$2 

,我一直在使用的AppleScript的代码如下供参考:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me)) 

set thisFile to "Dev" 

set testTarget to "/Users/lab/Desktop/TestTarget/" 

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges 

在哪里,我想我出错了是第二个参数的输入。当我只有一个参数时,它通过了很好:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges 

我很好奇什么正确的语法将传递在这第二个参数。如果有人有任何建议,请让我知道!另外如果您需要更多信息,我会很乐意提供!

回答

1

你只需要在你的参数之间添加一个空格。目前,在thisFiletestTarget之间没有添加空间。你的命令如下:

/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/ 

更改你的shell脚本行:

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges 

东西,我建立一个脚本时,找到有用的就是确保我的shell命令运行之前正确的。因此,不要直接构建它,请将该命令存储在变量中并记录下来。稍后,将日志语句替换为do shell script命令。

set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges 
log shellScript 
-- do shell script shellScript 
+0

这工作!我会upvote这篇文章,但显然我没有足够的stackoverflow点:(但非常感谢你! – mmilutinovic1313 2015-03-03 14:56:21

+0

@ mmilutinovic1313:你不需要声望点_accept_ _yours_问题的答案(点击复选标记)。 – mklement0 2015-03-03 17:10:56

+1

另一个值得一提的提示是:为了确保字符串(如文字)文件名被传递到未经修改的shell(为了保护字符串不受shell扩展),以“'quoted form of”作为前缀。 – mklement0 2015-03-03 17:13:09

0

这是在@Darrick Herwehe's中发布的答案的简短描述。我只需要添加空格关键字和命令完美工作。非常感谢!

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges 
相关问题