Illustrator CS - Align object to center of artboard

Is there a way to script Illustrator to align an object to the center of the artboard”similar to how this can be done using the align palette?

I can use dimensions to set an object. But the problem with doing this is that the artboard from certain existing documents (non-“New” documents) may have different 0,0 x,y, coordinates in relation to the artboard. My goal is to place guides always in the center of the artboard no matter where 0,0 may be in relation to the artboard.

The script below works well when the 0,0, is on the bottom left corner. But not all documents we open are built this way.

Any suggestions?

Thanks,
Jeff

tell application "Illustrator CS"
	set recRef to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{252, 450}, width:{108}, height:{108}, guides:{true}}
	set recRef2 to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{131, 450}, width:{350}, height:{108}, guides:{true}}
	set recRef3 to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{21, 504}, width:{570}, height:{216}, guides:{true}}
	
end tell

Hi Jeffkr

Not sure about aligning like you do via the pallette, would be interesting to look into it.
However this bit at the begining of your script will reset the rulers to bottom left.:

tell application "Illustrator CS"
	activate
	set doc to current document
	set theOrigin to ruler origin of doc as list
	set properties of doc to {ruler origin:{0, 0}}
end tell

pidge1,
You always pull through, it is amazing!

This is simple and better than going the align route. That snippet of code is very valuable, I am sure I will use it again.

Many thanks!

-Jeff

I think this is align center. you will need to test. If I remember rightly though visual bounds does not correctly calculate masks I never got around to working this out. You would have to check for groups with clipping true then run through the rest of the items.

tell application "Adobe Illustrator"
	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 locked of every layer to false
		set {vbL, vbT, vbR, vbB} to visible bounds
		set |selection| to every page item
		if (vbL ≥ 0) then
			translate |selection| delta x -((vbL + vbR) / 2)
		else
			translate |selection| delta x ((-(vbL) + -(vbR)) / 2)
		end if
		if (vbB ≥ 0) then
			translate |selection| delta y -((vbT + vbB) / 2)
		else
			translate |selection| delta y ((-(vbT) + -(vbB)) / 2)
		end if
	end tell
end tell

This is excellent and is definitely on the right track. But is there any way to have it work on an object that is to be introduced to a page that already has objects on it.

For instance, I am trying to set 3 rectangle frames to line up in the center of a document. The guides are to be placed in center without moving any other objects.

My present script is below. I was trying to combine this script with yours but I wasn’t having any success.

Can you help me out?

Thanks,
Jeff

tell application "Illustrator CS"
	activate
	set doc to current document
	set theOrigin to ruler origin of doc as list
	set properties of doc to {ruler origin:{0, 0}}
	
	set recRef to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{252, 450}, width:{108}, height:{108}, guides:{true}}
	set recRef2 to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{131, 450}, width:{350}, height:{108}, guides:{true}}
	set recRef3 to make new rectangle at beginning of document 1 with properties {name:"rec1", position:{21, 504}, width:{570}, height:{216}, guides:{true}}
	
end tell

Jeff,

If I understand this correctly then you need to create 3 rectangles as guides in existing documents that may be of different sizes. In these documents you need to hav these rectangles centered vertically on the page. The 3 rectangles are next to each other and the group of the 3 needs to be centered horizontally. is this correct?

J.

Yes, that appears very close. The 3 rectangles are all centered based on their center points. A small rectangle fits centered within a larger rectangle that fits centered within a larger rectangle. These rectangles (whether grouped or not) are to become guides and hopefully centered within any artboard, no matter what the artboard’s size or 0,0 point may be. - Originally I was hardcoding the rectangles’ location. That is before I realize there may be a way to center within the artboard.

It is difficult to explain why in few words why it is best if I did not have to change the location of the document’s 0,0, origin.

But FWIW, I am very content with the solution provided by pidge1. It works for almost all (99.9%) of the instances I need it for. But sometimes that .01 % can frustrate an operator, so I am open for any other solutions.

Jeff, I think that it would go something like this:

tell application "Adobe Illustrator"
	activate
	set docRef to the current document
	tell docRef
		set |page width| to width
		set |page height| to height
		set ruler origin to {0, 0}
		--
		set |guides group| to make new group item at beginning
		--
		set {|rectangle width|, |rectangle height|} to {108, 108}
		set |rectangle 1| to make new rectangle at beginning of |guides group| with properties ¬
			{name:"rectangle 1", position:{((|page width| - |rectangle width|) / 2), ((|page height| + |rectangle height|) / 2)}, width:{|rectangle width|}, height:{|rectangle height|}, guides:{true}}
		--
		set {|rectangle width|, |rectangle height|} to {350, 108}
		set |rectangle 2| to make new rectangle at beginning of |guides group| with properties ¬
			{name:"rectangle 2", position:{((|page width| - |rectangle width|) / 2), ((|page height| + |rectangle height|) / 2)}, width:{|rectangle width|}, height:{|rectangle height|}, guides:{true}}
		--
		set {|rectangle width|, |rectangle height|} to {570, 216}
		set |rectangle 3| to make new rectangle at beginning of |guides group| with properties ¬
			{name:"rectangle 3", position:{((|page width| - |rectangle width|) / 2), ((|page height| + |rectangle height|) / 2)}, width:{|rectangle width|}, height:{|rectangle height|}, guides:{true}}
	end tell
end tell

Hi jeffkr

this should work as well:

property mm : 2.834645
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 100 * mm
	set s to 200 * mm
	set p to 300 * mm
	set recone to make new rectangle in the_doc with properties {name:"rec one", width:t, height:t, guides:{true}}
	set position of recone to {0 + w / 2 - t / 2, 0 + h / 2 + t / 2}
	set rectwo to make new rectangle in the_doc with properties {name:"rec two", width:s, height:s, guides:{true}}
	set position of rectwo to {0 + w / 2 - s / 2, 0 + h / 2 + s / 2}
	set recthree to make new rectangle in the_doc with properties {name:"rec three", width:p, height:p, guides:{true}}
	set position of rectwo to {0 + w / 2 - p / 2, 0 + h / 2 + p / 2}
end tell

the position can be assigned when the rectangle is being made but i find it easier to read on a separate line also this is working in Millimetres.
looks like you have a fair bit to go at with marks solution as well…

good luck

Both of these solutions are awesome. I am glad that the artboard size can now be different yet the guides will still center.

I am a bit lost as to what exact math is going on in the script that makes this work.

My only concern with Pidge1’s solution is that it is using perfect squares. I don’t know how to make the condensed script work with rectangles of different heights vs. widths?

Regardless, I owe you both a great deal of thanks and praise.

-Jeff