Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bounds of selection in AppleScript

348 views
Skip to first unread message

Joseph Briggs

unread,
Feb 5, 2003, 8:29:53 PM2/5/03
to
Just starting to figure out scripting in AI and I'm going thru the dictionary and writing a bunch of utility handlers. One thing that is bugging me is that I can't find a way to easily get the bounds of the selection. My quick and dirty workaround is to use System Events to group the selection, I get the bounds of that, then ungroup it. It's not that bad and the delay might be lessened with the final release (of UI scripting), but I'm hoping for a more efficient solution.

Another idea I have is sorting the list of items to find the leftmost, topmost, rightmost, and bottommost items. It should be quick and I need to write that kind of sorter anyway but I was hoping to monkey around some more before I did that. It appears to be the best idea I've got though.

The only other thing I can think of messes with the UI again by setting everything that isn't selected invisible and getting the bounds of the doc. Presumably, as long as I don't call redraw, the user should be none the wiser. This might be easier than the second and it's better than the first, but I still feel there has to be something better.

It seems such a simple problem for these somewhat convoluted solutions. Am I just overlooking something?

anyHelp = manyThanks

Jason Howard

unread,
Feb 7, 2003, 12:19:26 PM2/7/03
to
"It seems such a simple problem for these somewhat convoluted solutions."

I have had that same thought about this same problem. I am still fairly new to Applescript, but it seems to me that there should be an easy solution to return the bounds of a selection. If you find it let me know.

What I ended up doing sounds like the direction you're heading. Make a list of the bounds of each item in the selection, then sort through it to get the outermost bounds. It works well, but it just seems far more complicated than it should be.

Good luck
Jason

Joseph Briggs

unread,
Feb 7, 2003, 9:17:47 PM2/7/03
to
Good to hear from you, Jason. It's nice to know there's at least one sympathizer. I was watching with dismay as my poor post travelled down the list with no response.

Since they now have AI9 and AI10 script support, I'm hoping Adobe will soon start an Illustrator Scripting forum.

Joseph Briggs

unread,
Feb 9, 2003, 11:47:46 AM2/9/03
to
Well, I finally got around to writing the handler and it was easy enough, still...


boundsOfSelection(0) -- demos handler

on boundsOfSelection(boundsType)
-- boundsType: 0 = geometric, 1 = visual, 2 = control
try
tell front document of application "Adobe Illustrator 10" to return my boundsOfItems(selection, boundsType)
end try
end boundsOfSelection

on boundsOfItems(itemList, boundsType)
-- boundsType: 0 = geometric, 1 = visual, 2 = control
-- takes a list of page item references
-- and returns outermost bounds of all items
set {x1f, y1f, x2f, y2f} to {0, 0, 0, 0}
set {x1List, y1List, x2List, y2List} to {{}, {}, {}, {}}
tell application "Adobe Illustrator 10"
repeat with thisItem in itemList
if boundsType = 0 then
copy geometric bounds of thisItem to {x1, y1, x2, y2}
else if boundsType = 1 then
copy visible bounds of thisItem to {x1, y1, x2, y2}
else if boundsType = 2 then
copy control bounds of thisItem to {x1, y1, x2, y2}
end if
copy x1 to end of x1List
copy y1 to end of y1List
copy x2 to end of x2List
copy y2 to end of y2List
end repeat
end tell
set x1f to item (lowNumIndex(x1List)) of x1List
set y1f to item (highNumIndex(y1List)) of y1List
set x2f to item (highNumIndex(x2List)) of x2List
set y2f to item (lowNumIndex(y2List)) of y2List
return {x1f, y1f, x2f, y2f}
end boundsOfItems

on highNumIndex(thisList)
-- takes a list of numbers, {18, 5, 270, -500}
-- and returns the index of the high number
set highIndex to 0
set compareNum to -1.0E+16
repeat with i from 1 to count of thisList
set thisNum to item i of thisList
if thisNum > compareNum then
set compareNum to thisNum
set highIndex to i
end if
end repeat
return highIndex
end highNumIndex

on lowNumIndex(thisList)
-- REQUIRES highNumIndex()
-- takes a list of numbers, {18, 5, 270, -500}
-- and returns the index of the low number
set lowIndex to 0
set compareNum to item (highNumIndex(thisList)) of thisList
repeat with i from 1 to count of thisList
set thisNum to item i of thisList
if thisNum < compareNum then
set compareNum to thisNum
set lowIndex to i
end if
end repeat
return lowIndex
end lowNumIndex


Jason Howard

unread,
Feb 10, 2003, 2:45:40 PM2/10/03
to
I am out here. I was glad to see someone else had run into this problem. Below is what I used. I was still pretty new when I wrote this and had some help from the Macscripter.net boards, but it works. An Illustrator Scripting forum would be a good idea. When I began to learn Applescript it was for use with Illustrator and I found that Illustrator specific help was hard to come by.

set artToBounds to selection

-- Create a list of the bounds of each item of the selection
set boundsList to {}
set bl to 1
set total to count of items in artToBounds
repeat until bl > total
set vb to visible bounds of item bl of artToBounds
set boundsList to boundsList & {vb}
set bl to bl + 1
end repeat

--Subroutine to take the list of bounds and return only the outermost bounds
set outerBounds to my findOuterBounds(boundsList)

on findOuterBounds(xList)
set {leftRes, topRes, rightRes, bottomRes} to xList's first item
repeat with boundsRef in rest of xList
set {leftVal, topVal, rightVal, bottomVal} to boundsRef's contents
if (leftVal is less than leftRes) then set leftRes to leftVal
if (topVal is greater than topRes) then set topRes to topVal
if (rightVal is greater than rightRes) then set rightRes to rightVal
if (bottomVal is less than bottomRes) then set bottomRes to bottomVal
end repeat
return {leftRes, topRes, rightRes, bottomRes}
end findOuterBounds

tom.i...@gmail.com

unread,
Mar 24, 2015, 1:56:42 PM3/24/15
to
Good subroutine.

Note, you can eliminate the repeat loop to build the list of bounds, Illustrator can return them all in one go.


tell application "Adobe Illustrator"
tell the current document
set boundsList to the visible bounds of every page item
set outerBounds to my find_outer_bounds(boundsList)
set the artboard rectangle of first artboard to outerBounds
end tell
end tell



on find_outer_bounds(boundsList)
set {leftRes, topRes, rightRes, bottomRes} to boundsList's first item
repeat with boundsRef in rest of boundsList
set {leftVal, topVal, rightVal, bottomVal} to boundsRef's contents
if (leftVal is less than leftRes) then set leftRes to leftVal
if (topVal is greater than topRes) then set topRes to topVal
if (rightVal is greater than rightRes) then set rightRes to rightVal
if (bottomVal is less than bottomRes) then set bottomRes to bottomVal
end repeat
return {leftRes, topRes, rightRes, bottomRes}
end find_outer_bounds
0 new messages