在 AppleScript 的显示对话框中使用反斜杠

Using backslash in display dialog in AppleScript

当我连接到 WiFi 网络时,我正在使用 AppleScript 输出我机器的 IP 地址。 在 Bash 脚本中,我使用了这个命令:

ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2

但是当我在 AppleScript 中使用相同的命令时,它会报错。 这是 AppleScript:

tell application "Terminal"
activate
set IPAddr to do shell script "ifconfig | grep \"inet\" | grep -v 127.0.0.1 | cut -d\ -f2"
display dialog IPAddr
end tell

在结果中,会显示:

 error "Terminal got an error: User canceled." number -128

我假设问题是我在终端命令中使用的反斜杠。 我该如何解决这个问题?

非常感谢! 干杯

您需要另一个反斜杠来转义第一个反斜杠。
还要考虑 d\-f2 之间的两个 space 字符以及 inet.

之后的 space 字符

不需要 Terminal 告诉块

set IPAddr to do shell script "ifconfig | grep \"inet \" | grep -v 127.0.0.1  | cut -d\  -f2"
display dialog IPAddr