(*

Set Scale of every Picture in every Picture Box to 100%, crop, save as jpeg, replace

I made this script because there was a quark document which had lots of high resolution TIFFs placed in it, and I wanted to upload it for someone, and the tiffs were much too big. This script saves the quark images as jpegs, which are much smaller, and it also crops the images to match (approximately) the quark picture boxes. Then it replaces the TIFF with the newly saved JPEG. Quark Xpress doesn't seem to get the exact size of an image correct, so I made the script ask for a value to overlap the right and bottom side, and .02 inches seems sufficient. If the operation was done precisely, there seemed to be extra white edges sometimes. If your document requires very precise image placement, double check it after the script runs. And run it on a copy of your document, because there's no "undo". And if an image appears more than once in the document with different cropping, that will cause a problem. How to fix that problem is left as an exercise for the reader.

This works with Quark express 4.1.1 and PhotoShop 8 (CS). Adobe PhotoShop 8 scripting interface seems to expect percentage values for image resizing as a fraction of 1 vs 100, ie, it expects the value .5 for 50% rather than 50 for 50% like PhotoShop 7 did.



*)

tell application "Adobe Photoshop CS"
	set myOptions to {class:JPEG save options, embed color profile:true, format options:optimized, quality:10}
end tell
set fudgeFactor to 0.02
set doneCount to 0
set theResult to (display dialog "This script tries to crop images in quark to fit in the picture boxes, and save them as jpegs in a chosen folder, and link quark to the jpegs (make backups!). How much image overlap on right and bottom edge?" default answer fudgeFactor)
set fudgeFactor to text returned of theResult as real

set theSaveFolChoose to choose folder with prompt "Choose Destination of cropped JPEG images:"
set theSaveFol to theSaveFolChoose as string

tell front document of application "QuarkXPress™ 4.11"
	activate
	set numPages to count of pages
	set imageList to {}
	repeat with i from 1 to numPages
		set boxCount to count of picture boxes of page i
		--	display dialog boxCount
		repeat with bx from 1 to boxCount
			set bxName to (file path of images of picture box bx of page i)
			
			if bxName ≠ null then
				
				--set bxName to file path of image 1 of picture box bx of page i
				--display dialog bxName
				set bxNameTxt to (coerce bxName to string)
				set bxScale to (scale of image 1 of picture box bx of page i) as list
				set bxPercentX to (coerce item 2 of bxScale to real)
				set bxPercentY to (coerce item 1 of bxScale to real)
				--display dialog bxPercentX & "% width " & bxPercentY & "% height" as string
				set bxOffset to (offset of image 1 of picture box bx of page i) as list
				--set bxImBounds to (bounds of image 1 of picture box bx of current page) as list
				--display dialog bxImBounds
				set bxBounds to (bounds of picture box bx of page i) as list
				--display dialog bxBounds
				set bxOffsetY to (coerce item 1 of bxOffset to real)
				set bxOffsetX to (coerce item 2 of bxOffset to real)
				set bxBoundsY1 to (coerce item 1 of bxBounds to real)
				set bxBoundsX1 to (coerce item 2 of bxBounds to real)
				set bxBoundsY2 to (coerce item 3 of bxBounds to real)
				set bxBoundsX2 to (coerce item 4 of bxBounds to real)
				if bxPercentX ≠ 100 then
					
				end if
				
				--display dialog bxBoundsY1 & " , " & bxBoundsX1 & " " & bxBoundsY2 & " , " & bxBoundsX2 as string
				set boxWidth to bxBoundsX2 - bxBoundsX1
				set boxHeight to bxBoundsY2 - bxBoundsY1
				--display dialog "width: " & boxWidth & " height: " & boxHeight & " offset: " & bxOffsetX & "," & bxOffsetY as string
				set beginBoundY to (-bxOffsetY)
				set beginBoundX to (-bxOffsetX)
				set endBoundY to beginBoundY + boxHeight
				set endBoundX to beginBoundX + boxWidth
				set bxNameTxtList to my list_proc(bxNameTxt, ":", "")
				set bxNameTxtName to item -1 of bxNameTxtList as string
				set endboundCompareX to (coerce endBoundX to real)
				set endboundCompareY to (coerce endBoundY to real)
				
				tell application "Adobe Photoshop CS"
					--activate
					open file (bxNameTxt as string) showing dialogs never
					set docRef to current document
					if bxPercentX ≠ 100 or bxPercentY ≠ 100 then
						resize image docRef width (bxPercentX / 100) as percent resample method none
						--resize image docRef width (bxPercentX / 100) as percent height (bxPercentY / 100) as percent resample method none
					end if
					set docWidth to width of docRef as inches
					set docHeight to height of docRef as inches
					if (docWidth as real) > (endboundCompareX as real) then
						--say "hi"
						--display dialog endBoundX
						set endBoundX to endBoundX + fudgeFactor
						--display dialog endBoundX
					end if
					if (docHeight as real) > (endboundCompareY as real) then
						set endBoundY to endBoundY + fudgeFactor
					end if
					crop docRef bounds {beginBoundX as inches, beginBoundY as inches, endBoundX as inches, endBoundY as inches}
					-- width boxWidth as inches height boxHeight as inches
					--resize image docRef width (bxPercent / 100) as percent resample method none
					--close docRef with saving
					set fileSpec to theSaveFol & bxNameTxtName as string
					save docRef in file fileSpec as JPEG with options myOptions
					close current document
					
				end tell
				set filespecnew to text 1 through -4 of fileSpec & "jpg" as string
				set image 1 of picture box bx of page i to (filespecnew as alias)
				set doneCount to doneCount + 1
				
			end if
		end repeat
	end repeat
	
	
end tell
set doneMessage to doneCount & " Images done." as string
try
	say doneMessage
	display dialog doneMessage
on error
	display dialog doneMessage
	
end try

on list_proc(searchList, search_string, replace_string)
	set AppleScript's text item delimiters to the search_string
	set searchListlist to every text item of searchList
	set AppleScript's text item delimiters to replace_string
	set searchList to the searchListlist as string
	set AppleScript's text item delimiters to ""
	
	if replace_string = "" then
		return searchListlist
	else
		return searchList
	end if
	
end list_proc




-- applescript page