Applescript(列表选择)

时间:2014-05-18 13:37:42

标签: list applescript choice

我需要帮助一个小的Applescript。

当我在列表中做出选择时。 Safari总是打开test3。也许有人可以帮助我?

代码

set choice to choose from list {"1", "2", "3"}

if choice is "1" then
        tell application "Safari" to open location "http://test1/"


else if choice is "2" then
        tell application "Safari" to open location "http://test2/"

else
        tell application "Safari" to open location "http://test3/"
end if

2 个答案:

答案 0 :(得分:1)

“从列表中选择”对话框的结果是一个列表。即使列表中只有1个项目,它仍然是一个列表。所以你真的希望列表中的“第1项”能够得到实际结果。

要修复您的代码,请将第一行更改为此...

set choice to item 1 of (choose from list {"1", "2", "3"})

答案 1 :(得分:0)

在Applescript编辑器中运行代码并查看“事件/回复”区域时,

enter image description here

您会看到您的选择以大括号形式返回 - {"1"}。它们表示列表已退回给您。如果您想使用with multiple selections allowed语句从列表中选择多个项目,这将非常有用。

因此,您的if then语句失败了,因为您要将列表{"1"}与字符串"1"进行比较。由于所有其他方法都失败了,test3是唯一的选择。

要修复它,您可以从返回的列表中选择一个项目,例如

set choice to item 1 of (choose from list {"1", "2", "3"})

或将选择转换为文本,以便将苹果与苹果进行比较:)

set choice to (choose from list {"1", "2", "3"}) as text

希望这会有所帮助。

相关问题