#!/usr/bin/python
#
#   clipPDF version 1.2
#
#   This Python/pyOjC script removes /ArtBox from a PDF file, and 
#   sets the /MediaBox equal to the /CropBox if the latter is specified in the file.
#
import os,sys,tempfile,re
from AppKit import *
from Foundation import *

rr=64
ruleA = re.compile(r"(\s*\/ArtBox\s*\[.*?\]\s*)", re.DOTALL)
ruleM = re.compile(r"(\s*\/MediaBox\s*\[.*?\]\s*)", re.DOTALL)
ruleC = re.compile(r"(\s*\/CropBox\s*\[(.*?)\]\s*)", re.DOTALL)
board=NSPasteboard.generalPasteboard()
result = board.dataForType_(NSPDFPboardType)
if result:
	tmpdir = tempfile.mkdtemp("","clipPDF","/tmp")
	inname=tmpdir+"/infile.pdf"
	outname=tmpdir+"/outfile.pdf"
	result.writeToFile_atomically_(inname,1)
	#
	#   Writing to a file and re-opening seems to be the easiest
	#   way to avoid type conversion problems for the PDF binary file.
	#   If I wanted to avoid this, I'd have to additionally
	#   import PyObjCTools.Conversion, so the net timing is equal.
	#
	try:
		resultFile=open(inname,"r")
		result2 = resultFile.read()
		resultFile.close()
	except:
		rr=74
	else:
		content = ruleA.sub(" ",result2)
		cropbox = ruleC.search(content)
		if cropbox:
			resizedContent = ruleM.sub(" /MediaBox ["+cropbox.group(2)+"] ", content)
		try:
			resultFile=open(outname,"w")
			resultFile.write(resizedContent)
			resultFile.close()
		except:
			rr=74
		else:
			content=NSData.dataWithContentsOfFile_(outname)
			board.declareTypes_owner_([NSPDFPboardType], None)
			board.setData_forType_(content, NSPDFPboardType)
			os.remove(outname)
			rr=0
	os.remove(inname)
	os.rmdir(tmpdir)
sys.exit(rr)
