InDesign Placing Text Frames On Page 1 instead of On Page 2

I have a script that measures text frames and places those measurements as text inside the text frames. The problem I’m having is that InDesign wants to place the measurements for page 2 of the spread on page 1. What do I need to change to get InDesign to place those measurements on page 2?



				--------------------------------------------------------------------------------------------------------------
				--CREATING SEPARATE NEW LAYER FOR THE MEASURMENTS AND TEXT BOXES IF IT DOESN'T EXIST
				--------------------------------------------------------------------------------------------------------------

				open afile
				
				
				tell document 1 -->Beginning of tell document 1 block
					try
						set measuringLayer to make layer with properties {name:"Measure"}
						
						--Telling script to use existing measure layer if it already exists
					on error
						set measuringLayer to layer "Measure"
					end try



					--------------------------------------------------------------------------------------------------------------
					--GETTING MEASUREMENTS AND ADDING THEM AS TEXT TO THE MEASURED TEXT FRAME
					--------------------------------------------------------------------------------------------------------------

--Displaying a message to the user if there are no text frames on the measure layer
					if (count of every text frame of measuringLayer) is 0 then -->Beginning of if statement
						set boxesDontExist to true -->Using this to display a dialog further down in the script, after the repeat loop ends.
						
						
						--Getting the measurements of every text frame on the measure layer
					else
						
						set my_pages to every page
						repeat with a_page in my_pages
							set page_name to get the name of a_page
							
							
							set boxesExist to true -->Using this to display a dialog further down in the script, after the repeat loop ends.
							set measuringTextFrames to (every text frame of page page_name whose item layer is measuringLayer)
							repeat with measuringTextFrame in measuringTextFrames -->Beginning of second repeat loop
								
								
								--Geometric bounds are {top side, left side, bottom side, right side}, also referred to as {y1, x1, y2, x2}
								set y2 to item 3 of geometric bounds of measuringTextFrame -->y2 is the bottom side of the frame
								set y1 to item 1 of geometric bounds of measuringTextFrame -->y1 is the top side of the frame
								set x1 to item 2 of geometric bounds of measuringTextFrame -->x1 is the left side of the frame
								set x2 to item 4 of geometric bounds of measuringTextFrame -->x2 is the right side of the frame
								
								
								--Doing some math with those measurements to figure out the square inches of each frame
								set frameHeight to y2 - y1 -->bottom of frame - top of frame = height of the frame
								set frameWidth to x2 - x1 -->right side of frame - left side of frame = width of the frame
								set frameSquareInchesLong to frameHeight * frameWidth -->multiplying height time width to get square inches
								set frameSquareInches to my roundThis(frameSquareInchesLong, 2) -->making the number round to only 2 decimal points instead of a gazillion
								set inchMarks to {"''"} -->Adding the inch marks in between quotes
								set measurementText to frameSquareInches & inchMarks as string -->Final result followed by the inch marks (example: 3.08")
								
								
								--Creating the text frames that will hold the measurements
								tell page page_name to set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0}
								
								
								--Inserting the square inches figured into each frame as text
								tell displayTextFrame
									
									--Adding the drop shadow to the text
									apply object style using dropShadowStyle
									
									--Adding the calculated result for square inches in each text frame 
									set contents of insertion point -1 to measurementText
									
								end tell
								
							end repeat -->End of second repeat loop
							
							
							
							
							
						end repeat --> End of repeat with page loop
					end if -->End of if statement

You don’t need to get each page name. Just refer to the page as the page itself as you get it from my_pages. So you could make a new text frame inside the repeat loop like this:


tell a_page to make text frame...

I tried this and it didn’t work properly when the two pages are in a spread. It did work when the two pages are not in a spread. Any ideas how I can get this to work properly in a two page spread? (It still puts the two text frames on top of one another on page 1 if the two pages are in a spread).

tell application "Adobe InDesign CC 2017"
	activate
	tell active document
		set my_pages to every page
		repeat with a_page in my_pages
			tell a_page to make text frame with properties {geometric bounds:{"1", "1", "5", "6"}, stroke color:"Black", stroke weight:3}
			log a_page
		end repeat
	end tell
end tell

Hi. The problem is caused by your ruler origin preference(s), as previously discussed here:
http://macscripter.net/viewtopic.php?id=40149 --ruler origin

You can adjust this setting or use an alternate method. Since your targets appear to be an entire document’s text frames, a page reference isn’t needed to limit them.

tell application "Adobe InDesign CS3"
	tell document 1
		repeat with aFrame in (get text frames)
			tell (duplicate aFrame)
				set {y1, x1, y2, x2} to its geometric bounds
				set contents to my roundThis(((y2 - y1) * (x2 - x1)), 2) & "''" as text
				move by {0.05, 0.075}
			end tell
		end repeat
	end tell
end tell


on roundThis()
	--unspecified rounding handler
end roundThis

It’s now working in both CC 2017 and CS5. :). I ended up having it check whether or not the pages were two page spreads or single pages. If it’s a two page spread, I have it setting the view preferences’s ruler origin to spread origin. Here’s the full script in case anyone else runs into this problem.

on open droppedfiles
	with timeout of 900 seconds --Script tries for 15 minutes before timing out. Default timeout is 2 minutes.
		
		
		
		--------------------------------------------------------------------------------------------------------------
		------------------------------------- CHANGING PREFERENCES --------------------------------------
		--------------------------------------------------------------------------------------------------------------
		
		
		tell application id "com.adobe.indesign"
			--"Adobe InDesign CS5.5" -->Beginning of tell InDesign block
			activate
			
			--GETTING RID OF INTERRUPTING MISSING FONT & IMAGE DIALOG BOXES
			set user interaction level of script preferences to never interact
			
			
			--SETTING MEASUREMENTS TO INCHES
			tell view preferences
				--Measurement unit choices are: 
				--picas, points, inches, inches decimal, millimeters, centimeters, or ciceros 
				--Set horizontal and vertical measurement units to points. 
				set horizontal measurement units to inches decimal
				set vertical measurement units to inches decimal
			end tell
			
			
			
			------------------------------------------------------------------------------------------------------------------------------
			---------------------------------- GETTING THE PATH TO THE FOLDER AFILE IS IN --------------------------------
			------------------------------------------------------------------------------------------------------------------------------
			
			repeat with afile in droppedfiles
				
				--DECLARING A VARIABLE TO BE USED IN CREATING NEW FOLDERS LATER ON
				tell application "Finder" to set parentFolder to my getParentFolder(afile) as string --Calling the getParentFolder function used to figure out the folder that a file is in. See bottom of the script.
				
				
				
				------------------------------------------------------------------------------------------------------------------------------
				----------------------------  CONVERTING LEGACY IDML FILES TO INDESIGN FILES  -----------------------------
				------------------------------------------------------------------------------------------------------------------------------
				
				
				tell application "Finder" to set file_ext to name extension of afile
				
				if file_ext contains "idml" then
					open afile as alias
					
					tell application "Finder"
						activate
						set oldDelims to AppleScript's text item delimiters
						
						set AppleScript's text item delimiters to "."
						set fileName to name of afile --> ex. Disneyland.tif
						set nameWithoutExtension to first text item of fileName --> Disneyland
						set newName to nameWithoutExtension & ".indd" --> Disneyland_s_4c.tif
						--set name of afile to newName -->Here is where the file actually gets renamed 
						
						
						--TELLING THE COMPUTER THAT THE FOLDER WE'RE ABOUT TO MOVE IS THE NEWLY NAMED ONE
						--The original dropped file no longer exists as far as the computer is concerned because we changed the filename
						set filePath to parentFolder & newName as string
						
						--RESETTING APPLESCRIPT'S TEXT ITEM DELIMITERS
						set AppleScript's text item delimiters to oldDelims
						
						
					end tell
					
					
					--Saving the idml file as an InDesign file
					tell active document to save to file filePath
					close active document saving no
					
					
					--Setting afile to the InDesign file
					set afile to filePath as alias
					
				end if
				
				
				--------------------------------------------------------------------------------------------------------------
				--CREATING SEPARATE NEW LAYER FOR THE MEASURMENTS AND TEXT BOXES IF IT DOESN'T EXIST
				--------------------------------------------------------------------------------------------------------------
				
				open afile
				
				
				tell document 1 -->Beginning of tell document 1 block
					try
						set measuringLayer to make layer with properties {name:"Measure"}
						
						--Telling script to use existing measure layer if it already exists
					on error
						set measuringLayer to layer "Measure"
					end try
					
					
					--------------------------------------------------------------------------------------------------------------
					--DETERMINING IF THE RULER ORIGIN SHOULD BE SET TO PAGE OR SPREAD
					--------------------------------------------------------------------------------------------------------------
					
					--Counting how many pages are in each spread 
					set my_spreads to every spread
					repeat with my_spread in my_spreads
						set spread_page_count to count of pages in my_spread
						log spread_page_count
					end repeat
					
					
					--Changing the ruler origin to spread origin if there is a two page spread
					if spread_page_count is greater than 1 then
						set view preferences's ruler origin to spread origin
					else
						set view preferences's ruler origin to page origin
					end if
					
					
					--------------------------------------------------------------------------------------------------------------
					--LOCKING EVERY LAYER EXCEPT THE MEASURE LAYER
					--------------------------------------------------------------------------------------------------------------
					
					try
						set the properties of every layer to {locked:true}
						set the properties of measuringLayer to {locked:false}
					end try
					
					
					--------------------------------------------------------------------------------------------------------------					
					--DELETING TEXT BOXES WITH MEASUREMENTS IF THEY ALREADY EXIST, SO NEW BOXES WITH MEASURMENTS CAN BE CREATED
					--------------------------------------------------------------------------------------------------------------
					
					try
						
						set boxesToDelete to (every text frame of measuringLayer whose stroke weight is 0)
						delete boxesToDelete
					end try
					
					
					--------------------------------------------------------------------------------------------------------------
					--UNGROUPING ALL ITEMS ON THE MEASURING LAYER SO THAT BOXES CAN BE CONVERTED TO TEXT FRAMES
					--------------------------------------------------------------------------------------------------------------
					
					tell measuringLayer
						try
							repeat until (count of every group) = 0
								ungroup every group
							end repeat
						end try
					end tell
					
					
					
					--------------------------------------------------------------------------------------------------------------
					--SETTING FORMATTING FOR THE TEXT
					--------------------------------------------------------------------------------------------------------------
					
					tell text defaults
						
						--Choosing Arial or Helvetica for the font
						try
							set applied font to "Arial Bold" --Making the font Arial
						on error
							try
								set applied font to "Helvetica" --Making the font Helvetica if Arial isn't on the user's computer
							end try
						end try
						
						--Setting the other formatting for the text
						try
							set font style to "Bold" -->Making the text bold
						end try
						set fill color to "Paper" -->Making the text white
						set stroke color to "Black" -->Putting a black stroke around the now white text
						set point size to 1 -->Resetting the point size to 1 so it doesn't give the data out of range error message
						set point size to 22 -->Making the text 22 point
					end tell
					
					
					--------------------------------------------------------------------------------------------------------------
					--CREATING THE DROP SHADOW OBJECT STYLE
					--------------------------------------------------------------------------------------------------------------
					
					try
						set dropShadowStyle to object style "Drop Shadow"
					on error
						--The character style did not exist, so create it.
						set dropShadowStyle to make object style with properties {name:"Drop Shadow", enable drop shadow:true, enable transparency:true, enable stroke:false}
						
						tell dropShadowStyle
							--Applying the drop shadow to the text
							tell transparency settings
								set properties of drop shadow settings to {mode:drop, use global light:true, opacity:100.0, spread:10.0, blend mode:multiply, noise:1.0, x offset:"p2", distance:"p4", size:"p1", effect color:"Black"}
							end tell
						end tell
					end try
					
					
					--------------------------------------------------------------------------------------------------------------
					--GETTING MEASUREMENTS AND ADDING THEM AS TEXT TO THE MEASURED TEXT FRAME
					--------------------------------------------------------------------------------------------------------------
					
					--Converting rectangles to text frames, in case rectangles are drawn instead of text frames
					--The script will be measuring only text frames
					
					try
						set allRectangles to every rectangle of measuringLayer
						repeat with aRectangle in allRectangles
							tell aRectangle
								set (content type of aRectangle) to text type
							end tell
						end repeat
					end try
					
					
					--Displaying a message to the user if there are no text frames on the measure layer
					if (count of every text frame of measuringLayer) is 0 then -->Beginning of if statement
						set boxesDontExist to true -->Using this to display a dialog further down in the script, after the repeat loop ends.
						
						
						--Getting the measurements of every text frame on the measure layer
					else
						
						set my_spreads to every spread
						repeat with a_spread in my_spreads
							
							set boxesExist to true -->Using this to display a dialog further down in the script, after the repeat loop ends.
							set measuringTextFrames to (every text frame of a_spread whose item layer is measuringLayer)
							repeat with measuringTextFrame in measuringTextFrames -->Beginning of second repeat loop
								--set parent_page to get parent page of measuringTextFrame
								
								--Geometric bounds are {top side, left side, bottom side, right side}, also referred to as {y1, x1, y2, x2}
								set y2 to item 3 of geometric bounds of measuringTextFrame -->y2 is the bottom side of the frame
								set y1 to item 1 of geometric bounds of measuringTextFrame -->y1 is the top side of the frame
								set x1 to item 2 of geometric bounds of measuringTextFrame -->x1 is the left side of the frame
								set x2 to item 4 of geometric bounds of measuringTextFrame -->x2 is the right side of the frame
								
								--Doing some math with those measurements to figure out the square inches of each frame
								set frameHeight to y2 - y1 -->bottom of frame - top of frame = height of the frame
								set frameWidth to x2 - x1 -->right side of frame - left side of frame = width of the frame
								set frameSquareInchesLong to frameHeight * frameWidth -->multiplying height time width to get square inches
								set frameSquareInches to my roundThis(frameSquareInchesLong, 2) -->making the number round to only 2 decimal points instead of a gazillion
								set inchMarks to {"''"} -->Adding the inch marks in between quotes
								set measurementText to frameSquareInches & inchMarks as string -->Final result followed by the inch marks (example: 3.08")
								
								--display dialog page_name as string
								
								--Creating the text frames that will hold the measurements
								--tell a_page to set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0}
								--tell a_page to set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0, parent page:a_page}
								--set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0, parent page:a_page}
								--set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0, parent:a_page}
								tell a_spread to set displayTextFrame to make text frame with properties {layer:measuringLayer, geometric bounds:{(y1 + "0.05"), (x1 + "0.075"), y2, x2}, stroke color:"None", stroke weight:0}
								
								--Inserting the square inches figured into each frame as text
								tell displayTextFrame
									
									--Adding the drop shadow to the text
									apply object style using dropShadowStyle
									
									--Adding the calculated result for square inches in each text frame 
									set contents of insertion point -1 to measurementText
									
								end tell
								
							end repeat -->End of second repeat loop
							
							
							
							
							
						end repeat --> End of repeat with page loop
					end if -->End of if statement
					
					
					
					--SAVING AND CLOSING EACH DOCUMENT
					close saving yes
					
					
				end tell -->End of tell document 1 block
			end repeat -->End of first repeat loop
		end tell -->End of tell InDesign block
		
		--------------------------------------------------------------------------------------------------------------
		--------------------------------------------------------------------------------------------------------------
		
		
		
		
		--------------------------------------------------------------------------------------------------------------
		--NOTIFYING THE USER THAT THE TEXT FRAMES TO MEASURE DON'T EXIST
		--------------------------------------------------------------------------------------------------------------
		
		try
			if boxesDontExist is true then
				tell application "SystemUIServer" to display dialog "Some of the dropped pages did not contain any text frames on the layer named 'Measure'." & return & return & "This script has left those pages open, so you can draw the text frames on the layer named 'Measure' and drop them on the script again." with icon 2 giving up after 600
			end if
			
			
			--NOTIFYING THE USER THAT THE SCRIPT HAS COMPLETED SUCCESSFULLY
		on error
			if boxesExist is true then
				tell application "SystemUIServer" to display dialog "All the dropped pages have been measured and are ready to drag into the completed folder." with icon note giving up after 15 -->This dialog box will close by itself after 15 seconds
			end if
		end try
		
		
	end timeout
end open

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------------
---------------------------------------FUNCTIONS -------------------------------------
-------------------------------------------------------------------------------------------

--FUNCTION TO GET PATH TO THE PARENT FOLDER
on getParentFolder(filePath)
	set folderParts to my GetTextItem(filePath as text, ":", {1, -2})
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set parentFolder to folderParts as text
	set AppleScript's text item delimiters to oldDelims
	
	return (parentFolder & ":") as text
end getParentFolder


--FUNCTION TO CHECK IF A FOLDER EXISTS YET
on CheckForFolder(thisFolder)
	tell application "Finder"
		return (exists folder thisFolder)
	end tell
end CheckForFolder

on GetTextItem(ThisString, ThisDelim, ThisItem)
	-- ThisString -> String to look in
	-- ThisDelim -> Text element that delimit the string
	-- ThisItem -> Number of the element to return
	copy the text item delimiters to oldDelims
	set the text item delimiters to ThisDelim
	set arrItem to every text item of ThisString
	
	if class of ThisItem is list then
		set FromItem to item 1 of ThisItem
		set ToItem to item 2 of ThisItem
		set TheReturn to (items FromItem thru ToItem of arrItem) as text
	else
		if ThisItem ≠ 0 then
			set TheReturn to (item ThisItem of arrItem) as text
		else
			set TheReturn to arrItem -- return every items
		end if
	end if
	set the text item delimiters to oldDelims
	return TheReturn
end GetTextItem


--FUNCTION NEEDED TO ROUND THE NUMBERS TO THE NEAREST 2 DECIMAL PLACES
--Many thanks to the people on the scripting forum who wrote and posted this function
--If you need people like that to help you out, try publi-script.com and macscripter.net

roundThis(75.5436, 1) --this will round to 75.5

on roundThis(n, numDecimals)
	set x to 10 ^ numDecimals
	(((n * x) + 0.5) div 1) / x
end roundThis