Illustrator Reflect Script

Please can anyone tell me how I can emulate the reflect tool in Illustrator CS2 using Applescipt. All I need to do is mirror image a group item about 90°.

Thanks for any help.

Tim Baldwin

Hi Tim

Gotta say this is not the best way to do this i’m sure!
However it works!!

tell application "System Events"
	tell process "Illustrator CS"
		set frontmost to true
		keystroke "o"
		delay 0.2
		keystroke return
		delay 0.2
		keystroke return
	end tell
end tell

Can’t see why it wouldn’t work in CS2 (can’t test it on this machine sorry!)
Will if i get chance have a look at how this might be done without using the UI.
You will need to make sure that what you want reflecting is selected for this script to work.

Tim,

Aside from pidge1’s solution, I don’t see any direct “mirror”,“reflect” command in the scripting guide. However, it may be possible to do this using the transformation matrix commands. I haven’t done much with this and will have to experiment some. Meanwhile, I know there are several others on this forum who may already know how this can be done.

PreTech

Hi Pidge1

Thanks for that.

Your scrupt does work if I activate Illustrator first (the frontmost line does not work)

Being a UI solution I’m going to have to work out how the process click the vertical button in the dialog.

If anyone has another solution PLEASE let me know.

Thanks again

Tim Baldwin

A less portable solution is to record 2 actions for V & H reflects then with your script set selection and do script. As for matrix not played much but can’t see any axis options to work with also a bug with stroke widths default. Actions from script work just fine fo me until i’ve learn’t some more.

tell application "Adobe Illustrator"
	set docRef to the current document
	tell docRef
		set selection to group item 3 of layer 1
		do script "Reflect Horizontal" from "Marks" without dialogs
		do script "Reflect Vertical" from "Marks" without dialogs
		set selection to {}
	end tell
end tell

Why is it when you open your big mouth you think afterwards and are proved wrong. Strokes have been screwing up on me when playing with this before and now it just works. I will need to investigate further here is a reflect V & H that works fine now for me in basic file tests.

tell application "Adobe Illustrator"
	activate
	set docRef to the current document
	tell docRef
		set RV_Matrix to ¬
			{class:matrix, mvalue_a:-1.0, mvalue_b:0.0, mvalue_c:0.0, mvalue_d:1.0, mvalue_tx:0.0, mvalue_ty:0.0}
		transform group item 1 of layer 1 using RV_Matrix
		--
		set RH_Matrix to ¬
			{class:matrix, mvalue_a:1.0, mvalue_b:0.0, mvalue_c:0.0, mvalue_d:-1.0, mvalue_tx:0.0, mvalue_ty:0.0}
		transform group item 1 of layer 1 using RH_Matrix
	end tell
end tell

Thats brilliant!

Thanks for that.

EErrr… but…

I am VERY grateful but I think I’ve got another problem. Your brilliant script works fine but with one tiny problem. The reflect process does not seem to be about the exact centre of the group. If I mirror image the group manually the final position differs slightly from the final position of the group using your script. Why?

I would not be so nit picky about this if it wasn’t for the fact that the groups final position is very important. (the group is a set of co-ordinate orientated targets which is in sync with another machine)

I was hoping that if a group which has identical objects at either end is reflected, then the objects would appear to have not moved when the script is run. But it does!

Sorry if that is unclear. Basically, if you run your script on say a square, you’ll see the square shift slightly.

Thanks again

Tim Baldwin

Tim, to be honest I only loaded CS2 to our macs 2 days ago. And its the first time I’ve been able to get matrix’s to work without problem. However you are indeed right it does move from item center which is default adding this to the script has no effect I had not noticed this. To get around this I would pull the bounds info then repostion to the original bounds something like this. Hope this has helped.

tell application "Adobe Illustrator"
	activate
	set docRef to the current document
	tell docRef
		set groupXY to position of group item 1 of layer 1
		set RH_Matrix to ¬
			{class:matrix, mvalue_a:1.0, mvalue_b:0.0, mvalue_c:0.0, mvalue_d:-1.0, mvalue_tx:0.0, mvalue_ty:0.0}
		transform group item 1 of layer 1 using RH_Matrix
		set position of group item 1 of layer 1 to groupXY
	end tell
end tell

Tim,

For what its worth, here’s my stab at it. I found the same problem you did with the positioning. I had a similar problem with Ill CS when rotating an object. It would not rotate it around its center and I had to do something like this to get it back to the proper position. It may be a bug or just not enough understanding of how the matrix commands work. This script will “reflect” 90 degrees horizontal and vertical. I don’t know which you need.

tell application "Adobe Illustrator"
	activate
	set t to current document
	tell t
		set theGroups to every group item
		repeat with aG in theGroups
			set thePos to position of aG
			set someMatrix to get rotation matrix of aG
			set mvalue_a of someMatrix to -1.0
			transform aG using someMatrix
			set position of aG to thePos
			set someMatrix to get rotation matrix aG
			set mvalue_d of someMatrix to -1.0
			transform aG using someMatrix
			set position of aG to thePos
		end repeat
	end tell
end tell

A slightly different approach than mark67 but pretty much the same.

PreTech

Oops! Sorry, this is actually just rotating the objects. Heh, heh, he. . .:rolleyes:

Well, Ok maybe it is actually reflecting. I’m getting confused. I think I may have actually AppleScripted my brain and now I’m just a reflection of myself. Hummm. . .:smiley:

PreTech, are you getting bloated strokes with transfom matrix in the other commands?

Mark67,

I was when I first started playing with this. It seems that if the mvalue_b, c, or d is changed the stroke will expand to 100. At least it does when changing the values in the method we have used here. I tried just rotating the image and didn’t have the bloat problem. Don’t know why yet.

PreTech

result stroke weight is original times 100. And I have yet to work out the wording for “with line scale” it won’t let me put in my integers. I was going to try zero as something is wrong with the defaults math. I won’t spend the weekend worrying about it though.

Tim,

I know this has nothing to do with your immediate problem, but . . .

Mark67 here’s a work around solution to the stroke problem.

set theRecord to {tI:{}, strk:{}}
tell application "Adobe Illustrator"
	activate
	set docRef to the current document
	tell docRef
		
		set thG to every path item of group item 1
		repeat with i from 1 to count thG
			set strk's end of theRecord to stroke width of item i of thG
			set tI's end of theRecord to item i of thG
		end repeat
		
		set s to get scale matrix
		set s to concatenate scale matrix s horizontal scale 110 vertical scale 110
		transform group item 1 using s
		
		repeat with i from 1 to count thG
			set stroke width of (item i of tI of theRecord) to (item i of strk of theRecord)
		end repeat
		
	end tell
end tell

I haven’t tested it extensively, but it seems to work.

PreTech

Thanks everyone for your invaluable help.

I think I have enough info to complete the project.

I a bit of a newbie when it comes to scripting Ill CS2, the transfromation matrix is blowing my mind a bit. But I’m getting there with your help!

Thanks again

Tim