我有以下代码
on open the_Droppings
-- set something to {item 1 of the_Droppings, item 2 of the_Droppings}
set file1 to POSIX path of item 1 of the_Droppings
set file2 to POSIX path of item 2 of the_Droppings
set diff to (do shell script "diff " & file1 & " " & file2)
if (diff is equal to "") then
display dialog "files are the same"
else
set diff to ""
tell application "TextEdit"
activate
set NewDoc to make new document
set diff to text of NewDoc
end tell
end if
end open
end
它有两个问题!
一:它打开一个很长的对话框。很久以至于我甚至无法退出它。 (我知道我可以点击返回键)
问题,我该如何停止对话框?
二:它永远不会将文本放在它打开的新文本中。
答案 0 :(得分:2)
您的对话框不是输出对话框,它是错误对话框。问题是如果diff
找到差异(0表示没有差异,1表示差异,2表示根据this question的程序错误),do shell script
将退出错误代码,Applescript认为这是{diff
的失败1}}命令并帮助显示调试输出,当然包含完整的差异。但是,它永远不会分配给您的bash
变量,因为它会触发错误。
假设您的shell为set diff to (do shell script "diff '" & file1 & "' '" & file2 & "'; [[ $? = 1 ]] && exit 0")
,请执行以下操作
stdout
将解决问题 - 您取消退出代码1,AppleScript乐意选择quoted form of POSIX path
上的输出并将其分配给您的变量(注意我在文件路径中添加了引号 - 您也可以使用{{1 }})。要通过AppleScript将其插入到新的TextEdit文档中,您还必须反转您的作业as per my comment,即
set text of NewDoc to diff -- not "set diff to text of NewDoc"
这应解决所有问题。
答案 1 :(得分:0)
当do shell脚本显示对话框时,问题是shell命令没有返回0.错误的原因可能是您没有使用引用的形式。您还可以将文本管道传输到文本编辑器。
on open these_Items
set file1 to quoted form of POSIX path of item 1 of these_Items
set file2 to quoted form of POSIX path of item 2 of these_Items
do shell script "diff " & file1 & " " & file2 & " | open -f -e"
end open
答案 2 :(得分:-1)
这是另一种方法:
set resultsPath to "~/Desktop/results.txt"
try
do shell script "diff " & file1 & space & file2 & " >" & resultsPath
end try
set yyy to do shell script "cat " & resultsPath
if yyy is "" then
display dialog "files are the same"
else
do shell script "open " & resultsPath
end if