AppleScript display dialog and number arrays
A fairly basic problem here, but almost no AppleScript experience. I've got an array of numbers that I'd like to list in a display dialog so the final output looks like this:
Frame Count: 29
Frame Offsets:
12.684
15.909
28.841
46.332
etc.
Unfortunately I can't find any examples of looping through a list of numbers like this without converting them all to a string first, so at the moment I'm calling each one individually like so:
display dialog "Frame Count: " & FrameCount & return & return 开发者_StackOverflow& "Frame Offsets: " & return & ((item 1 of DataList) / 1000) & return & ((item 2 of DataList) / 1000) & return & ((item 3 of DataList) / 1000)
which is pretty un-elegant (and challenging unless the frame count is known beforehand). Is there any way to do this without having to convert these numbers to a string? Any help greatly appreciated :-)
You do have to convert the numbers to a string, but you can use a loop instead. The following code works, for instanace:
set FrameCount to 29
set DataList to {12684, 15909, 28841, 46332}
set OffsetString to "Frame Offsets:" & return
repeat with off in DataList
set OffsetString to OffsetString & (off / 1000) & return
end repeat
display dialog "Frame Count: " & FrameCount & return & return & OffsetString
Here, you loop over DataList
with each frame offset, and just keep appending to OffsetString
. Note that I didn't do any extra string formatting; that'll be hard in AppleScript, as far as I know. (For instance, 12680
would become "12.68"
instead of "12.680"
, which may or may not be what you want.) It's not gorgeous, but it's about as elegant as you'll get with AppleScript.
The repeat
loop has multiple capabilities; it's AppleScript's only looping construct, but it supports while loops, for loops, foreach loops, and a few other things.
repeat while <cond> ... end repeat
andrepeat until <cond> ... end repeat
repeatedly run their body until<cond>
is true or false, respectively.repeat with <var> from <start> to <end> [by <step>] ... end repeat
repeatedly runs the body by incrementing<var>
by<step>
until it's greater than<end>
(leavingby <step>
out defaults toby 1
).repeat <n> times ... end repeat
runs the body<n>
times.repeat with <var> in <list> ... end repeat
repeatedly runs the body with<var>
equal to each successive element of<list>
repeat ... end repeat
is an infinite loop (you can leave it, or any of these, withexit repeat
)
There is no way around this if you have a specific format in mind and the OS defaults aren't good enough. You have to convert your list into a string in the specific format you need and them display in the dialog.
You could use the AppleScript standard commmand choose from list instead of display dialog
. This command natively handles a list of numbers:
set theList to {12.684, 15.909, 28.841, 46.332}
choose from list theList with title "Frame Count: " & (length of theList) with prompt "Frame Offsets:"
精彩评论