Illustrator CC visible bounds

My ultimate goal is to automate the resizing of the artboard to the dimensions of the visible bounds. But visible bounds is not always accurate. The below script works well, but not if an effect such as a drop shadow is applied to a “group”. In that case it only considers the visible bounds of the vector objects and does not consider the raster effects as part of the visible bounds.

Is there a way using AS to have visible bounds consider the extra dimension caused by an effect such as a drop shadow that has been applied to a group?

If not, is there an easy way for Applescript detect the presence of an effect such as an outer glow or drop shadow so I can return an alert dialog to the end user? This way I can have the script abort the resizing of the artboard in such instances.

tell application "Adobe Illustrator"
	tell document 1
		set SelectedArt to visible bounds
		set artboard rectangle of artboard 1 to SelectedArt
	end tell
end tell

Thanks,
-Jeff

Hi. Live effects don’t actually modify a host object, so any accurate measurement method would involve flattening.

Thank you very much Marc for confirming this.

-Jeff

I’ve had this exact problem, and I had my script duplicate the objects in-place, flatten them, perform the resize based on the flattened versions, then delete the flattened copy. Leaving the original data intact with no flattened copy, but with the documents resized based upon the visual bounds including the effects, once they were flattened.

Live text also can yield some weird visual bounds before flattening.

Thank you so much for responding t.spoon. Your approach will work just fine by grabbing the dimensions of the temporary flattened objects to use as the artboard’s future dimensions.

Could you by any chance share the snippet of code that selects all, duplicates, then flattens? But this is by no means critical if you cannot do so.

Thanks,
-Jeff

I am wondering why an object with a drop shadow applied will rasterize fine using my code below. However, it will treat the entire artboard as the final rasterized section, and not consider the new raster object as just the size of just the artwork it acted upon?

Currently I am stuck because the final raster object becomes the size of the artboard. I am probably overlooking something very obvious to others?

Thanks in advance,
-Jeff

tell application "Adobe Illustrator"
	activate
	tell document 1
		set selection to every page item
		set myArt to selection
		rasterize source art myArt with options {resolution:72, transparency:true}
		set SelectedArt to visible bounds
		set artboard rectangle of artboard 1 to SelectedArt 
	end tell
end tell

Hi,

calling upon the menu action (give it a shortcut or use UI-scripting or use javascript, under ‘object’ way down below) works just fine?

Grtz, Eric

The exact issue was related to resizing the artboard to account for a raster effect such as a drop shadow that isn’t considered in the page items dimensions, so the effect could become cropped by the resized artboard.

I appear to have created a workaround for this.

Thanks,
-Jeff

global itemCount
global toExpandApp

on onWait()
	tell application "Adobe Illustrator"
		tell document 1
			set itemCount to count of page items
			repeat while itemCount is less than 1
				set itemCount to count of page items
			end repeat
		end tell
	end tell
end onWait


tell application "Adobe Illustrator"
	activate
	set the_doc to document 1
	tell the_doc
		set has selected artwork of (every layer) to true
		
		copy
		
			tell application "System Events"
				try
					tell application process "Adobe Illustrator"
						set frontmost to true
						tell menu bar 1
							tell menu bar item "Object"
								tell menu 1
									click menu item "Expand Appearance"
								end tell
							end tell
						end tell
					end tell
				end try
			end tell
		
		--This will select all regardless if items are locked
		set has selected artwork of (every layer) to true
		
		
		--Capture the visible bounds to use later
		set SelectedArt to visible bounds
		
		--Clear the seletion
		tell application "System Events"
			try
				tell application process "Adobe Illustrator"
					set frontmost to true
					tell menu bar 1
						tell menu bar item "Edit"
							tell menu 1
								click menu item "Clear"
							end tell
						end tell
					end tell
				end tell
			end try
		end tell
		
		--Paste in front
		tell application "System Events"
			tell application process "Illustrator CC"
				set frontmost to true
				keystroke "f" using {command down}
			end tell
		end tell
		
		--test for objects back on page
		my onWait()
		if itemCount is greater than or equal to 1 then
			set artboard rectangle of artboard 1 to SelectedArt
			-- decelect all
			tell application "System Events"
				tell application process "Illustrator CC"
					set frontmost to true
					keystroke "a" using {command down, shift down}
				end tell
			end tell
		end if
	end tell
end tell

Sorry, I would be happy to share the code, but it’s from something like 10 years ago, has been unused for at least 7 years, and I just don’t have time to go digging through archives for it now. I don’t even remember the name of the script that would have been doing that. I could probably re-write it faster than I could find it, but don’t have time for that right now either.

IIRC, the biggest PITA of writing it was that Applescript didn’t have commands equivalent to the “Flatten Transparency” dialog box, which had to be worked around.

  • Tom

No problem at all Tom,

I just edited my code once again, which seems to work very well.

Thanks again,
-Jeff

GUI scripting is fragile and most likely will break with an app version or OS update; it should only be used as a last resort. You can record menu functions in Adobe Illustrator with an Action and call them by script. Record a single flatten or expand step (with already selected items) and your overall code burden can be reduced.

tell application "Adobe Illustrator"'s document 1
	set page items's locked to 0
	set page items's selected to 1
	do script "flatten" from "Default Actions" --or whatever you named it
end tell


I have not used the Adobe Illustrator application yet, but I follow this topic out of pure interest. As I understand it, they want the visible bounds property to contain effects. But from the experience of using other similar applications, I can say that each effect is a control. Therefore, logically, these effects should be contained in the property control bounds, and not in visible bounds.

What I want say: to detect the presence of an effect such as an outer glow or drop shadow, AppleScript should check property control bounds of image. If control bounds is not same with visible bounds, then the object contains some effect.

Hi Marc,
I was unaware Illustrator can call actions in such a way, which is very nice!

Do you happen to know if an action can somehow be told to bypass an alert message? For example, let’s assume I create an action to run the menu item, Object > Expand Appearance. This menu item might not always be accessible based on the type of artwork, therefore the action would throw an alert in such instances:

The object “Expand Appearance” is not currently available.

The only way I know to test for the presence of a menu item relies on the use of System Events, which is best to avoid based on your response.

KniazidisR
As for Control Bounds - I need to test this a bit more. In my initial findings it does not appear to yield different dimensions than Visual Bounds for a single object with a drop shadow applied. But it does yield different dimensions for items such a live text.

Thanks,
-Jeff

There’s no way to squelch an error that the Action is throwing, as that’s happening in the application, however, flatten transparency should be available in all contexts, except where the document doesn’t exist; try that instead of expand appearance.

Hi Marc,
Calling the actions from the script and specifically using “flatten transparency” works very well! And my script is so much shorter and cleaner thanks to you.

Thanks again for all of your help and knowledge.

-Jeff

tell application "Adobe Illustrator"'s document 1
	set page items's locked to 0
	set page items's selected to 1
	copy
	do script "FlattenTransparency" from "ActionsForScripts"
	set SelectedArt to visible bounds
	do script "Clear" from "ActionsForScripts"
	do script "PasteInFront" from "ActionsForScripts"
	set artboard rectangle of artboard 1 to SelectedArt
	do script "DeselectAll" from "ActionsForScripts"
end tell