0

我有以下代码

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

有两个问题!一:它打开一个很长的对话框。这么久,我什至不能点击确定退出它。(我知道我可以按回车键) 问题,如何停止对话框?二:它永远不会将文本放入它打开的新文本编辑器中。

4

3 回答 3

2

您的对话框不是输出对话框,而是错误对话框。问题是,diff如果发现差异(0 是没有差异,1 是差异,2 是根据这个问题的程序错误),则以错误代码退出,Applescript 认为这是do shell script命令失败并有助于显示输出以进行调试,其中当然包含完整的差异。但是,它永远不会分配给您的diff变量,因为它触发了错误。

假设您的 shell 是bash,请执行以下操作

set diff to (do shell script "diff '" & file1 & "' '" & file2 & "'; [[ $? = 1 ]] && exit 0")

将解决这个问题——你取消退出代码 1 并且 AppleScript 愉快地拾取输出stdout并将其分配给你的变量(注意我在你的文件路径中添加了引号——你也可以使用quoted form of POSIX path)。要通过 AppleScript 将其插入到新的 TextEdit 文档中,您还必须根据我的评论反转您的分配,即

set text of NewDoc to diff -- not "set diff to text of NewDoc"

那应该可以解决所有问题。

于 2012-05-03T10:50:19.707 回答
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
于 2012-05-03T10:37:49.047 回答
-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
于 2012-05-03T12:47:19.200 回答