Can't do shell script with a repeat with i from 1 to n loop

三世轮回 提交于 2019-11-30 23:46:47

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 accumulator
end 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 2
do 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 repeat
end run

test.txt will contain four lines, like so:

3 arguments
Argument 1: foo
Argument 2: bar
Argument 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!