Question regarding System Events within Filemaker

Bear with me on this as I have very little experience of applescript, let alone applescript within the filemaker environment. I have an applescript which essentially saves a pdf of a record ( I’m working with FM6…don’t ask!) and then attaches that pdf to an email. All is well if the applescript is run externally from filemaker but sadly when it is Embedded into FM…nothing.

The part of the code that I think is failing is getting System events to do a bit of UI manipulation, but its probably because I’m asking FM to trigger System Events to control itself, bit like performing DIY brain surgery.

Or it could be my code is all wrong…
this is the element I think is failing in the applescript step…

tell application "Finder"
	set filesavepath to "Macintosh HD:Users:colinpartridge:Desktop:Quotation Emails:"
	set Pfilesavepath to POSIX path of (filesavepath as Unicode text)
set varSubjectValue to "quote"

set mailattachpath to filesavepath & varSubjectValue & ".pdf"
	if exists file mailattachpath then
		--display dialog "It exists."
		delete file mailattachpath -- moves it to the trash
	end if
end tell

tell application "System Events"
	set frontmost of process "FileMaker Pro" to true
	tell process "FileMaker Pro"

		keystroke "p" using {command down}
		repeat until exists window "Print"
		delay 0.2
		end repeat
		#check to see if current record is selected
		if value of radio button "Current record" of window "Print" is 0 then click radio button "Current record" of window "Print"
		click menu button "PDF" of window "Print"
		menu button "PDF" of window "Print"
		click menu item 1 of menu 1 of menu button "PDF" of window "Print"
		repeat until exists window "Save"
			delay 0.2
		end repeat
		keystroke "g" using {command down, shift down}
		set value of text field 1 of sheet 1 of window "Save" to Pfilesavepath
		click button "Go" of sheet 1 of window "Save"
		set value of text field 1 of window "Save" to varSubjectValue & ".pdf"
		set value of text field 3 of group 1 of window "Save" to varSubjectValue
		delay 0.2
		keystroke return
	end tell
end tell

Thanks for looking at this, any tips and pointers gratefully received.



Ok, I now realise that this script is not going to work directly within filemaker. I thought that I would try an indirect route, by triggering the main script from a simpler script, such as

set myScript to alias ((path to scripts folder as text) & "myscript")
run script myScript

This of course runs perfectly well from Applescript editor but when the script is run from Filemaker, it just hangs.
any ideas gratefully received.