AppleScript 存储两个文件的差异 returns 一个错误

AppleScript to store diff of two files returns an error

我有两个简单的 txt 文件,a.txt 和 b.txt。

a.txt

bla

b.txt

bla
bla

接下来我想将 diff 的输出存储在这样的变量中:

set my_result to do shell script "diff a.txt b.txt"

这一切看起来很简单,但是returns出现以下错误:

error "1c1,2
< bla
\ No newline at end of file
---
> bla
> bla" number 1

如何避免这种情况,并将结果存储为字符串?

如果文件不匹配,/usr/bin/diff returns 值 ≠ 0 并且在 stderr 中显示错误消息。

如果 shell 脚本 returns 非零值,AppleScript 会抛出错误,因此您必须捕获错误并获取错误消息

try
    set my_result to do shell script "diff a.txt b.txt"
    -- my_result is empty if the files match
on error e
    set my_result to e
end try