Script to send fax automatically


I’m writing an AppleScript to send fax automatically now, but I came across a problem… I let the script to open a text named “Test FAX” (which is TextEdit file) on desktop and select print menu item of File menu. In print dialog, there is “Fax…” button on it. I’d like to click it with AppleScript using System Events call but I found that the button is under “Unknown” parent of “Print” sheet of “TextEdit” application, and I could not find any way to click it via AppleScript… Do you have any idea about this? cannot we control the button via AppleScript? Please help me…

Here is the scirpt I’m now writing.

global gPhoneNum, gFAXCount, gSentCount

tell application “System Events”
my EnableUI()
my OpenDialog()
my OpenFile()

repeat gFAXCount times
	my SendFAX()
end repeat

my quitTextEdit()

end tell

on EnableUI()
log “Checking that Enable Access for assistive devices is enabled”
tell application “System Events”
if not UI elements enabled then
tell application “System Preferences”
set current pane to pane “”
display dialog “UI element scripting is not enabled. Check Enable access for assistive devices”
end tell
end if
end tell
end EnableUI

on OpenDialog()
display dialog “Input the phone number” default answer “”
set gPhoneNum to text returned of the result

-- Set how many times the script shold send fax
display dialog "How many times do you want to send fax?" default answer ""
set gFAXCount to text returned of the result

set gSentCount to 0

end OpenDialog

on OpenFile()
tell application “Finder”
open file “Test FAX” of desktop
end tell
end OpenFile

on SendFAX()
set gSentCount to gSentCount + 1
tell application “System Events”
tell process “TextEdit”
tell menu bar 1
tell menu bar item “File”
tell menu “File”
click menu item “Print…”
end tell
end tell
end tell
end tell

	tell process "TextEdit"
		tell sheet "Print"
			click button 1
		end tell
		tell sheet "Print"
			set value of text field 1 to gPhoneNum
			click button 1
		end tell
	end tell
end tell

end SendFAX

on quitTextEdit()
log “Quitting TextEdit application”
tell application “TextEdit”
delay 5
end tell
end quitTextEdit