繁体   English   中英

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

[英]How to pass multiple arguments to osascript?

我有以下使用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个第一个参数。

例如,当运行./test.sh foo bar buzz ... ,我希望显示所有参数。

我如何转换上面的代码来支持多个无限数量的参数? 当我没有指定时它不会破坏?

默认情况下, AppleScript的 text item delimiters{} ,除非将其设置为other,然后在此之前的脚本中的其他位置默认 ,并且不应该在操作后直接重置,或者您只是不使用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

您必须添加一个重复循环以将参数列表映射到它们的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM