ken*_*orb 5 macos shell applescript arguments
我有以下使用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
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时,代码仅限于打印 2 个第一个参数。
例如,运行时./test.sh foo bar buzz ...,我希望显示所有参数。
如何将上述代码转换为支持多个无限数量的参数?当我不指定任何内容时它不会中断?
默认情况下,AppleScript 的 text item delimitersis {},除非在此之前将其设置为其他脚本中的默认值,并且在操作后不会直接重置,或者您只是没有使用AppleScripts 的,那么这里有一种方法可以做到这一点,而无需必须显式使用例如和这样的代码: text item delimitersset {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
Run Code Online (Sandbox Code Playgroud)
您必须添加一个重复循环以将参数列表映射到它们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
Run Code Online (Sandbox Code Playgroud)