Skip to main content
Known Participant
March 8, 2018
Question

Add together all numbers in a list

  • March 8, 2018
  • 3 replies
  • 1337 views

Hi all,

I'm still learning Applescript and am stuck on something I imagine is relatively simple!

I have a script that cycles through all the graphic frames in my document and records their widths and heights to a list.

--outside of the loop

set fWList to {}

set fHList to {}

--inside the loop

set frameWidths to (d - b)

set end of fWList to frameWidths

set frameHeights to (c - a)

set end of fHList to frameHeights

--at the end of the script, verify values

{fWList, fHList}

Its this last bit I'm stuck on, I don't know how to work with the list given. These will always be numbers and I need to sum all the numbers recorded

if fWList is 500, 500, 500 I need 1500 saved to a variable called totalWidths

if fHList is 300, 500, 500 I need 1300 saved to a variable called totalHeights

I've tried everything and just get errors at the last moment, the errors do show '500,500,500' etc so I know the info is being collected properly, I just can't get it back out!

Thanks

This topic has been closed for replies.

3 replies

Known Participant
March 9, 2018

Thanks for your reply first of all.

I imagine that gets me closer but it stops my script completely and I do not get anything passed into totalwidths.

The problem I have is that I have no code as below when setting the list

set fWList to {500, 400, 300, 200, 100}

it actually is a repeat loop below that gets me my fWList

so I'm not sure it that makes a difference?

set fWList to {}

           

            set fHList to {}

           

            repeat with oneFrame in (get all page items of layerImages)

                set {a, b, c, d} to geometric bounds of oneFrame

                set geometric bounds of oneFrame to {a + theChosenBleed, b + theChosenBleed, c - theChosenBleed, d - theChosenBleed}

                make rectangle with properties {geometric bounds:{a + theChosenBleed, b + theChosenBleed, c - theChosenBleed, d - theChosenBleed}, stroke color:"Cut", stroke weight:2}

               

               

                set frameWidths to (d - b)

               

                set end of fWList to frameWidths

               

                set frameHeights to (c - a)

               

                set end of fHList to frameHeights

               

               

                delete oneFrame

               

               

            end repeat

Inspiring
March 9, 2018

Hi. My use of "set fWList to {500, 400, 300, 200, 100}" was just an example to get a list of numbers to add up. In your case, you already have a list of numbers in fWList, so you don't need that line. When you have your fWList populated with the numbers you want to add up, just use the rest of my lines. The last line, "return totalWidths --> 1500", does stop the script, and you don't' need it. It was just to show that everything was being added up properly.

Known Participant
March 8, 2018

I normally would be, I'm writing scripts in both but this is part of a much larger pre-existing Applescript workflow that I couldn't possibly re-write in Javascript. Thank you anyways.

Inspiring
March 8, 2018

-- Try something like this:

    set fWList to {500, 400, 300, 200, 100}

          set totalWidths to 0

          repeat with aWidth in fWList -- will cycle thru list items and then stop

            

                      set totalWidths to totalWidths + aWidth

                 end repeat

          return totalWidths --> 1500

Known Participant
March 29, 2018

Sorry for the late reply - this is perfect! With this and your other comments I got it working. Thank you very much.

Peter Kahrel
Community Expert
Community Expert
March 8, 2018

You're not interested in JavaScript?