代码之家  ›  专栏  ›  技术社区  ›  Ho1m3s

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

  •  0
  • Ho1m3s  · 技术社区  · 7 年前

    当我连接到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
    

    我假设问题是我在terminal命令中使用的反斜杠。 我怎样才能解决这个问题?

    非常感谢! 干杯

    1 回复  |  直到 7 年前
        1
  •  0
  •   vadian    7 年前

    您需要另一个反斜杠来避开第一个反斜杠。
    还要考虑以下两个空格字符 d\\ -f2 以及后面的空格字符 inet

    这个 Terminal 不需要tell块

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