AppleScript:IF 命令没有执行
AppleScript:IF command doesn't execute
我是 AppleScript 的初学者,所以我真的不是很了解。我一直在尝试让用户 select 从一系列情况中做出选择,然后根据他们的选择做出适当的回应。但是,我 运行 遇到了一些问题:
- 脚本 运行s 但不显示通知。
- 有没有比链接大量 IF 语句更好的方法?
PS: 我离完成整个剧本还差得很远。
on run
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
return the result as string
if string is Thunderstorm then display notification "hello"
end run
您对所查看的示例代码的作用做出了一些很好的猜测;但其中一些猜测是错误的。随着时间的推移,您会更好地了解 AppleScript 的工作原理。
您已在 choose from list
行中将情境名称正确输入为字符串,并用引号将其文本括起来。但是在检查它是否是雷雨时,您不会用引号括起该文本。 AppleScript 正在寻找一个名为 Thunderstorm
的变量来获取其内容,而不是寻找文本字符串“Thunderstorm”。
return the result as string
不会创建名为 string
的变量,而是结束名为 run
的处理程序,returning the result
以任何名为 run
的代码,作为文本字符串。要将 choose from list
的结果分配给变量,请使用 set theSituation to the result as string
.
以下是您的代码如何更好地工作的示例:
on run
--get the situation we're dealing with
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
copy the result as string to theSituation
--Thunderstorms require that the user be immediately notified
if theSituation is "Thunderstorm" then display notification "hello"
end run
由于您将此作为 run
处理程序,您可能不想 return 任何结果,因为您很少调用 运行 处理程序。但是,如果您确实需要 return 结果,则处理程序的最后一行(就在 end run
之前)将是:
return theSituation
您可能会四处寻找适合您需要的 AppleScript 教程。 Apple 有他们的 Introduction to AppleScript Language Guide.
IF 语句可以使用 if/else if/else 链接起来 if:
on run
--get the situation we're dealing with
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
copy the result as string to theSituation
--Thunderstorms require that the user be immediately notified
if theSituation is "Thunderstorm" then
display notification "hello"
else if theSituation is "Flood" then
display notification "hike pants"
else if theSituation is "Heatwave" then
display notification "Play Bing Crosby"
else if theSituation is "Hazmat" then
display notification "Use highway 183"
end if
end run
AppleScript 中没有 switch/case 控制结构。
我是 AppleScript 的初学者,所以我真的不是很了解。我一直在尝试让用户 select 从一系列情况中做出选择,然后根据他们的选择做出适当的回应。但是,我 运行 遇到了一些问题:
- 脚本 运行s 但不显示通知。
- 有没有比链接大量 IF 语句更好的方法?
PS: 我离完成整个剧本还差得很远。
on run
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
return the result as string
if string is Thunderstorm then display notification "hello"
end run
您对所查看的示例代码的作用做出了一些很好的猜测;但其中一些猜测是错误的。随着时间的推移,您会更好地了解 AppleScript 的工作原理。
您已在
choose from list
行中将情境名称正确输入为字符串,并用引号将其文本括起来。但是在检查它是否是雷雨时,您不会用引号括起该文本。 AppleScript 正在寻找一个名为Thunderstorm
的变量来获取其内容,而不是寻找文本字符串“Thunderstorm”。return the result as string
不会创建名为string
的变量,而是结束名为run
的处理程序,returningthe result
以任何名为run
的代码,作为文本字符串。要将choose from list
的结果分配给变量,请使用set theSituation to the result as string
.
以下是您的代码如何更好地工作的示例:
on run
--get the situation we're dealing with
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
copy the result as string to theSituation
--Thunderstorms require that the user be immediately notified
if theSituation is "Thunderstorm" then display notification "hello"
end run
由于您将此作为 run
处理程序,您可能不想 return 任何结果,因为您很少调用 运行 处理程序。但是,如果您确实需要 return 结果,则处理程序的最后一行(就在 end run
之前)将是:
return theSituation
您可能会四处寻找适合您需要的 AppleScript 教程。 Apple 有他们的 Introduction to AppleScript Language Guide.
IF 语句可以使用 if/else if/else 链接起来 if:
on run
--get the situation we're dealing with
choose from list {"Thunderstorm", "Flood", "Heatwave", "Hazmat"} with prompt "Please select one of the emergency situations below" without multiple selections allowed and empty selection allowed
copy the result as string to theSituation
--Thunderstorms require that the user be immediately notified
if theSituation is "Thunderstorm" then
display notification "hello"
else if theSituation is "Flood" then
display notification "hike pants"
else if theSituation is "Heatwave" then
display notification "Play Bing Crosby"
else if theSituation is "Hazmat" then
display notification "Use highway 183"
end if
end run
AppleScript 中没有 switch/case 控制结构。