Hi,
I have the rough start of a script which takes the selected mail messages, gets data from mail on sender, subj, date, content, and then puts that data into a sqlite database and moves the selected message to the trash. The great advantage to doing this is that the script doesn’t need to open any database like filemaker, etc. The disadvantage is that you have to use shell to send the data to sqlite3. Therein is the problem. The script seems to work fine for TEXT emails, but to get the unix shell command correct, I have to search by char for any single quotes and turn them into single curly-quotes, to avoid an erroneous shell call. What seems to be the problem is HTML emails which for some reason confuses the “single quote replacement” feature, I believe.
So, does anyone have any clues as to why html email isn’t being parsed correctly?
Any better suggestions for the single quote replacement?
Here’s the script:
-- parts of this use Apple's script provided in Script menu for Mail...small parts only
set desktopPath to path to desktop
set dbPath to desktopPath & "Mail_Archive.db" as string
set fixedtext to ""
tell application "Mail"
set theSelectedMessages to selection -- from Apple
if (count of theSelectedMessages) is equal to 0 then -- from Apple
display dialog "Please select a message in Mail first, then run this script again." -- from Apple
else -- from Apple
repeat with x from 1 to (count of theSelectedMessages) -- from Apple
set theMessage to item x of theSelectedMessages -- from Apple
set theSender to sender of theMessage
set theSubject to subject of theMessage
set theContent to content of theMessage
set theDateSent to date sent of theMessage
move theMessage to mailbox "Trash"
repeat with x from 1 to (count theContent) -- this repeat screens out single quotes which would mess up the shell command syntax
if character x of theContent = "'" then
set fixedtext to fixedtext & "'" as string -- inserting the curly quote here
else
try
set fixedtext to fixedtext & character x of theContent as string -- this is choking with html mails...
on error errnum
-- do nothing
-- I added this to see if it would allow it to work with whatever was choking the script...it didn't help
end try
end if
end repeat
-- code to add to SQLite database
if not my fileExists(dbPath) then
set dbPath to POSIX path of file dbPath
set d to space & dbPath & space
set s to "sqlite3" & d & quote
set s to s & "create table Mail_Archive(sender,subject,date,content);"
set s to s & "insert into Mail_Archive values('" & theSender & "','" & theSubject & "','" & theDateSent & "','" & fixedtext & "');"
set s to s & quote
do shell script s
else
set dbPath to POSIX path of file dbPath
set d to space & quote & dbPath & quote & space
set s to "sqlite3" & d & quote
set s to s & "insert into Mail_Archive values('" & theSender & "','" & theSubject & "','" & theDateSent & "','" & fixedtext & "');"
set s to s & quote
do shell script s
end if
end repeat
end if
end tell
on fileExists(thepath)
try
alias thepath
return true
on error
return false
end try
end fileExists
TIA
Vince