无法使用 i 从 1 到 n 循环重复执行 shell 脚本
这有效(例如,打印“3 个参数”):
to run argv
do shell script "echo " & (count argv) & " arguments"
end run
这不起作用(仅打印“参数 3:三个”,而不是前两个参数):
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
在这两种情况下,我都使用 运行脚本Mac OS X 10.5.5 上的 osascript
。 调用示例:
osascript 'Script that takes arguments.applescript' Test argument three
我没有重定向输出,所以我知道脚本没有抛出错误。
如果我在 do shell 脚本
上方添加一个 display dial
语句,它会抛出“不允许用户交互”错误,因此我知道它正在执行循环体。
我究竟做错了什么? 这个循环导致 osascript 不打印任何内容的原因是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样做以避免使用临时文件。
Try this to avoid having to use the temporary file.
您的问题似乎与循环或 argv 的使用无关。 这是一个更简单的测试用例,其中只有最后一个
do shell script
实际上返回结果:此外,以下细微更改将产生预期结果:
test.txt
将包含四个行,如下所示:此解决方法失败:
即使现在,也仅返回最后一行。 这可能与TN2065的以下问题有关:
唉,我没有足够的 AppleScript-fu 来知道如何让 AppleScript 本身读取多行,我怀疑这会起作用。
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:In addition, the following slight change will produce expected results:
test.txt
will contain four lines, like so:This workaround fails:
Even now, only the last line is returned. This may be related to the following question of TN2065:
Alas, I don't have enough AppleScript-fu to know how to have AppleScript itself read multiple lines, which I suspect would work.