Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You're not interested in JavaScript?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
-- 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
Copy link to clipboard
Copied
Sorry for the late reply - this is perfect! With this and your other comments I got it working. Thank you very much.
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Lucas,
what's in variable theChosenBleed ?
A number?
A string?
Something else?
Regards,
Uwe
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now