IDCS4 -- TextFrame

Friends,
I am facing a logical problem and I am confused what need to do in that. Below code is working fine.
I am looking for a solution so that this text frame should come at the center of the page if the either document height is 2in or 7in.
Currently what is happening is it starts from 0 origin and if I start with some negative value then it will be a problem for A4 size document.


tell application "Adobe InDesign CS4"
	set myDocument to active document
	try
		set myFont to font "Arial"
	end try
	set user interaction level of script preferences to never interact
	
	tell myDocument
		try
			set myPageItems to page item "myRemoveFrame"
			repeat with aFrame in myPageItems
				if class of aFrame is text frame then delete aFrame
			end repeat
		end try
		set myString to "Ad Number:" & tab & "Pooja Test 123" & tab & "Status:" & tab & "Operator Proof" & return & "Advertiser:" & tab & "Pooja Live" & tab & "Sales Rep:" & tab & "Pooja Live12" & return & "Size:" & tab & "8in x 11in" & tab & "Color:" & tab & "Process" & return & "Run Date:" & tab & "ajsdhjahsdhajshdahkdasd hjadjasdh hakjshdahsd ahdkjhsakdhkas"
		set myBounds to my myGetBounds(myDocument, page 1)
		set myPage to page 1 of myDocument
		tell myPage
			set myTextFrame to make text frame with properties {geometric bounds:my myGetBounds(myDocument, myPage), contents:myString}
			set label of myTextFrame to "myRemoveFrame"
			
		end tell
		set myStory to parent story of myTextFrame
		tell text 1 of myStory
			try
				set applied font to myFont
			end try
			set point size to 6
			set myTable to convert to table
		end tell
		tell myTable
			merge cell -3 of row 4 with cell -1 of row 4
		end tell
		
	end tell
end tell

on myGetBounds(myDocument, myPage)
	tell application "Adobe InDesign CS4"
		tell document preferences of myDocument
			set myPageWidth to page width
			set myPageHeight to page height
		end tell
		tell margin preferences of myPage
			if side of myPage is left hand then
				set myX2 to left
				set myX1 to right
			else
				set myX1 to left
				set myX2 to right
			end if
			set myY1 to top
			set myY2 to bottom
		end tell
		set myX2 to myPageWidth - myX2
		set myY2 to myPageHeight - myY2
		--		return {"-5pt", myX1, "-62pt", myX2}
		return {"-5pt", myX1, "-62pt", "504pt"}
	end tell
end myGetBounds

Regards,
Poo

Maybe this will help, dont know, got this a few years ago from a website, adapted it a bit, but I think you will be able to get what you need from it. Forget the Locale variables, these were only needed because it could switch language depending on the IP. The point is it aligns objects to the page, optionally considering margins:

set myObjectList to {}
try
	tell application "Adobe InDesign CS4"
		activate
		set myPageItemTypes to {rectangle, oval, polygon, text frame, graphic line, group}
		if (count documents) > 0 then
			set mySelection to selection
			if (count mySelection) > 0 then
				repeat with myCounter from 1 to (count mySelection)
					if (class of item myCounter of mySelection) is in myPageItemTypes then
						copy item myCounter of mySelection to end of myObjectList
					end if
				end repeat
				if (count myObjectList) > 0 then
					my myDisplayDialog(myObjectList)
				end if
			else
				set myResult to display alert LocaleError message LocaleErrorMsg2 buttons {"Instructions", "OK"} default button "OK" --"SVP Sélectionnez au moins un item et réessayer."
				if button returned of myResult = "Instructions" then
					my DisplayInstructions(LocaleInstructions)
				end if
			end if
		else
			set myResult to display alert LocaleError message LocaleErrorMsg1 buttons {"Instructions", "OK"} default button "OK" --"Il n'y a aucun document d'ouvert."
			if button returned of myResult = "Instructions" then
				my DisplayInstructions(LocaleInstructions)
			end if
		end if
	end tell
on error errMsg number errNum
	tell application "Adobe InDesign CS4" to display alert LocaleError message ((LocaleErrorOccured & " (start)" & return & return & errNum & " " & errMsg) as string) buttons {"OK"} default button "OK"
end try

on myDisplayDialog(myObjectList)
	try
		set LocaleDialogName to "Align to the page" as text
		set LocaleMsg1 to "Vertical alignement" as text
		set LocaleMsg2 to "Horizontal alignement" as text
		set LocaleMsg3 to "Top" as text
		set LocaleMsg4 to "Center" as text
		set LocaleMsg5 to "Bottom" as text
		set LocaleMsg6 to "Ignore" as text
		set LocaleMsg7 to "Left" as text
		set LocaleMsg8 to "Center" as text
		set LocaleMsg9 to "Right" as text
		set LocaleMsg10 to "Ignore" as text
		set LocaleMsg11 to "Respect margins" as text
		tell (system info) to set current_ip_v4_add to get IPv4 address
		repeat with i from 1 to (count of items in MTLipV4Addresses)
			if current_ip_v4_add begins with (item i in MTLipV4Addresses) then
				set LocaleDialogName to "Aligner par rapport à la page" as text
				set LocaleMsg1 to "Vertical" as text
				set LocaleMsg2 to "Horizontal" as text
				set LocaleMsg3 to "Haut" as text
				set LocaleMsg4 to "Centre" as text
				set LocaleMsg5 to "Bas" as text
				set LocaleMsg6 to "Ne rien faire" as text
				set LocaleMsg7 to "Gauche" as text
				set LocaleMsg8 to "Centre" as text
				set LocaleMsg9 to "Droite" as text
				set LocaleMsg10 to "Ne rien faire" as text
				set LocaleMsg11 to "Considérer les marges" as text
			end if
		end repeat
		tell application "Adobe InDesign CS4"
			set myDialog to make dialog with properties {name:LocaleDialogName} --"Aligner dans la page"
			tell myDialog
				tell (make dialog column)
					tell (make dialog row)
						tell (make dialog column)
							tell (make border panel)
								make static text with properties {static label:LocaleMsg1} --"Vertical"
								set myVerticalAlignmentButtons to make radiobutton group
								tell myVerticalAlignmentButtons
									make radiobutton control with properties {static label:LocaleMsg3, checked state:true} --"Haut"
									make radiobutton control with properties {static label:LocaleMsg4} --"Centre"
									make radiobutton control with properties {static label:LocaleMsg5} --"Bas"
									make radiobutton control with properties {static label:LocaleMsg6} --"Ne rien faire"
								end tell
							end tell
						end tell
						tell (make dialog column)
							tell (make border panel)
								make static text with properties {static label:LocaleMsg2} --"Horizontal"
								set myHorizontalAlignmentButtons to make radiobutton group
								tell myHorizontalAlignmentButtons
									make radiobutton control with properties {static label:LocaleMsg7, checked state:true} --"Gauche"
									make radiobutton control with properties {static label:LocaleMsg8} --"Centre"
									make radiobutton control with properties {static label:LocaleMsg9} --"Droite"
									make radiobutton control with properties {static label:LocaleMsg10} --"Ne rien faire"
								end tell
							end tell
						end tell
					end tell
					tell (make dialog row)
						set myConsiderMarginsCheckbox to make checkbox control with properties {static label:LocaleMsg11, checked state:false} --"Considérer les marges"
					end tell
				end tell
			end tell
			set myResult to show myDialog
			if myResult is true then
				set myVerticalAlignment to selected button of myVerticalAlignmentButtons
				set myHorizontalAlignment to selected button of myHorizontalAlignmentButtons
				set myConsiderMargins to checked state of myConsiderMarginsCheckbox
				destroy myDialog
				my myAlignEveryObjects(myObjectList, myVerticalAlignment, myHorizontalAlignment, myConsiderMargins)
			else
				destroy myDialog
			end if
		end tell
	on error errMsg number errNum
		tell application "Adobe InDesign CS4" to display alert LocaleError message ((LocaleErrorOccured & " (myDisplayDialog)" & return & return & errNum & " " & errMsg) as string) buttons {"OK"} default button "OK"
	end try
end myDisplayDialog

on myAlignEveryObjects(myObjectList, myVerticalAlignment, myHorizontalAlignment, myConsiderMargins)
	try
		tell application "Adobe InDesign CS4"
			set myPageHeight to page height of document preferences of active document
			set myPageWidth to page width of document preferences of active document
			set myOldRulerOrigin to ruler origin of view preferences of active document
			set ruler origin of view preferences of active document to page origin
			set zero point of active document to {0, 0}
			set myMarginPreferences to margin preferences of active page of active window
			set myTopMargin to top of myMarginPreferences
			set myLeftMargin to left of myMarginPreferences
			set myBottomMargin to bottom of myMarginPreferences
			set myRightMargin to right of myMarginPreferences
			if myConsiderMargins is true then
				set myXCenter to (myPageWidth / 2) - ((myRightMargin - myLeftMargin) / 2)
				set myYCenter to (myPageHeight / 2) - ((myBottomMargin - myTopMargin) / 2)
				if myHorizontalAlignment is 0 then
					set myX to myLeftMargin
				else if myHorizontalAlignment is 1 then
					set myX to myXCenter
				else if myHorizontalAlignment is 2 then
					set myX to myPageWidth - myRightMargin
				else if myHorizontalAlignment is 3 then
					set myX to nothing
				end if
				if myVerticalAlignment is 0 then
					set myY to myTopMargin
				else if myVerticalAlignment is 1 then
					set myY to myYCenter
				else if myVerticalAlignment is 2 then
					set myY to myPageHeight - myBottomMargin
				else if myVerticalAligment is 3 then
					set myY to nothing
				end if
			else
				set myXCenter to myPageWidth / 2
				set myYCenter to myPageHeight / 2
				if myHorizontalAlignment is 0 then
					set myX to 0
				else if myHorizontalAlignment is 1 then
					set myX to myXCenter
				else if myHorizontalAlignment is 2 then
					set myX to myPageWidth
				else if myHorizontalAlignment is 3 then
					set myX to nothing
				end if
				if myVerticalAlignment is 0 then
					set myY to 0
				else if myVerticalAlignment is 1 then
					set myY to myYCenter
				else if myVerticalAlignment is 2 then
					set myY to myPageHeight
				else if myVerticalAligment is 3 then
					set myY to nothing
				end if
			end if
			repeat with myCounter from 1 to (count myObjectList)
				my myAlignObject(item myCounter of myObjectList, myX, myY, myHorizontalAlignment, myVerticalAlignment)
			end repeat
			set ruler origin of view preferences of active document to myOldRulerOrigin
		end tell
	on error errMsg number errNum
		tell application "Adobe InDesign CS4" to display alert LocaleError message ((LocaleErrorOccured & " (myAlignEveryObjects)" & return & return & errNum & " " & errMsg) as string) buttons {"OK"} default button "OK"
	end try
end myAlignEveryObjects

on myAlignObject(myObject, myX, myY, myHorizontalAlignment, myVerticalAlignment)
	try
		tell application "Adobe InDesign CS4"
			set myBounds to geometric bounds of myObject
			--Geometric bounds are in the form {y1, x1, y2, x2}
			set myWidth to (item 4 of myBounds) - (item 2 of myBounds)
			set myHeight to (item 3 of myBounds) - (item 1 of myBounds)
			if myX is nothing then
				set myX to item 2 of myBounds
			end if
			if myY is nothing then
				set myY to item 1 of myBounds
			end if
			if myHorizontalAlignment is 0 then
				--do nothing
			else if myHorizontalAlignment is 1 then
				set myX to myX - (myWidth / 2)
			else if myHorizontalAlignment is 2 then
				set myX to myX - myWidth
			else if myHorizontalAlignment is 3 then
				--do nothing
			end if
			if myVerticalAlignment is 0 then
				--do nothing
			else if myVerticalAlignment is 1 then
				set myY to myY - (myHeight / 2)
			else if myVerticalAlignment is 2 then
				set myY to myY - myHeight
			else if myVerticalAlignment is 3 then
				--do nothing
			end if
			tell myObject to move to {myX, myY}
		end tell
	on error errMsg number errNum
		tell application "Adobe InDesign CS4" to display alert LocaleError message ((LocaleErrorOccured & " (myAlignObject)" & return & return & errNum & " " & errMsg) as string) buttons {"OK"} default button "OK"
	end try
end myAlignObject

Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Dear All,
Finally I come up with another approach but there is some problem in code so it is not working. Please check and correct me.


tell application "Adobe InDesign CS4"
	set myDocument to active document
	try
		set myFont to font "Arial"
	end try
	set user interaction level of script preferences to never interact
	
	tell myDocument
		tell view preferences
			set oldH to horizontal measurement units
			set oldV to vertical measurement units
			set horizontal measurement units to inches
			set vertical measurement units to inches
		end tell
		
		try
			set myPageItems to page item "myRemoveFrame"
			repeat with aFrame in myPageItems
				if class of aFrame is text frame then delete aFrame
			end repeat
		end try
		set myString to "Ad Number:" & tab & "Pooja Test 123" & tab & "Status:" & tab & "Operator Proof" & return & "Advertiser:" & tab & "SacBee Live" & tab & "Sales Rep:" & tab & "SacBee Live" & return & "Size:" & tab & "8in x 11in" & tab & "Color:" & tab & "Process" & return & "Run Date:" & tab & "ajsdhjahsdhajshdahkdasd hjadjasdh hakjshdahsd ahdkjhsakdhkas"
		set myBounds to my myGetBounds(myDocument, page 1)
		set myPage to page 1 of myDocument
		tell myPage
			set myTextFrame to make text frame with properties {geometric bounds:my myGetBounds(myDocument, myPage), contents:myString}
			set label of myTextFrame to "myRemoveFrame"
			
		end tell
		set myStory to parent story of myTextFrame
		tell text 1 of myStory
			try
				set applied font to myFont
			end try
			set point size to 6
			set myTable to convert to table
		end tell
		tell myTable
			merge cell -3 of row 4 with cell -1 of row 4
		end tell
		
	end tell
	tell view preferences of myDocument
		set horizontal measurement units to oldH
		set vertical measurement units to oldV
	end tell
end tell

on myGetBounds(myDocument, myPage)
	tell application "Adobe InDesign CS4"
		tell document preferences of myDocument
			set myPageWidth to page width
			set myPageHeight to page height
		end tell
				if myPageWidth is greater than "8" then
					return {"0pt", "150pt", "62pt", "400pt"}
				else if myPageWidth is greater than "7" then and  less than "8" then
					return {"36pt", "-50pt", "0pt", "200pt"}
		else if myPageWidth between "6" and "7" then
			return {"-5pt", "100pt", "-62pt", "400pt"}
		
				end if
		
	end tell
end myGetBounds


Regards,
Poo