【问题标题】:AppleScript GUI elements besides dialog对话框之外的 AppleScript GUI 元素
【发布时间】:2011-04-05 19:48:19
【问题描述】:

我想知道 AppleScript 中是否有一种方法可以创建一个小 Applet,而不必使用一堆对话框,我认为最终会更好。

这是我目前拥有的脚本,感谢 AppleScript Wikia

set x1 to text returned of (display dialog "What is X1?" default answer "")
set y1 to text returned of (display dialog "What is Y1?" default answer "")
set x2 to text returned of (display dialog "What is X2?" default answer "")
set y2 to text returned of (display dialog "What is Y2?" default answer "")
display dialog "Distance = " & ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ (1 / 2)

现在,很明显,我理解了这个脚本,因为它很简单!

但我想知道,如果像这样的小 Applet 更有用更实用,最好有某种窗口,而不用一堆对话框来骚扰用户。

有没有一种方法可以构建一个带有输入字段和“计算”按钮或类似的东西的窗口。

或者,在这一点上,构建一个超级简单的 Objective-C 应用程序会更好吗?

【问题讨论】:

    标签: dialog applescript


    【解决方案1】:

    Applescript 仅提供与用户在本机上的非常基本的交互,超出了您在此处显示的内容。 Xcode 支持创建带有 GUI 前端和 Applescript 后端的简单应用程序。 New Project>Mac OS X>Application>Applescript Application。文档是有限的,在这一点上,如果你有 Cocoa Choa,我建议你走这条路以获得更容易的长期支持。

    【讨论】:

    • 好的,感谢您的帮助! AppleScript 似乎主要用于自动化小型重复性任务,而不是创建一个小型应用程序或小程序。
    • 一个人可以创建非常复杂的脚本(如数千行和数十个子程序),只是如果你想要一个 GUI,你需要去其他地方。
    • 没错。我只是在试验,再次感谢您的意见。
    【解决方案2】:

    你知道你可以在一个对话框而不是 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 重置为其初始值。

    希望对你有帮助。

    【讨论】:

    • 嘿,谢谢你的替代品!这些确实有助于我的开始代码!但是,我在理解您给出的第一个替代方案时遇到了一些麻烦。我无法理解第 2、3 和 4 行。我相信这些与text item delimiters 和此替代方案提供的间距有关,但如果您能帮助我进一步理解这一点,我将非常感激。
    • 我在答案中添加了对文本项分隔符的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    相关资源
    最近更新 更多