NEWBIE: Get Data from Appleworks

My wife has a little Appleworks database of all of her teacher colleagues (200). At times, she needs to email them memos, forms, etc. Not knowing AS at all, I’ve been reserching for about a week and I’m real close to getting a script to send each person an email with a user-defined attachment.

I’m looping through the records and can get the email address field, however; I can’t seem to transfer that email variable into the Mail recipients property value.

Any pointers?

Thanks, Cliff

{------- script below -------------}

– LOOP TEST

– Load Libraries at compile time.
property librariesPath : “Macintosh HD:Applications:AppleWorks 6:AppleWorks Essentials:Scripts Support:Libraries:”
property awLib : load script alias (librariesPath & “AppleWorks.lib”)

tell application “AppleWorks 6”
set database1 to a reference to database of document 1
set checkField to “Email”

show every record in database1

set visibleIDsDB1 to id of every record in every record in database1
repeat with idNDB1 from 1 to length of visibleIDsDB1
	set recordDB1 to (a reference to record id (item idNDB1 in visibleIDsDB1) in database1)
	
	--Next Value is the email address
	--(value of field checkField in recordDB1)
	
	tell application "Mail"
		activate
		set this_message to make new outgoing message at beginning of every outgoing message
		tell this_message

                                 --It breaks on the temp1 assignment below
                                --
			set temp1 to its value of checkField in recordDB1
			--set temp2 to display dialog "Enter the file to attach:" default answer ""
			
			
			make new to recipient at beginning of to recipients ¬
				with properties {address:temp1}
			set the subject to "TEST MESSAGE"
			set the content to "Hello,  Attached is your form"
			make new attachment with properties {file name:alias temp2} at before the first character
		end tell
		
		
		set the content of this_message to the content of this_message
		make new OLD message editor at beginning of every OLD message editor
		set the outgoing message of OLD message editor 1 to this_message
		send this_message
	end tell
	
end repeat

end tell


Update: I figured out my problem was the scope of the variable. Like a lot of languages, the error message was not very descriptive so it took me a while. Attached below is the final script which loops through every record of the current appleworks result set and sends an individual email. it prompts for an attachment and creates a logfile on the desktop which contains the first name, last name and email address that was sent.

Requirements: OS X, Appleworks db with 3 fields of "First Name, “Last Name” and "Email.

Cliff
{-----------------------------------------}


-- LOOP TEST
--
-- Load Libraries at compile time.
property librariesPath : "Macintosh HD:Applications:AppleWorks 6:AppleWorks Essentials:Scripts Support:Libraries:"
property awLib : load script alias (librariesPath & "AppleWorks.lib")


global theButtonPressed


on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

------------------------------------------------

--Assign record file
set report_file to (((path to desktop folder) as text) & "EMAIL_RECORD")


---Get file for attachment
set displayString to "Do you want to attach a file?"
set answer to display dialog displayString buttons {"Yes", "No"}
set theButtonPressed to button returned of answer

if theButtonPressed = "Yes" then
	set pathToMe to (choose file with prompt "Display path of:")
end if


--- Get Email subject line
set temp to display dialog "Enter the Subject" default answer "" buttons {"OK"}
set subjectText to text returned of temp
--display dialog subjectText

--Get records from current Appleworks database
tell application "AppleWorks 6"
	set database1 to a reference to database of document 1
	set report_text to ""
	
	--Assign field names for email and report output
	set emailField to "Email"
	set firstnameField to "First Name"
	set lastnameField to "Last Name"
	
	--Write Header report record
	set report_text to firstnameField & "," & lastnameField & "," & emailField & return & return
	
	
	show every record in database1
	
	set visibleIDsDB1 to id of every record in every record in database1
	
	repeat with idNDB1 from 1 to length of visibleIDsDB1
		set recordDB1 to (a reference to record id (item idNDB1 in visibleIDsDB1) in database1)
		
		--Get Field Data		
		set firstnamevalue to value of field firstnameField of recordDB1
		set lastnamevalue to value of field lastnameField of recordDB1
		set emailvalue to value of field emailField of recordDB1
		
		
		--Do Mail app stuff
		-------------------------------------	
		tell application "Mail"
			activate
			set this_message to make new outgoing message at beginning of every outgoing message
			tell this_message
				make new to recipient at beginning of to recipients ¬
					with properties {address:emailvalue}
				set the subject to subjectText
				set the content to "Please review the attached documents."
				if theButtonPressed = "Yes" then
					make new attachment with properties {file name:pathToMe} at before the first character
				end if
			end tell
			
			
			set the content of this_message to the content of this_message
			make new OLD message editor at beginning of every OLD message editor
			set the outgoing message of OLD message editor 1 to this_message
			send this_message
		end tell
		-------------------------------------
		
		
		set report_text to report_text & firstnamevalue & "," & lastnamevalue & "," & emailvalue & "," & return
		
	end repeat
	
	my write_to_file(report_text, report_file, true)
	
end tell

-- Quit Mail
tell application "Mail"
	quit
end tell
set displayString to "All Done.  " & length of visibleIDsDB1 & " Emails sent."
display dialog displayString buttons {"OK"}