#!/usr/bin/python
import os,sys,tempfile,re
from AppKit import *
from Foundation import *

rr=64
ruleT = re.compile(r"(\s*\/ArtBox\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 = ruleT.sub(" ",result2)
		try:
			resultFile=open(outname,"w")
			resultFile.write(content)
			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)
