Applescript 在 do shell 脚本上返回 "The command exited with a non-zero status"

标签 applescript

我正在使用我正在尝试制作的Applescript,在它执行命令后检查命令的结果,但是当我执行它时,它会提示一条消息(在AppleScript编辑器中); 预期的表达,但发现“错误”。

如何回退并检查该命令是否与 The command exited with a non-zero status. 相同,以便在与错误消息相同时我可以执行其他操作?

do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges

if error = "The command exited with a non-zero status." then
    display dialog "Returned zero"
else if result = "The command exited with a non-zero status." then
    display dialog "Returned zero"
else if result = "" then
    do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
    display dialog "Memcached Started"
else
    do shell script "killall memcached" with administrator privileges
    display dialog "Memcached Stopped"
end if

编辑:更新版本

set error to do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges

if error = "The command exited with a non-zero status." then
    do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
    display dialog "Memcached Started"
else
    do shell script "killall memcached" with administrator privileges
    display dialog "Memcached Stopped"
end if

最佳答案

您的特殊问题是使用“错误”一词作为变量。 Error对于applescript来说是一个特殊的词,所以它不能用作变量。

但是,即使您解决了该问题,您的代码仍然无法正常工作。您可以使用 try block 捕获错误消息。请注意,“theError”包含错误消息...

try
    do shell script "echo \"stats\" | nc localhost 11211" password "~password~" with administrator privileges
on error theError
    if theError is "The command exited with a non-zero status." then
        do shell script "memcached -d -l 127.0.0.1 -p 11211 -m 64"
        display dialog "Memcached Started"
    else
        do shell script "killall memcached" with administrator privileges
        display dialog "Memcached Stopped"
    end if
end try

关于Applescript 在 do shell 脚本上返回 "The command exited with a non-zero status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558435/

相关文章:

xcode - 有没有办法在 Xcode 中列出所有文件及其目标?

c - Applescript 或 Automator 从网站逐步保存 Mandelbrot 图像

ffmpeg - Applescript 使用 ffmpeg 批量转换视频

cocoa - 使用 Cocoa 将文件夹挂载为 Finder 中的设备

objective-c - Cocoa:应用程序脚本文件夹

objective-c - 苹果事件 : Send port for process has no send right

macos - 在 Mac 上打开 Google Chrome 的键盘快捷键

html - 将 HTML 源渲染为图像(png、gif) - Cocoa、Applescript、Automator 一切都受欢迎

variables - 如何在 AppleScript 中使用变量(从 Automator 中调用)

macos - 如何检查应用程序是否通过 MAC OS 中的 Automator 打开?