Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add together all numbers in a list

Community Beginner ,
Mar 08, 2018 Mar 08, 2018

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

TOPICS
Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2018 Mar 08, 2018

You're not interested in JavaScript?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 08, 2018 Mar 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 29, 2018 Mar 29, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Mar 14, 2018 Mar 14, 2018

lucas.maximus  a écrit

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.

You can still write Javascript code and call it from the AppleScript. I can only encourage you to do so.

do script v : Executes the script in the specified language as a single transaction.

do script any : The script to execute.

[language unknown/‌javascript/‌applescript language] : The language of the script to execute. If not specified, uses the language used to call this method. Can accept: unknown/javascript/applescript language (Default: unknown).

[with arguments any] : An array of arguments passed to the script.

[undo mode script request/‌entire script/‌auto undo/‌fast entire script] : How to undo this script. Can accept: script request/entire script/auto undo/fast entire script (Default: script request).

[undo name text] : The name of the undo step for entire script undo mode. Can accept: string (Default: "Script").

→ any : The return value of the scrip

tell application "Adobe InDesign CC 2018"

  set theScript to "alert('hello from js')"

  do script theScript language javascript

end tell

Here I use a string script but you could pass a file reference. See Scripting Guides for more details…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 29, 2018 Mar 29, 2018
LATEST

This is brilliant thanks, I never knew that could be done. I haven't used it on this particular project yet, but its opened many doors in future. Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 09, 2018 Mar 09, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 09, 2018 Mar 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2018 Mar 09, 2018

Hi Lucas,

what's in variable theChosenBleed ?
A number?

A string?

Something else?

Regards,
Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 09, 2018 Mar 09, 2018

Hi,

Below is your original script with a handler added to total the items in each of the lists. Notice that variables passed do not need to be the same as the variable that receives its value; if more than one variable is passed they just need to be in the same position.

set theChosenBleed to 12

set fWList to {}

set fHList to {}

tell application "Adobe InDesign CC 2018"

  set measurement unit of script preferences to points

  tell document 1

  set theSwatch to swatch "Cut"

  set layerImages to layer "Images"

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

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

  set FrameWidths to (d - b)

  set end of fWList to FrameWidths

  set FrameHeights to (c - a)

  set end of fHList to FrameHeights

  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 properties of oneFrame to {geometric bounds:{a + theChosenBleed, b + theChosenBleed, c - theChosenBleed, d - theChosenBleed}, stroke color:"Cut", stroke weight:2}

  end repeat

  end tell

end tell

set totalWid to 0

set totalHgt to 0

set fwTotal to getTotal(fWList)

set fhTotal to getTotal(fHList)

{fhTotal, fwTotal}

(*Handler totals items in list and returns total*)

on getTotal(theList)

  set listTotal to 0

  repeat with i from 1 to length of theList

  set listTotal to listTotal + (item i of theList)

  end repeat

  return listTotal

end getTotal

ps: you might want to follow my blog, it is written for those who are new to scripting Adobe InDesign: yourscriptdoctor.com/blogs

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 09, 2018 Mar 09, 2018

Oops: Forgot to mention: With this example script, instead of creating a new rectangle and deleting the original, you can simply just change the properties of the original rectangle. (Notice I have commented out the statement that creates the rectangle and replaced it with one that changes the properties.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines