Upgrading InDesign CS3 script to CC

I have a frustrating issue with a script I am converting from an InDesign CS3 script to a CC script. Having converted a few already, successfully, I am stumped on one line of this script.

It is for a reprographic department and takes a multi-page PDF and makes an imposition that fits our large format printer. The way it works is: It temporarily loads page 1 of the PDF and measures it. Once it has the measurements it asks a few questions (how many pages are in the PDF and checks that it has gotten the correct dimensions from the PDF) and then asks you what page you want to start the imposition from as sometimes people want to skip page 1 as it is a blank facing page. It then calculates how many pages it can impose across the page width etc.

The script fetches, places and measures the first page perfectly. It passes the info on but when it comes to place all of the pages the place command fails. It’s identical to the place command in the first part of the script!!

tell application "Adobe InDesign CC"
	activate
	--set PDF crop of PDF place preferences to crop bleed
	tell PDF place preferences
		set page number to 4
		set PDF crop to crop bleed
	end tell
	
	set mydoc to make document
	
	--Set Units to millimeters and ruler to 0,0
	tell view preferences of mydoc
		set ruler origin to page origin
		set horizontal measurement units to millimeters
		set vertical measurement units to millimeters
		set show frame edges to true
	end tell
	
	--Set Document Margins
	tell master spread 1 of mydoc
		tell margin preferences of pages
			set top to 0
			set left to 0
			set bottom to 0
			set right to 0
		end tell
	end tell
	
	-----------------------
	--Document Prefs--
	-----------------------
	tell document preferences of mydoc
		set facing pages to false
		
		-- Bleed
		set document bleed top offset to "0mm"
		set document bleed uniform size to true
		
	end tell
	
	
	set myfile to choose file
	
	set tempbox to make rectangle of page 1 of mydoc
	tell tempbox
		
		set {myfile} to place (myfile as alias) on tempbox
		--«event K2  plac» myfile
		
		set stroke weight to 0
		fit given frame to content
		--«event K2  fitc» given «class givn»:frame to content
		
		set myy1 to item 1 of geometric bounds
		set myx1 to item 2 of geometric bounds
		set myy2 to item 3 of geometric bounds
		set myx2 to item 4 of geometric bounds
		set defwidth to myx2 - myx1 as integer
		set defheight to myy2 - myy1 as integer
	end tell
	
	
	--delete tempbox

	set maxwidth to 1060
	set maxheight to 1100
	display dialog {"Enter the number of pages from the PDF to impose"} default answer "1"
	set pgcount to the text returned of the result as integer
	display dialog {"Enter the number of the first page in the PDF to impose"} default answer "1"
	set pgstart to the text returned of the result as integer
	set pgstart to pgstart - 1
	display dialog {"Enter the width of the individual PDF pages"} default answer defwidth
	set pgwidth to the text returned of the result as integer
	display dialog {"Enter the height of the individual PDF pages"} default answer defheight
	set pgheight to the text returned of the result as integer
	set pgacross to (maxwidth / pgwidth) - 0.5 as integer
	if pgacross > pgcount then set pgacross to pgcount
	display dialog {"Confirm: " & pgacross & " pages across (" & pgacross * pgwidth & "mm wide)"} default answer pgacross
	set pgacross to the text returned of the result as integer
	set docwidth to pgacross * pgwidth
	set pgrows to (pgcount / pgacross) + 0.4999 as integer
	set pgdown to (maxheight / pgheight) - 0.5 as integer
	if pgdown > pgrows then set pgdown to pgrows
	display dialog {"Confirm: " & pgdown & " pages down (" & pgdown * pgheight & "mm tall)"} default answer pgdown
	set pgdown to the text returned of the result as integer
	set docheight to pgdown * pgheight
	set docpages to (pgcount / (pgacross * pgdown)) + 0.4999 as integer
	if docpages = 1 then
		display dialog {"Confirm: 1 InDesign page"} default answer 1
	else
		display dialog {"Confirm: " & docpages & " InDesign pages"} default answer docpages
	end if
	set docpages to the text returned of the result as integer
	if docwidth > docheight then
		set pgorient to "L"
	else
		set pgorient to "P"
	end if
	tell document preferences of mydoc
		set page width to docwidth
		set page height to docheight
		set pages per document to docpages
		set facing pages to false
		if pgorient = "L" then
			set page orientation to landscape
		else
			set page orientation to portrait
		end if
	end tell
	tell active window
		zoom given fit page
	end tell
	repeat with p from 1 to docpages
		repeat with y from 1 to pgdown
			repeat with x from 1 to pgacross
				set bottomval to y * pgheight
				set topval to bottomval - pgheight
				set rightval to x * pgwidth
				set leftval to rightval - pgwidth
				set pgoffset to pgdown * pgacross * (p - 1)
				set pgnumber to x + (y - 1) * pgacross + pgoffset
				if pgnumber > pgcount then
					-- do nothing
				else
					tell PDF place preferences
						set page number to pgnumber + pgstart
						set transparent background to false
						set PDF crop to crop bleed
					end tell
					
					display dialog pgnumber + pgstart
					
					set mybox to make rectangle of page p of mydoc
					tell mybox
						set geometric bounds of mybox to {topval, leftval, bottomval, rightval}
						set stroke weight to 0
						
						set {myfile} to place (myfile as alias) on mybox
						--«event K2  plac» myfile
						
					end tell
				end if
			end repeat
		end repeat
	end repeat
end tell

The commented out “–«event K2 plac» my file”, the first time it hits, is the old code that worked in CS3. The replacement code above it, “set {myfile} to place (myfile as alias) on temp box”, works perfectly fine. I have commented out the --delete temp box just so I could check that the script was successfully pulling in the first page and measuring it properly. It is. I also changed (just as a test) what page it brings in to measure to check that I was able to choose a specific page with my new code.

Further down the script I have added “display dialog pgnumber + pgstart” so that I could check that the pages were incrementing properly. It is. When I get the script working properly I will delete this line.

Just after this we see the “set {myfile} to place (myfile as alias) on my box” again which, in the CS3 script pulls in the proper page from the PDF. This is where it fails on this CC version. If I comment this line out it successfully makes the correct size frames in the correct places, but obviously empty.

So the only line that is screwing up is that second “set {myfile} to place (myfile as alias) on my box” and I cannot work out why. It works in the first occurrence and it works in CS3 (with the old code. If I add a second “Choose file” command right before this, it works ok. So somewhere it is losing the myfile variable. Doesn’t lose it in CS3 though.

The error I’m getting is “Can’t make «class PDF » id 216 of «class crec» id 213 of «class sprd» id 198 of document id 43 of application “Adobe InDesign CC” into type alias.”

If anyone could help I’d be most grateful.

Many thanks

When you first run this:

   set myfile to choose file
   
   set tempbox to make rectangle of page 1 of mydoc
   tell tempbox
       
       set {myfile} to place (myfile as alias) on tempbox

The result of the place command is whatever you placed, in this case presumably a PDF. So myfile no longer contains a file, which is why you get an error when you try to use it again in a place command.

Just use:

place myfile on tempbox

You don’t use the result anywhere, so there’s no point storing it, and the “as alias” is redundant because choose file returns an alias.

Thank you so much Shane!!

For some reason I had a total mental block on this and your explanation proves that I didn’t have a full understanding on how things were handled.

Many, many, many thanks.

Paul.