Question

This works (prints, for example, “3 arguments”):

to run argv
    do shell script "echo " & (count argv) & " arguments"
end run

This doesn't (prints only “Argument 3: three”, and not the previous two arguments):

to run argv
    do shell script "echo " & (count argv) & " arguments"

    repeat with i from 1 to (count argv)
        do shell script "echo 'Argument " & i & ": " & (item i of argv) & "'"
    end repeat
end run

In both cases, I'm running the script using osascript on Mac OS X 10.5.5. Example invocation:

osascript 'Script that takes arguments.applescript' Test argument three

I'm not redirecting the output, so I know that the script is not throwing an error.

If I add a display dialog statement above the do shell script, it throws a “no user interaction allowed” error, so I know that it is executing the loop body.

What am I doing wrong? What is it about this loop that causes osascript to not print anything?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top