Applescript错误"无法结束"

时间:2015-02-21 02:38:48

标签: macos applescript average

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

错误"无法结束{按钮返回:\"确定\",文本已返回:\" 3 \"}。"编号-1728从{按钮返回的最后插入点开始:"确定",文本返回:" 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

1 个答案:

答案 0 :(得分:1)

所以,在我开始之前:我强烈建议您自学如何使用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)

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