Applescript 错误 "can't get end of"

Applescript Error "can't get end of"

我正在尝试使用 Applescript,看不出有什么问题。 我得到的错误是

错误 "Can’t get end of {button returned:\"确定\",返回文本:\"3\"}。"从{按钮返回的最后一个插入点开始的数字 -1728:"OK",返回的文本:“3”}

这是我的代码:

beep
set counter to 0
set tempX to 0
set temp to 0
set counting to 0
set stored to {0}
set input to "How many grades do you wish to enter?" as string
set str to display dialog input buttons {"NEXT"} default button "NEXT" default answer ""
repeat text returned of str times
    counting = counting + 1
    set grades to display dialog "GRADES:  " default answer ""
    set stored to grades
end repeat
set rep to the length of stored
repeat rep times
    counter = counter + 1
    set tempX to the ((end of stored) - counter) as number
    set temp to temp + tempX
end repeat
set ln to the length of grades
set average to temp / ln
if text returned of str is 1 then
    say "The Average of your grade is " & average using "Zarvox"
else
    say "The Average of your grades is " & average using "Zarvox"
end if
get "AVERAGE:  " & average

因此,在我开始之前:我强烈建议您自学如何使用 Apple Events 的 Javascript 界面,而不是 Applescript 语言本身。 Applescript 是一种 非常奇怪的 语言,它的怪癖在很大程度上是独一无二的;学习它会令人沮丧,并且不会帮助您学习其他语言。

话虽如此,让我们深入研究您的代码:

set stored to {0}

这将使您从一个始终存在并设置为零的成绩开始。您可能只想将其初始化为一个空列表:

set stored to {}

下一个:

set grades to display dialog "GRADES:  " default answer ""

这会将 grades 设置为 结果对象 ,而不仅仅是答案。您可能在这里想要的实际上是结果的 text returned

set grades to text returned of (display dialog "GRADES:  " default answer "")

(这就是在您的错误消息中创建看起来非常奇怪的对象的原因。)


接下来,用这个结果对象覆盖 stored

set stored to grades

您在这里可能想要的是将此元素插入到列表中。因为 Applescript 是一种奇怪且令人讨厌的语言,所以这比您想象的要麻烦一些:

set stored to stored & {grades}

最后,您的平均值存在一些逻辑问题;您每次都将 end of stored(即最后成绩输入)添加到 temp 变量。一个更简单的方法是:

set temp to 0
repeat with n in stored
    set temp to temp + n
end repeat
set average to sum / (count of stored)

完成所有这些更改后,您的脚本应该可以正常工作。