Вы сделали довольно хорошие предположения о том, что делает пример кода, на который вы смотрели; но некоторые из этих догадок были неверны. Со временем вы лучше поймете, как работает AppleScript.
Имена ситуаций правильно введены в виде строк в строке choose from list
, заключив их текст в кавычки. Но в проверке, чтобы увидеть, является ли это гроза, вы не окружаете этот текст кавычками. AppleScript ищет переменную с именем Thunderstorm
для своего содержимого, а не ищет строку текста «Гроза».
return the result as string
не создает переменную с именем string
, но скорее завершает обработчик с именем run
, возвращая 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
, вы, вероятно, не хотите возвращать какой-либо результат, так как вы редко вызываете обработчики запуска. Но если вам нужно вернуть результат, последняя строка обработчика (непосредственно перед end run
) будет выглядеть так:
return theSituation
Вы можете найти учебник по AppleScript, который соответствует вашим потребностям. У Apple есть Введение в руководство по языку AppleScript .
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.