你知道你可以在一个对话框而不是 4 个对话框中做到这一点......
set theValues to text returned of (display dialog "Enter X1 Y1 X2 Y2 separated by a space." default answer "")
set {tids, text item delimiters} to {text item delimiters, space}
set {x1, y1, x2, y2} to text items of theValues
set text item delimiters to tids
display dialog "Distance = " & ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ (1 / 2)
因此,您只需一次输入所有值,每个值之间有一个空格。然后在代码中你把它分成你的变量。如果您的价值观始终是积极的,那么您只需获取 theValues 的“单词”即可使其更加简单。但如果您还想使用负值,我会坚持使用文本项分隔符。使用“words”去除数字中的“-”符号。
如果你想变得非常花哨,你可以让用户像这样将每个值放在单独的行上......
set theValues to text returned of (display dialog "Enter X1 Y1 X2 Y2 on separate lines." default answer (return & return & return))
set {x1, y1, x2, y2} to paragraphs of theValues
display dialog "Distance = " & ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ (1 / 2)
解释文本项目分隔符:
您可以通过获取字符串的“文本项”将字符串转换为列表。有一个名为“文本项分隔符”(tids)的值,它决定了如何将该字符串分解为一个列表。默认情况下,tids 是“”(例如,什么都没有)。所以例如看看这个脚本......
set theString to "some text words"
set theList to text items of theString
--> {"s", "o", "m", "e", " ", "t", "e", "x", "t", " ", "w", "o", "r", "d", "s"}
你得到的列表是字符串的每个字符作为一个单独的项目。那是因为 tids 是 "" 的默认值。现在让我们看看如果我们将 tids 更改为其他东西会发生什么。让我们将 tids 改为一个空格字符,然后再次运行脚本...
set theString to "some text words"
set text item delimiters to space
set theList to text items of theString
--> {"some", "text", "words"}
通过将其设置为空格,字符串被分解为它们之间有空格的项目。所以你看我们可以通过控制 tids 来控制字符串如何变成一个列表。需要注意的一件事:当我们将 tids 更改为默认值以外的其他值时,在使用它之后,我们必须将 tids 改回来。这是安全的编程,因为脚本的其他部分可能取决于 tids 的值。因此,请养成完成后重置 tids 的习惯。这就是 tids 代码的基本功能。它存储 tids 的初始值(以便我们以后可以将其改回),将 tids 更改为空格,使用 tids 将字符串转换为列表,然后将 tids 重置为其初始值。
希望对你有帮助。