A weird one

I am trying to get FileMaker to extract the content of the inbox in mail. The code works on my computer but on my clients it has an issue. The code sets cell “g_Email_Inbox” and when I query that cell from applescript it returns its contents but within FileMaker on my client’s machine NaDa. It displays as empty and any scripts within filemaker see the field as empty.

So here is the code

tell application "System Events"
	set userName to the name of current user
	set startDisk to the name of the startup disk
	set theProcesses to the name of every process
	set mailOpen to theProcesses contains "Mail"
end tell

tell application "Finder"
	if mailOpen is false then
		tell application "Mail"
			try
				open
			end try
		end tell
		delay 2
	end if
	
	set the email_log to startDisk & ":Users:" & userName & ":Library:Application Support:Clean Sweep:Email Account Log.txt"
	
	
	try
		open for access file the email_log
		try
			set theEmails to read file the email_log
			close access file the email_log
		on error
			close access file the email_log
			set theEmails to "Null"
		end try
	on error
		close access file the email_log
		display dialog "There was a problem with this process. We have attemted to fix it.  Try running the script again. If the error persists, quit the application and reopen.  Then run the script again."
		return -48
	end try
end tell

set theMailBoxes to theEmails

if theMailBoxes is "Null" then
	tell application "Mail"
		set theAccounts to name of every account as list
	end tell
	set theChosenMailBoxes to choose from list theAccounts with prompt "Choose the mailboxes to include when importing messages" with multiple selections allowed
	set theMailBoxes to theChosenMailBoxes as list
	my write_email_log(theMailBoxes, email_log)
else
	
	set AppleScript's text item delimiters to "
"
	set theMailBoxes to the first text item of theMailBoxes
	set AppleScript's text item delimiters to ""
end if

set AppleScript's text item delimiters to " , "
set the theAccounts to every text item of the theMailBoxes
set AppleScript's text item delimiters to ""

--return theAccounts

tell application "Mail"
	set theMessageList to "<Start>"
	repeat with i from 1 to (count of theAccounts)
		set theAccount to account (get item i of theAccounts)
		set theAccountDir to account directory of theAccount as alias
		set theAccountInbox to mailbox "INBOX" of theAccount
		set theMessages to (every message of theAccountInbox whose read status is false)
		
		repeat with ii from 1 to (count of theMessages)
			set theMessage to item ii of theMessages
			
			set theMessageHeader to "<message " & id of theMessage & ">"
			set theMessageFrom to "<from>" & reply to of theMessage & "</from>"
			set theMessageDate to "<date>" & date received of theMessage & "</date>"
			set theMessageSubject to "<subject>" & subject of theMessage & "</subject>"
			set theMessageContent to "<content>" & content of theMessage & "</content>"
			set theMessageFooter to "</message " & id of theMessage & ">"
			set theMessageCode to theMessageHeader & theMessageFrom & theMessageDate & theMessageSubject & theMessageContent & theMessageFooter
			set theMessageList to theMessageList & theMessageCode
			set the read status of theMessage to true
			
		end repeat
	end repeat
	set theMessageList to theMessageList & "</End>"
end tell

if the length of theMessageList is greater than 13 then
	tell application "FileMaker Pro Advanced"
		tell record 1 of table "Query" of database "Clean Sweep.fmp12"
			set cell "g_Email_Inbox" to cell "g_Email_Inbox" & theMessageList
		end tell
	end tell
end if

tell application "Mail"
	if mailOpen is false then
		quit application "Mail"
	end if
end tell

on write_email_log(emails, email_log)
	tell application "Finder"
		set theEmails to ""
		repeat with i from 1 to (count of the emails)
			if i is the (count of emails) then
				set theEmails to theEmails & item i of emails
			else
				set theEmails to theEmails & item i of emails & " , "
			end if
		end repeat
		
		
		try
			open for access file the email_log with write permission
			write (theEmails & return) to file the email_log
			close access file the email_log
		on error
			close access file the email_log
		end try
	end tell
	
end write_email_log

What might be causing this issue?

Both modern machines (less than one year old) Mine runs FileMaker 14 and Client is running 13 - will confirm that is not the issue. Thoughts?!?

Hi,

probably not a solution, but some code optimization.
You can replace


tell application "System Events"
	set userName to the name of current user
	set startDisk to the name of the startup disk
	set theProcesses to the name of every process
	set mailOpen to theProcesses contains "Mail"
end tell

tell application "Finder"
	if mailOpen is false then
		tell application "Mail"
			try
				open
			end try
		end tell
		delay 2
	end if
	
	set the email_log to startDisk & ":Users:" & userName & ":Library:Application Support:Clean Sweep:Email Account Log.txt"
	try
		open for access file the email_log
		try
			set theEmails to read file the email_log
			close access file the email_log
		on error
			close access file the email_log
			set theEmails to "Null"
		end try
	on error
		close access file the email_log
		display dialog "There was a problem with this process. We have attemted to fix it. Try running the script again. If the error persists, quit the application and reopen. Then run the script again."
		return -48
	end try
end tell

with


set mailOpen to application "Mail" is running
if mailOpen is false then launch application "Mail"
set the email_log to (path to application support folder from user domain as text) & "Clean Sweep:Email Account Log.txt"
try
	set theEmails to read file email_log
on error
	set theEmails to "Null"
end try


¢ there is a smarter way to check if an application is running
¢ the proper command to launch an application is launch not open
¢ using a relative path (path to . ) avoids retrieving the current user name and startup disk
¢ Neither the Finder nor open for access is needed to read a file.
¢ the display dialog line is not needed, too.

at the end the portion to quit Mail can be simplified with


if mailOpen is false then quit application "Mail"

the write log routine also does not require the Finder
I added a time stamp and a more reliable writing code which appends new text to the file


on write_email_log(emails, email_log)
	tell (current date) to set timestamp to short date string & space & time string & ": "
	set {TID, text item delimiters} to {text item delimiters, " , "}
	set theEmails to emails as text
	set text item delimiters to TID
	
	try
		set fileDescriptor to open for access file email_log with write permission
		write (timestamp & theEmails & return) to fileDescriptor starting at eof
		close access fileDescriptor
	on error
		try
			close access file email_log
		end try
	end try
end write_email_log

Hello.

In addition to all that Stefan has provided, I’d also have the users run a standalone script, that check that the logfile exists, and that it has the mail address within it.

What I really don’t understand, is why there isn’t any global field with that mailaddress in the database, or in another Filemaker database, that is created upon installation.

Either:
Tell layout “xpto” instead of table “Query”
or use:
contents of cell …
or:
show layout “xpto” before using get/set cell of table.

Where layout “xpto” contains your field “g_Email_Inbox”

So Guess what …

Reboot client computer … fixed!!! Plus the refinements from Stefan. Oh and Stefan, I’d like to ask a follow up off forum via email. If your willing pls email me at support@humanedevelopment.com

Thanx all
M