I wrote this sub routine to remove object out side of a given area but it does not consistently work. I ended up repeating 3 times with trying it seems to have problems finding the object it just found
here it ist
on RemoveStrayObjects(boundingBox)
tell application "Adobe Illustrator"
tell document 1
set theItems to page items
set {LeftX, TopY, RightX, BottomY} to geometric bounds of page item boundingBox
repeat 3 times
repeat with anItem in theItems
try
set {iLEftX, iTopY, iRightX, iBottomY} to geometric bounds of anItem
if iBottomY > TopY or iTopY < BottomY or iLEftX > RightX or iRightX < LeftX then
delete anItem
end if
end try
end repeat
end repeat
end tell
end tell
end RemoveStrayObjects
This is one off the first scripting projects i started to work on when i first started scripting.
Trying to do something very similar to what your trying to acheive.
I got a lot of help from Pretech on this!!
It never really got finished,cause the customer whose files where covered in junk managed to start deleting it before sending it to us. so there was no need.
Have a look at it and feel free to use and abuse it or not!!
set itemList to {}
set thePos to {}
set docBounds to {}
set theOrigin to {}
set minX to 0
set miny to 0
tell application "Adobe Illustrator"
activate
set thisDoc to current document
set maxX to width of thisDoc
set maxY to height of thisDoc
set theOrigin to ruler origin of thisDoc as list
set properties of thisDoc to {ruler origin:{0, 0}}
set itemCount to count every page item of thisDoc
repeat with i from 1 to itemCount
set selection to page item i of thisDoc
set thePos to geometric bounds of the selection
if item 1 of thePos is less than minX and item 3 of thePos is not greater than minX then
set properties of selection to {name:"item" & i}
if name of selection is not in itemList then
set itemList to itemList & name of selection
end if
else if item 2 of thePos is less than 0 and item 4 of thePos is not greater than 0 then
set properties of selection to {name:"item" & i}
if name of selection is not in itemList then
set itemList to itemList & name of selection
end if
else if item 1 of thePos is greater than maxX and item 3 of thePos is not less than maxX then
set properties of selection to {name:"item" & i}
if name of selection is not in itemList then
set itemList to itemList & name of selection
end if
else if item 2 of thePos is greater than maxY and item 4 of thePos is not less than maxY then
set properties of selection to {name:"item" & i}
if name of selection is not in itemList then
set itemList to itemList & name of selection
end if
end if
end repeat
repeat with anItem in itemList
set selection to page item anItem of thisDoc
delete selection
end repeat
set properties of thisDoc to {ruler origin:{(item 1 of theOrigin), (item 2 of theOrigin)}}
end tell
might give you some ideas.
Re Looking at it now with a bit more knowledge i’m sure there is a lot better way of doing it! but theres some good stuff in there…
Hope it helps
ok so I fixed it… here i the final code I’m not sure how to submit the to the code exchange but I think it should go there!
what the problem was is that when I would delete the first item illustrator would then renumber them or at least thats what I think was happening so by going from the bottom up there was no need to renumber ?
on RemoveStrayObjects(boundingBox)
tell application "Adobe Illustrator"
tell document 1
set theItems to page items
set {LeftX, TopY, RightX, BottomY} to geometric bounds of page item boundingBox
set C to count of items in theItems
repeat with i from C to 1 by -1
set {iLEftX, iTopY, iRightX, iBottomY} to geometric bounds of item i of theItems
if iBottomY > TopY or iTopY < BottomY or iLEftX > RightX or iRightX < LeftX then
delete item i of theItems
end if
end repeat
end tell
end tell
end RemoveStrayObjects
Model: MacBook Pro Lapzilla 2.16Ghz Core Duowha
AppleScript: 2.11
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)