Quark Users - Center Box On Page (The Real Deal)

Hello Quark Users,

As you probably already know Quark gives you some Applescripts with the application. One of them is center box on page. The problem with this script is it doesn’t work on rotated boxes because of how Quark handles an objects point of origin. So… I dusted off the old geometry book and solved the problem.

CarbonQuark


try
	tell application "QuarkXPress"
		tell document 1
			set tool mode to drag mode
			activate
			set PAGE_CENTER_X to ((page width as number) / 2)
			set PAGE_CENTER_Y to ((page height as number) / 2)
			set BOX_SELECTED to selection
			if BOX_SELECTED is null then error display dialog "Please select an item to center and try again" buttons {"Cancel"}
			set BOX_BOUNDS to bounds of BOX_SELECTED as list
			set BOX_ROTATION to rotation of BOX_SELECTED as number
		end tell
	end tell
	
	set BOX_HEIGHT to (item 3 of BOX_BOUNDS as number) - (item 1 of BOX_BOUNDS as number)
	set BOX_WIDTH to (item 4 of BOX_BOUNDS as number) - (item 2 of BOX_BOUNDS as number)
	set BOX_CENTER_RAD to (((BOX_HEIGHT / 2) * (BOX_HEIGHT / 2)) + ((BOX_WIDTH / 2) * (BOX_WIDTH / 2))) ^ 0.5
	set BOX_CENTER_RAD_ANGLE to inverse_tangent_of(BOX_WIDTH / BOX_HEIGHT)
	set BOX_ROTATED_CENTER_RAD_ANGLE to BOX_CENTER_RAD_ANGLE + (BOX_ROTATION)
	set BOX_LEG_Y to (sin(180 - (BOX_ROTATED_CENTER_RAD_ANGLE + 90))) * BOX_CENTER_RAD
	set BOX_LEG_X to (sin(BOX_ROTATED_CENTER_RAD_ANGLE)) * BOX_CENTER_RAD
	
	tell application "QuarkXPress"
		tell document 1
			set origin of bounds of BOX_SELECTED to {PAGE_CENTER_Y - BOX_LEG_Y, PAGE_CENTER_X - BOX_LEG_X}
		end tell
	end tell
end try
beep

------------------------------------------------------------------------------------
-- Begin Subroutines --
------------------------------------------------------------------------------------

on sin(x)
	repeat until x > 0 and x < 360
		if x > 360 then
			set x to x - 360
		end if
		if x < 0 then
			set x to x + 360
		end if
	end repeat
	set x to x * (2 * pi) / 360 --convert from degrees to radians
	set answer to 0
	set numerator to x
	set denominator to 1
	set factor to -(x ^ 2)
	repeat with i from 3 to 40 by 2
		set answer to answer + numerator / denominator
		set numerator to numerator * factor
		set denominator to denominator * i * (i - 1)
	end repeat
	return answer
end sin

on cosine_of(x)
	repeat until x ≥ 0 and x < 360
		if x ≥ 360 then
			set x to x - 360
		end if
		if x < 0 then
			set x to x + 360
		end if
	end repeat
	set x to x * (2 * pi) / 360 --convert from degrees to radians
	set answer to 0
	set numerator to 1
	set denominator to 1
	set factor to -(x ^ 2)
	repeat with i from 2 to 40 by 2
		set answer to answer + numerator / denominator
		set numerator to numerator * factor
		set denominator to denominator * i * (i - 1)
	end repeat
	return answer
end cosine_of

on tan(x) --x is in degrees
	set answer to sine_of(x) / (cosine_of(x))
	return answer
end tan

on inverse_tangent_of(x) --x is ratio of opposite to adjacent sides of triangle
	set complimentFlag to false
	if x > 1 or x < -1 then
		set x to 1 / x
		set complimentFlag to true
	else if x = 1 then
		return 45.0
	end if
	set answer to 0
	set numerator to x
	set denominator to 1
	set ratio to x
	set factor to -(x ^ 2)
	repeat while abs(ratio) > 1.0E-4
		set answer to answer + ratio
		set numerator to numerator * factor
		set denominator to denominator + 2
		set ratio to numerator / denominator
	end repeat
	set answer to answer * 360 / (2 * pi) --convert from radians to degrees
	if complimentFlag is true then
		set answer to 90 - answer
	end if
	return answer
end inverse_tangent_of

on abs(numericVariable)
	if numericVariable < 0 then
		return -numericVariable
	else
		return numericVariable
	end if
end abs

Cool! I just tested it and it works great.

Thanks, CQ.

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
AppleScript: Tiger
Operating System: Mac OS X (10.4)