【发布时间】:2015-12-17 13:11:32
【问题描述】:
所以,我有一个问题。我从网上下载了一个程序。它是一个命令行应用程序。我编写了一个代码,它为应用程序生成了一些n-k 命令。我已将它们写入输出文件。我可以用 Python 编写一个应用程序,但它会冻结某些命令。我已经手动测试了它们,似乎有两个问题:
- 命令必须一个接一个地运行;
- 有些命令会给出像
bla-bla-bla这样的输出,这个东西没有写入输出文件。所以,如果我运行命令./app -p /file1 -o /file2 -s -a smth- > /fileOutput.txtfileOutput.txt是空的,虽然在终端中,有这个bla-bla-bla消息,说明有问题。如果命令给出bla-bla-bla,应用程序可能会冻结一段时间。
这是我想做的:
-
CD放入文件夹,包含应用程序; -
For command in fileWithCommands执行命令并开始下一个,只有在上一个完成时; - 如果命令给出的消息包含
bla-bla-bla(因为它可能看起来像file1 bla-bla-bla),请将命令和这个奇怪的输出写入文件 badOutputs.txt。
以前从未做过applescript。然而,这就是我到目前为止所做的:
set theFile to "/Users/MeUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
#I have found the previous code here: http://alvinalexander.com/mac-os-x/applescript-read-file-into-list-array-examples
close access fileHandle
tell application "Terminal"
activate
do script "cd /Users/MeUser/Desktop/anApp/"
repeat with command in arrayCommand
do script command
end repeat
end tell
虽然有一个问题,如果在一个窗口中的命令组成了一个巨大的队列。没有window 1 cd 并且该命令位于不同的窗口中。而且我仍然无法保存输出。
更新
按照 @Mark Setchell 的建议进行。所以现在我有这样的代码:
set theFile to "/Users/meUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
close access fileHandle
repeat with command in arrayCommand
do shell script "cd /Users/meUser/Desktop/App/; " & command
end repeat
在命令中我添加了以下内容:
2>&1 /Users/meUser/Desktop/errorOut.txt
但是,苹果脚本说应用程序的错误是脚本的错误。即:文件损坏,应用程序失败。我希望它写入失败的错误文件并移动到下一个命令,而脚本只是失败了。
【问题讨论】:
-
@MarkSetchell 谢谢,它可以工作,虽然有点不清楚,如何将命令和错误写入同一个文件?我需要获取它,例如查看哪个文件已损坏或不合适。另外,如何将这些输出附加到一个文件中?我刚刚手动测试过,它会重写文件。
标签: terminal applescript