Illustrator center a selection to the center of artboard

I have a script that works very well for centering an Illustrator selection to the center of the artboard. However it only works on the assumption that no other objects besides the selection exists.

Ideally I am looking for a way to center any selected object and have it centered on the artboard, regardless if there is another object, guides, or even another locked layer with objects on it.

I may be able to modify this script but I am not sure about the “translation of the delta vaule”. The dictionary doesn’t get me very far. It’s a concept I would like to learn. Can anybody help me out?

tell application "Illustrator CS"
	activate
	set docRef to the current document
	tell docRef
		set |width| to width
		set |height| to height
		set ruler origin to {(|width| / 2), (|height| / 2)}
		
		set sel to selection
		set {vbL, vbT, vbR, vbB} to visible bounds
		
		
		
		if sel is equal to {} then
			display dialog "you must have an active selection" buttons {"Cancel"} default button "Cancel"
		end if
		
		if (vbL ≥ 0) then
			translate sel delta x -((vbL + vbR) / 2)
		else
			translate sel delta x ((-(vbL) + -(vbR)) / 2)
		end if
		if (vbB ≥ 0) then
			translate sel delta y -((vbT + vbB) / 2)
		else
			translate sel delta y ((-(vbT) + -(vbB)) / 2)
		end if
	end tell
	
end tell

Hi Jeff

Does something like this work for you?

property mm : 2.834645 --gives you millimetres in points
tell application "Adobe Illustrator"
	activate
	set the_doc to the current document
	set properties of the_doc to {ruler origin:{0, 0}}
	set h to height of the_doc
	set w to width of the_doc
	set t to width of selection
	set s to height of selection
	set position of selection to {0 + w / 2 - t / 2, 0 + h / 2 + s / 2}
end tell

i’m not very familiar with the delta translate stuff either sorry.

Pidge, you are one smart man!!!
Works like a charm.

But I do have some questions, if you have the time to answer.

If the script is adding the width & height of doc & selection to “0”, then do I really need to include the 0’s in the equation?

Also, I am assuming the mm is a carry over from some other script you used this for? Or is this some kind of foreshadowing in which I may need this later on?

Once again, you have really pulled through for me!

Many thanks,
Jeff

hi Jeff

When i saw your post i realised i had used something similar myself previously, so the bits like the ‘0’s’ are
left over from the rest of this script, the property is there aswell to remind me what the point to millimetre conversion is, and the 0’s are there for when you don’t want the item to hit the centre and it needs to be somewhere else on the page you can just change there value.

hope that makes sense.

Cheers
pidge

Yes, that makes total sense. And I am glad I asked, because I see the logic and I am sure that added functionality will be useful in the future. I appreciate you getting back to me on this.