0

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 & "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 :-)

3 Answers 3

2

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 and repeat 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> (leaving by <step> out defaults to by 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, with exit repeat)
Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I was looking for Antal. Works perfectly. And thanks for the detailed and very useful looping summary :-)
2

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.

2 Comments

Thanks Philip. I learned from an earlier posting that Scripting Bridge seems the only practical way around such limitations. It's on my future 'to-do' study list. Appreciate your input.
@Bender: Fair warning about Scripting Bridge: If you don't understand Applescript yet, Scripting Bridge is going to present a real challenge. The two are radically different from each other (which could play in your favor, actually since you don't have the burden of heavy Applescript experience to hold you back). Also in some applications, like Adobe Photoshop, there things you can't do in SB that you can only do in AS. IMHO, SB is not a very well thought-out framework.
1

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

1 Comment

Thanks for the alternative solution which I'll also look further into, Sakra. Appreciate your input :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.