Save as PDF from Print Dialog with filename dialogue

I’m trying to write a script – or Automator action that calls a script – that I can save into ~Library/PDF Services/ and run from the Print dialogue box in pretty much any application (by selecting it from the PDF dropdown in that dialogue).

The script should:

  1. Prompt me for a filename, inserting as the default the text that I have selected in the file (or, alternatively, text that I have copied from the file and which is already in the clipboard).

  2. Save the file, with the filename, to a specific, pre-determined folder (the same folder every time), without asking me about it.

  3. If possible, I’d like the script to then take the PDF file I just created and saved and attach it to a new email message in my default email application (which is Postbox). This part of the script, I think I can figure out, by using variables from above instead of 'aAttachmentPath" in the script below:

tell application "Postbox"
	send message subject "Invoice" body "Attached please find a new invoice from me." attachment aAttachmentPath with args
	activate
end tell

(If you wonder what I’m doing - I’m creating an invoice, saving as a PDF with the invoice number to the invoices folder on my computer, and then emailing the invoice to a client)

I assume that this script may help.

# Build the name of the PDF to fit your needs
set thePDFname to "beurk.pdf"

tell application "System Events"
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is "AppleScript Editor" then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
			delay 0.2
		end repeat
		tell (first process where it is frontmost)
			set theProcess to its name
			set theApp to its file
		end tell
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		tell frontmostProcess
			set theProcess to its name
			set theApp to its file
		end tell
	end if
	set theApp to name of theApp
end tell

activate application theApp
tell application "System Events" to tell process theProcess
	set nbw to count windows
	keystroke "p" using command down
	(*
Wait for the availability of the sheet *)
	repeat
		if exists sheet 1 of window 1 then exit repeat
	end repeat
	--name of menu buttons of sheet 1 of window 1
	--> {"PDF"} # but I don't know if it's spelled this way worldwide
	tell sheet 1 of window 1
		set PDFButton to first menu button
		click PDFButton
		-- name of menu items of menu 1 of PDFButton
		--> {"Ouvrir le PDF dans Aperçu", "Enregistrer au format PDF.", "Enregistrer au format PostScript.", "Faxer le document PDF.", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Add PDF to iTunes", "Envoyer le document PDF par courrier électronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menu."}
		
		click menu item 2 of menu 1 of PDFButton
		(*
Wait for the availability of the Print sheet *)
		repeat
			if exists sheet 1 then exit repeat
		end repeat
		(*
Set the name of the new PDF *)
		tell sheet 1
			# Set the Desktop as destination folder
			keystroke "d" using {command down}
			set value of text field 1 to "beurk.pdf"
			-- name of buttons
			--> {missing value, missing value, missing value, "Enregistrer", "Nouveau dossier", "Annuler"}
			--click button -3 -- ditto keystroke return
			keystroke return
		end tell # sheet 1 (the Save As one)
	end tell # sheet 1 of window 1
end tell

# Now the pdf is available on the Desktop, move it where you want.

Yvan KOENIG (VALLAURIS, France) mardi 8 juillet 2014 21:44:07

hi and welcome to the forum

heres’ some code that does most of what you want to do.

http://hints.macworld.com/article.php?story=2004122222145585

taken from that thread I adapted some code to take the contents of the clipboard and use that as the title
you will need to export your script as an application, and put it in the pdf services folder.

on open these_items
	try
		set this_file to item 1 of these_items
		tell application "Finder"
			set ClipBoardContents to the clipboard
			--set the file_name to the name of this_file
			set the parent_folder to (the container of this_file) as alias
		end tell
		tell application (path to frontmost application as string)
			repeat
				--display dialog "Enter a name for file:" default answer file_name
				display dialog "Enter a name for file:" default answer ClipBoardContents
				set this_name to the text returned of the result
				if this_name is not "" then exit repeat
			end repeat
		end tell
		tell application "Finder"
			set the name of this_file to this_name
			move this_file to "Macintosh HD:Users:Budgie:Desktop:FINAL:" replacing yes
		end tell
	end try
end open