将多个参数从 Applescript 传递到终端命令脚本

mmi*_*313 2 parameters macos bash terminal applescript

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

#!/bin/bash

var=$1

var=$2
Run Code Online (Sandbox Code Playgroud)

我一直在使用的 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
Run Code Online (Sandbox Code Playgroud)

我认为我出错的地方是第二个参数的输入。当我只有一个参数时,它运行得很好:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*ehe 5

您只需要在参数之间添加一个空格。现在,thisFile和之间没有添加空格testTarget。您的命令如下所示:

/Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/
Run Code Online (Sandbox Code Playgroud)

将您的 shell 脚本行更改为:

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
Run Code Online (Sandbox Code Playgroud)

在构建脚本时,我发现有帮助的事情是在运行它们之前确保我的 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
Run Code Online (Sandbox Code Playgroud)