Applescript 使用 if then 语句并从列表中选择

Applescript using if then statements and choose from list

我正在学习一门课程 selection 脚本,每个人应该只能 select 三门课程。如果用户 select 少于或多于三门课程,我无法弄清楚如何从列表中重复选择,并且只有在三个选择被 selected 时才继续。将选项从列表转换为字符串应该可行,但是从列表

中选择后没有任何反应
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}

repeat

set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed

set theClassString to theClass as string

if words in theClassString ≠ 3 then

    display dialog "Please select three courses"
    exit repeat

else if words in theClassString = 3 then
    display dialog "Ok"

end if
end repeat

计算 theClassString 中的字数是一个聪明的主意(这将使用 number of words in theClassString 而不是简单地 words in theClassString 来完成)。这在大多数情况下都是有效的,直到用户将 "I&S" 作为他们的选项之一,遗憾的是这算作两个词,"I""S",因为&符号不是一个词字符.

您的 exit repeat 也位于 if...then...else 块的错误一半,因为您想在用户 selects 3 门课程时打破循环,而不是在他们失败时select 3 门课程。

与其尝试将列表 selection 的结果强制转换为字符串,您应该只计算结果中的项目数,这可以通过以下三种方式之一完成:

  1. count theClass
  2. length of theClass
  3. number in theClass

这是您脚本的修改版本:

property theClassList : {"Math ", "English ", "Science ", "I&S ", "Design "}
property text item delimiters : linefeed

set choices to missing value

repeat
    if choices ≠ missing value then display dialog ¬
        "You must select precisely three courses"

    set choices to choose from list theClassList with prompt ¬
        "Select three courses" with multiple selections allowed

    if choices = false or the number of choices = 3 then exit repeat
end repeat

if choices = false then return
display dialog choices as text

...这是一个使用递归处理程序而不是重复循环的版本:

property theClassList : {"Math", "English", "Science", "I&S", "Design"}
property text item delimiters : linefeed

to choose()
    tell (choose from list theClassList ¬
        with prompt ("Select three courses") ¬
        with multiple selections allowed) to ¬
        if it = false or its length = 3 then ¬
            return it

    display dialog "You must select precisely three courses"
    choose()
end choose

display alert (choose() as text)

通过尊重脚本的流畅性使其更容易。最好将 "theClass" 声明为一个相当字符串的列表,并将 n 声明为列表的计数。以下是您修改后的脚本或以下采用您自己的脚本但声明 n 为 "theClassString".`

中的字数
set theClassList to {"Math", "English", "Science", "I & S", "Design"}

repeat

set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed

set theClassString to theClass as list

set n to count of theClassString

set n to do shell script "echo" & n

if n ≠ "3" then

display dialog "Please select three courses"
exit repeat

else if n = "3" then
display dialog "Ok"

end if
end repeat

以下 通过声明 String

set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}

repeat

    set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed

    set theClassString to theClass as string

    set n to count of words in theClassString

    set n to do shell script "echo " & n

    if n ≠ "3" then

        display dialog "Please select three courses"
        exit repeat

    else if n = "3" then
        display dialog "Ok"

    end if
end repeat

嗯可能是这样

set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}

repeat
    
    set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
    
    set theClassString to theClass as string
    
    set n to count of words in theClassString
    
    set n to do shell script "echo " & n
    
    if n ≠ "3" then
        
        display dialog "Please select three courses" buttons {"ok"}
        
        
    else if n = "3" then
        display dialog "Ok"
        exit repeat
    end if
end repeat