我正在使用我正在尝试制作的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/