AppleScript GUI elements besides dialog
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 开发者_JAVA百科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?
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.
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.
精彩评论