除对话框外的 AppleScript GUI 元素

发布于 2024-09-16 21:54:20 字数 791 浏览 6 评论 0原文

我想知道 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 应用程序会更好吗?

I was wondering if there is a way in AppleScript to create a small Applet of sorts, without having to use a bunch of dialogs, and I think it would be nicer in the end.

Here is the script I have currently, courtesy of 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)

Now, obviously, I understand this script, as it is quite simple!

But I was wondering, if a small Applet such as this were to be a little more useful and practical, it would be better to have some sort of window, without barraging the user with a bunch of dialogs.

Is there a way I can construct a window of sorts, with input fields and a "Calculate" button or something like that.

Or, at this point, would it just be better to construct a super simple Objective-C application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-09-23 21:54:20

Applescript 只提供与用户的非常基本的交互,超出了您在那里显示的内容。 Xcode 支持创建带有 GUI 前端和 Applescript 后端的简单应用程序。 新建项目>Mac OS X>应用程序>Applescript 应用程序。文档是有限的,在这一点上,如果您有 Cocoa 能力,我建议您只走这条路,以获得更轻松的长期支持。

Applescript offers only very basic interaction with the user natively beyond what you gave displayed there. Xcode includes support to create simple applications with a GUI front and an Applescript back. New Project>Mac OS X>Application>Applescript Application. Documentation is limited, and at this point, if you have the Cocoa chops, I would suggest just going that route for easier long-term support.

半岛未凉 2024-09-23 21:54:20

您知道您可以在一个对话框而不是 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)

因此您只需一次输入所有值,每个值之间留一个空格。然后在代码中将其分离到变量中。如果您的价值观始终是积极的,那么您只需获取价值观的“单词”即可使其变得更加简单。但如果您还想使用负值,我会坚持使用文本项分隔符。使用“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是“”的默认值。现在让我们看看如果我们将 tid 更改为其他内容会发生什么。让我们将 tid 设为空格字符,然后再次运行脚本...

set theString to "some text words"
set text item delimiters to space
set theList to text items of theString
--> {"some", "text", "words"}

通过将其设为空格,字符串将被分解为之间有空格的项目。所以你看我们可以通过控制tids来控制字符串如何变成列表。需要注意的一件事是:当我们将 tid 更改为默认值以外的值时,使用后我们必须将 tid 改回来。这是安全的编程,因为脚本的某些其他部分可能取决于 tid 的值。因此,请养成在完成后重置潮汐的习惯。这就是 tid 代码的基本功能。它存储tids的初始值(以便我们稍后可以将其更改回来),将tids更改为空格,使用tids将字符串转换为列表,然后将tids重置回其初始值。

我希望这有帮助。

You know you can do that in one dialog instead of 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)

So you just enter all the values at once with a space between each value. Then in code you separate it out into your variables. If your values will always be positive then you can just get the "words" of theValues to make it even more simple. But I would stick with using text item delimiters in case you want to also use negative values. Using "words" strips off the "-" symbol from the numbers.

If you wanted to get really fancy you could have the user put each value on a separate line like this...

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)

TO EXPLAIN TEXT ITEM DELIMITERS:
You can convert a string into a list by getting the "text items" of the string. There is a value called "text item delimiters" (tids) which determines how that string is broken up into a list. By default tids is "" (eg. nothing). So for example look at this script...

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"}

The list you get is each character of the string as a separate item. That's because tids is the default value of "". Now let's look what happens if we change tids to something else. Let's make tids a space character instead and run the script again...

set theString to "some text words"
set text item delimiters to space
set theList to text items of theString
--> {"some", "text", "words"}

By making it a space the string gets broken up into the items that had a space between them. So you see we can control how the string is turned into a list by controlling tids. One thing to note: when we change tids to something other than the default value, after using it we have to change tids back. This is safe programming because some other part of the script may depend on the value of tids. So get in the habit of resetting tids after you're done. That's the basics of what the tids code does. It stores the initial value of tids (so we can change it back later), changes tids to a space, uses tids to turn the string into a list, then resets tids back to its initial value.

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文