如何将多个参数传递给osascript?

时间:2018-02-21 20:56:34

标签: macos shell arguments applescript

我已经获得了以下使用osascript命令的shell脚本:

#!/usr/bin/osascript
on run argv
  tell application "Terminal"
    activate
    do script "echo " & quoted form of (item 1 of argv) & " " & quoted form of (item 2 of argv)
  end tell
end run

但是当我运行时,代码仅限于打印2个第一个参数。

E.g。运行./test.sh foo bar buzz ...时,我希望显示所有参数。

如何将上述代码转换为支持多个无限数量的参数?当我没有指定时,它会不会中断?

2 个答案:

答案 0 :(得分:2)

默认情况下, AppleScript的 text item delimiters{},除非将其设置为其他默认 脚本< / em>在此之前并且不应该在操作之后直接重置,或者您只是没有使用 AppleScripts的 text item delimiters,那么这是一种方法来实现它而无需明确使用像例如的代码set {TID, text item delimiters} to {text item delimiters, space}set text item delimiters to TID

#!/usr/bin/osascript

on run argv

    set argList to {}
    repeat with arg in argv
        set end of argList to quoted form of arg & space
    end repeat

    tell application "Terminal"
        activate
        do script "echo " & argList as string
    end tell

end run

答案 1 :(得分:1)

你必须添加一个重复循环来将参数列表映射到它们的quoted form,然后将列表加入一个空格分隔的字符串text item delimiters

#!/usr/bin/osascript
on run argv
    set argList to {}
    repeat with arg in argv
        set end of argList to quoted form of arg
    end repeat
    set {TID, text item delimiters} to {text item delimiters, space}
    set argList to argList as text
    set text item delimiters to TID

    tell application "Terminal"
        activate
        do script "echo " & argList
    end tell
end run