Reputation: 166833
I've got the following shell script which uses osascript
command:
#!/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
However when I run, the code is only limited to print 2 first arguments.
E.g. when running ./test.sh foo bar buzz ...
, I expect all the arguments to be displayed.
How I can convert the above code to support multiple unlimited number of arguments? And it won't break when I specify none?
Upvotes: 5
Views: 5996
Reputation: 7555
By default, AppleScript's text item delimiters
is {}
and unless is was set to other then the default elsewhere in the script before this and not reset as one should directly after the manipulation, and or you just are not using AppleScripts's text item delimiters
, then here is a way to do it without having to explicitly use code like e.g. set {TID, text item delimiters} to {text item delimiters, space}
and 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
Upvotes: 5
Reputation: 285250
You have to add a repeat loop to map the argument list to their quoted form
and then join the list to a space separated string with 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
Upvotes: 3