Sending pdfs from Multiad, indesign to clients

Requires Indesign CS2, Multiad creator, acrobat 7, distiller 7, entourage 2004
If you dont have these, it won’t compile.
You need to fill in recipients for it and understand how it works before it will work.

(* PDF Proof - written 15/1/2006 - revised 15/7/2006

Purpose - Sends low resolution PDF of an advertisment to an email address or location on a server for another person to view and approve.

--Requires Indesign CS2, Multiad creator, acrobat 7, distiller 7, entourage 2004

*)

--set all global variables and define lists here
global PDFAds
global adsEmailed
global docName
global NewDocName
global PDFSaved
global recipientAddress
global sendPDF
global EPSSaved
global InDesignActive
global MultiAdActive
global OneOfMyApps
set sendPDF to false
set InDesignActive to false
set MultiAdActive to false
set OneOfMyApps to false
set RecipientListMain to {"Dude1", "Dude2", "Email unlisted..."} --define main list
set PDFAds to path to desktop as string --PDF ads folder where proofs are exported to
set adsEmailed to alias "" --location where PDF is moved to when finished with
set CRMNameList to {} --defines list of names of recipients for sending PDF proof to via entourage mail
set CRMAddressesList to {} -- defines list of corrisponding email addresses for CRMs listed in "CRMNameList" in the same numerical order
set SubsidsNameList to {} --defines list of names of recipients to send proofs via folder system & email
set subsidsAddressesList to {} --defines list of corrasponding addresses for "SubsidsList", both email and server locations, in the same numerical order

tell application "System Events"
	--Establishes if InDesign is the front application
	if exists application process "Adobe InDesign CS2" then
		tell application process "Adobe InDesign CS2"
			if frontmost then
				set InDesignActive to true
				set OneOfMyApps to true
			end if
		end tell
	end if
	--Establishes if MultiAd Creator is the front application
	if exists application process "MultiAd Creator Pro" then
		tell application process "MultiAd Creator Pro"
			if frontmost then
				set MultiAdActive to true
				set OneOfMyApps to true
			end if
		end tell
	end if
end tell





if OneOfMyApps = true then
	if InDesignActive = true then
		my getDocNameInDesign()
	else if MultiAdActive = true then
		my getDocNameMultiAd()
	end if
	my CutNameTo27Chars()
	--asks the user to choose from a list of recipient groups
	tell current application
		set chooseFromRecipientListMain to choose from list RecipientListMain with prompt "Please choose a recipient group" & return & "to send Proof." OK button name {"Send"}
		if (chooseFromRecipientListMain as string) = "false" then
			--do nothing
		else if chooseFromRecipientListMain is {"Dude1"} then
			set chooseFromCRMNameList to choose from list CRMNameList with prompt "Please choose a Dude" & return & "to send Proof." with multiple selections allowed
			if (chooseFromCRMNameList as string) = "false" then
				--do nothing
			else
				if MultiAdActive = true then
					my exportPDFMultiAd()
				else if InDesignActive = true then
					my exportPDFInDesign()
				end if
				my ApprovePDF()
				if sendPDF = true then
					repeat with CurrentCRM in chooseFromCRMNameList
						repeat with i from 1 to count CRMNameList
							if item i of CRMNameList = contents of CurrentCRM then
								set recipientAddress to item i of CRMAddressesList
								my ThenSendItViaEmail()
								exit repeat
							end if
						end repeat
					end repeat
				end if
				if MultiAdActive = true then
					my MultiAdPrintOrNot()
				else if InDesignActive = true then
					my IndesignPrintOrNot()
				end if
			end if
			
		else if chooseFromSubsidsNameList is {"Dude2"} then
			set recipientAddress to item 2 of subsidsAddressesList as alias
			if InDesignActive = true then
				my exportPDFInDesign()
			else if MultiAdActive = true then
				my exportPDFMultiAd()
			end if
			my ApprovePDF()
			if sendPDF = true then
				my ThenSendItViaSpool()
			end if
			if MultiAdActive = true then
				my MultiAdPrintOrNot()
			else if InDesignActive = true then
				my IndesignPrintOrNot()
			end if
			
		else if chooseFromRecipientListMain is {"Email unlisted..."} then
			my EmailUnknownPleaseEnter()
			if recipientAddress ≠ "" then
				if InDesignActive = true then
					my exportPDFInDesign()
				else if InDesignActive = true then
					my exportPDFMultiAd()
				end if
				my ApprovePDF()
				if sendPDF = true then
					my ThenSendItViaEmail()
				end if
				if MultiAdActive = true then
					my MultiAdPrintOrNot()
				else if InDesignActive = true then
					my IndesignPrintOrNot()
				end if
			end if
		end if
	end tell
end if

--Begin SubRoutines definitions

--get name of front document of MultiAd
on getDocNameMultiAd()
	tell application "MultiAd Creator Pro"
		set docName to name of front document as string
		if docName contains ".crtr" then
			set aCount to count every character of docName
			set aCount to aCount - 5
			set docName to characters 1 thru aCount of docName as string
		end if
	end tell
end getDocNameMultiAd


--get name of front document of InDesign
on getDocNameInDesign()
	tell application "Adobe InDesign CS2"
		set docName to name of front document as string
		if docName contains ".indd" then
			set aCount to count every character of docName
			set aCount to aCount - 5
			set docName to characters 1 thru aCount of docName as string
		end if
	end tell
end getDocNameInDesign


--samples the name of the document to use on the PDF, cuts the name down to 27 characters due to PDFs not being able to support as many characters in their filenames
on CutNameTo27Chars()
	set charCount to count (every character in docName)
	if charCount > 27 then
		set NewDocName to characters 1 through 27 of docName as string
	else
		set NewDocName to docName
	end if
end CutNameTo27Chars


--Exports a PDF document from MultiAd Creator
on exportPDFMultiAd()
	tell application "MultiAd Creator Pro"
		set PDFSaved to PDFAds & NewDocName & ".pdf" as string
		export PDF document 1 saving in PDFSaved export method composite resolution limiting 72 ¬
			black and white compression use zip color image compression use JPEG quality 100 ¬
			with embed, use doc size, binary encoding and compress text and line art without print as spreads, crop marks, registration marks, color bars, document notes, plate information, text blocks only and presentation mode
	end tell
	set PDFSaved to PDFSaved as alias
end exportPDFMultiAd


--Exports a PDF document from Adobe InDesign
on exportPDFInDesign()
	tell application "Adobe InDesign CS2"
		set PDFSaved to PDFAds & NewDocName & ".pdf" as string
		export document 1 format PDF type to PDFSaved using PDF export preset "[Smallest File Size]" without showing options
	end tell
end exportPDFInDesign


on exportHiResPDFInDesign()
	tell application "Adobe InDesign CS2"
		set PDFSaved to PDFAds & NewDocName & ".pdf" as string
		export document 1 format PDF type to PDFSaved using PDF export preset "Newsprinter100#" without showing options
	end tell
end exportHiResPDFInDesign


--Exports EPS file from MultiAd Creator
on ExportEPS()
	tell application "MultiAd Creator Pro"
		set EPSSaved to PDFAds & NewDocName & ".eps" as string
		export EPS spread 1 of document 1 saving in EPSSaved flatness limiting 3.0 font inclusion all fonts preview options PC color export method composite scaling by 100.0 bleed amount 0.0 with replacing and binary encoding without level 2 PostScript, TIFF OPI Comments, OPI Omit TIFF, OPI Omit EPS and include Type 1 fonts with custom encoding
	end tell
end ExportEPS


--Distills a PostScript file to a PDF, deletes the PostScript file
on distillThis()
	set EPSSavedasPOSIX to the POSIX path of EPSSaved
	set theSettingsPath to alias "PrePress2:JakeAppleScripts:Newsprinter100#.joboptions"
	set theSettingsPathasPOSIX to the POSIX path of theSettingsPath
	set PDFsAsPOSIX to the POSIX path of PDFAds
	with timeout of 60 seconds
		tell application "Acrobat Distiller 7.0"
			Distill sourcePath EPSSavedasPOSIX adobePDFSettingsPath theSettingsPathasPOSIX destinationPath PDFsAsPOSIX
		end tell
		try
			tell application "System Events"
				set visible of application process "Acrobat Distiller 7.0" to false
			end tell
		end try
	end timeout
	set tempName to NewDocName & ".pdf"
	repeat until exists
		file tempName of alias PDFAds
	end repeat
	set PDFSaved to PDFs & NewDocName & ".pdf" as alias
	tell application "Finder"
		delete file EPSSaved
	end tell
end distillThis


--asks user to approve PDF. on no, deletes PDF, on yes, returns true
on ApprovePDF()
	tell application "Adobe Acrobat 7.0 Professional"
		activate
		open PDFSaved
		delay 3
		with timeout of 1000 seconds
			set anyGood to display dialog "PDF OK?" buttons {"Closer Look", "No", "Yes"} default button "Yes" with icon stop
		end timeout
		if button returned of anyGood is "Closer Look" then
			delay 12
			with timeout of 1000 seconds
				set nowAnyGood to display dialog "PDF OK?" buttons {"No", "Yes"} default button "Yes" with icon stop
			end timeout
			if button returned of nowAnyGood is "Yes" then
				close document 1
				set sendPDF to true
			else if button returned of nowAnyGood is "No" then
				close document 1
				tell application "Finder"
					delete file PDFSaved
				end tell
			end if
		else if button returned of anyGood is "Yes" then
			close document 1
			set sendPDF to true
		else if button returned of anyGood is "No" then
			close document 1
			tell application "Finder"
				delete file PDFSaved
			end tell
		end if
	end tell
end ApprovePDF


--sends PDF to predefined recipient via entourage
on ThenSendItViaEmail()
	tell application "Microsoft Entourage"
		set thisMail to (make new outgoing message with properties {attachment:PDFSaved, recipient:recipientAddress, subject:NewDocName & " Proof", content:""})
		send thisMail
	end tell
	tell application "Finder"
		move PDFSaved to adsEmailed with replacing
	end tell
end ThenSendItViaEmail


--moves PDF to the spool to the predefined folder
on ThenSendItViaSpool()
	tell application "Finder"
		duplicate PDFSaved to recipientAddress with replacing
		move PDFSaved to adsEmailed with replacing
	end tell
end ThenSendItViaSpool


--asks the user whether or not to print the MultiAd file
on MultiAdPrintOrNot()
	tell application "MultiAd Creator Pro"
		activate
		set PrintYesNo to display dialog "Print Ad?" buttons {"No", "Yes"} default button "Yes" with icon caution giving up after 8
		if button returned of PrintYesNo is "Yes" then
			print front document without interaction
		end if
	end tell
end MultiAdPrintOrNot


on IndesignPrintOrNot()
	tell application "Adobe InDesign CS2"
		activate
		set PrintYesNo to display dialog "Print Ad?" buttons {"No", "Yes"} default button "Yes" with icon caution giving up after 8
		if button returned of PrintYesNo is "Yes" then
			print front document without print dialog
		end if
	end tell
end IndesignPrintOrNot


--asks the user to enter an email address to use as the sending address
on EmailUnknownPleaseEnter()
	tell application "MultiAd Creator Pro"
		with timeout of 1000 seconds
			set sendThatMsg to display dialog "Please enter an email address for the recipient." default answer ""
			set recipientAddress to text returned of sendThatMsg
		end timeout
	end tell
end EmailUnknownPleaseEnter