Automator action to create form letters from FMP?

Hi people. I haven’t done much with Automator, but I think it’s what I’ll need in this case.

I need to mail a PDF generated from a Filemaker record, and I want to populate the recipient, subject line and body text with data from fields in the record. The resulting mail message should look like this:

Subject: Groucho’s approval form
Recipient: groucho@groucho.org
Body: Hello Groucho, here is your approval form …

Thanks to Nigel G and Martin Michel I now know that Mail PDF is an Automator workflow found in /Library/PDF Services, and extracting the required data from FMP is simple enough, but how do I pass that data from my script to Automator?

Thanks in advance y’all!

If you have the information in FMP then you could use this modifying
to fit your needs. It takes a few seconds for the pdf to show up
depending on how large the pdf.

Regards,

Craig


tell application "FileMaker Pro Advanced"
	tell database "YourDatabase"
		tell current record
			set theRecipient to cell "theRecipient"
			set theBody to cell "theBody" & return & return
			set theSubject to cell "theSubject"
			set thePDF to cell "thePDF"
		end tell
	end tell
end tell

tell application "Mail"
	activate
	set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theBody}
	
	tell theMessage
		make new to recipient at end of to recipients with properties {address:theRecipient}
	end tell
	
	tell content of theMessage
		make new attachment with properties {file name:thePDF} at after the last paragraph
	end tell
end tell

Thanks, but alas, it’s not that simple. The PDF isn’t in a cell of the record - I’m creating the PDF by printing the record, using the built-in ‘Mail PDF’ service.

If all else fails, I guess I can the PDF to a hot folder and make a folder action to handle the address & subject, etc.

You can grab the pdf that you print the same as taking it from
a FMP cell. Just give the path to the pdf instead of getting
it from the cell.

It may be moot at this point as I was able to get the job done via a folder action. But I’ve often found folder actions to be a little flaky. It would be cool to have one button in the print dialog PDF options that invoked the “Mail PDF” workflow, populating the to: and subject fields, etc.

I know how to make AS talk to Mail & FMP, but I still don’t get how to pass those variables to the Mail PDF workflow.

If you post the cell names in your db and the export pdf path
I will fix the script above.

Basically though you are taking the path to the newly created
pdf and inserting that instead of taking if from the cell in the
db. You could also make a new field in the db with the file
path and pull from there as well.

If I am not clear in these instructions you could send me a
clone (no records) of your db and I will make the changes
and send it back.

HTH,

Craig

Well thanks, that’s mighty generous of you!

I might not have mentioned that the reason I want to do it this way instead of with a native FMP script is because … we’re using FMP v. 5.5 … so AS support is minimal. But at least printing with PDF services works.

Here’s the folder action, which works.

on adding folder items to this_folder after receiving these_items
	set this_item to item 1 of these_items
	set theFileName to {}
	set theText to ",

Attached is a sign-off form for your order in PDF format (printable with Adobe Acrobat)."
	
-- the Finder tell block is because I'd like to test that the file is a PDF, but haven't added that code yet
	tell application "Finder"
		

		tell application "FileMaker Pro"
			tell database "Invoices.VMX"
				tell current record
					set theContact to cell "Contact" -- that's a person's first & last names in one field
					set theRecipient to cell "email"
					set theCustomer to cell "Customer Full Address" -- first line is company name
				end tell
			end tell
		end tell
		set theCustomer to (paragraph 1 of theCustomer)
		set theContact to (word 1 of theContact)
		set theBody to "Hi" & space & theContact & theText
		
		--process the filename & remove illegal chars
		repeat with an_item in (every character of theCustomer as list)
			set x to ASCII number of an_item
			if (x ≥ 48 and x ≤ 57) or (x ≥ 65 and x ≤ 90) or (x ≥ 97 and x ≤ 122) then
				set theFileName to theFileName & an_item
			end if
		end repeat
		set theFileName to (theFileName as text) & "_ApprovalForm.pdf"
		
		set theSubject to theCustomer & space & "approval form"

--rename the pdf and reset the variable to the renamed file 
		set name of this_item to theFileName
		set this_item to (file theFileName of this_folder) as alias
		
		tell application "Mail"
			activate
			set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theBody}
			
			tell theMessage
				make new to recipient at end of to recipients with properties {address:theRecipient}

				
				tell content
					make new attachment with properties {file name:this_item} at after the last paragraph
				end tell
			end tell
			
			
		end tell

		
		move this_item to trash
	end tell
end adding folder items to

I have not used FM 5. I started with v3 and then did not come back
to it until v7 and I am now on v9. I am guessing v5 does not have a
Perform AppleScript function.

If this way is working then I would only suggest turning it into a
stay open script instead of a folder action.

Actually, FMP 5.5 does have a “perform AS” function though it strikes me as rather primitive. You have to paste the uncompiled Applescript into a Filemaker script - FMP will compile it every time the script is invoked! It works okay in this case but I wouldn’t want to rely on it for anything heavy.

For the sake of the folks who have to administer them, I trust that newer versions of FMP are less clunky …

What’s the advantage of a stay-open script over a folder action?

I made the experience that Folder Actions sometimes ignore newly added files when they are already processing files. If you are using a stay-open script which watches a folder, you can build a more reliable workflow, as you can monitor, control and process the contents of the folder item by item yourself.

Newer versions of FM do not seem to fix this clunky way of adding AppleScript. One cool thing you can do though is to do a “Calculated AppleScript” instead of the more commonly used “Native AppleScript.”

This option allows you to integrate FM functions into the AppleScript. I use this quite often to insert the values into the script using the “Substitute” function before FM compiles it. This saves making Event calls back to FM to get the information.

I have also found that AppleScripts run from within FM are significantly faster and intermingling them within the FM script allows more use of the native FM scripts which definitely run faster.

Thanks for all the input, people!

Is there any syntax I need to change in order to convert a folder action script to a stay-open ‘watch folder’ script?

Can I save some processor cycles by making the script check the folder once a minute instead of constantly watching?