Can't do shell script with a repeat with i from 1 to n loop
Try this to avoid having to use the temporary file.
to run argv set accumulator to do shell script "echo " & (count argv) & " arguments" altering line endings false repeat with i from 1 to (count argv) set ln to do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'" altering line endings false set accumulator to accumulator & ln end repeat return accumulatorend run
Your problem appears to be unrelated to the loop, or the use of argv, for that matter. Here's a much simpler test case where only the last do shell script
actually returns a result:
do shell script "echo foo"delay 2do shell script "echo bar"
In addition, the following slight change will produce expected results:
to run argv do shell script "echo " & (count argv) & " arguments > /test.txt" repeat with i from 1 to (count argv) do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /test.txt" end repeatend run
test.txt
will contain four lines, like so:
3 argumentsArgument 1: fooArgument 2: barArgument 3: baz
This workaround fails:
to run argv do shell script "echo " & (count argv) & " arguments > /tmp/foo.txt" repeat with i from 1 to (count argv) do shell script "echo 'Argument " & i & ": " & (item i of argv) & "' >> /tmp/foo.txt" end repeat do shell script "cat /tmp/foo.txt" do shell script "rm /tmp/foo.txt"end run
Even now, only the last line is returned. This may be related to the following question of TN2065:
Q: My script will produce output over a long time. How do I read the results as they come in?
A: Again, the short answer is that you don’t — do shell script will not return until the command is done. In Unix terms, it cannot be used to create a pipe. What you can do, however, is to put the command into the background (see the next question), send its output to a file, and then read the file as it fills up.
Alas, I don't have enough AppleScript-fu to know how to have AppleScript itself read multiple lines, which I suspect would work.