Could someone help me identify what I’m doing wrong in this script to transfer emails and attachment info into a filemaker database. It works fine for messages without attachments (which are saved into numbered folders on my Macintosh.) I think I must be overdoing the loops and subroutines somewhere!
tell application “Mail”
---WORK ON MULTIPLE SELECTION---
activate
set theSelection to selection
set messagetotal to count (items of theSelection)
if messagetotal < 1 then
display dialog "One or more messages must be selected." buttons {"OK"} default button 1 with icon caution
return
else
---------------------------------------------------------------------------
--LOOP TO PROCESS MAIL start---
---------------------------------------------------------------------------
repeat with messagecounter from 1 to messagetotal
set themessage to item messagecounter of theSelection
---------------------------------------------------------------------------
--GET INFO FROM SELECTED MESSAGE---
---------------------------------------------------------------------------
set theSendername to extract name from sender of themessage
set theSenderemail to extract address from sender of themessage
set thedate to date received of themessage as string
set theSubject to subject of themessage
set theContent to the content of themessage as string
set theSource to source of themessage
set allattachments to every mail attachment of themessage
set attachtotal to count (items of allattachments)
---------------------------------------------------------------------------
--PUT INFO IN FILEMAKER RECORD start---
---------------------------------------------------------------------------
tell application "FileMaker Pro Advanced"
tell table "MAIL" of database "_IanMailin"
create record
set cell "Subject" of last record to theSubject
set cell "Text" of last record to theContent
set cell "Sendername" of last record to theSendername
set cell "Senderemail" of last record to theSenderemail
set cell "source" of last record to theSource
set cell "Date" of last record to thedate as string
set mymailid to cell "idserial" of last record
end tell
end tell
---------------------------------------------------------------------------------------------------------------
---ROUTINES FOR ATTACHMENTS start---
-------------------------------------------------------------------------------------------------------------
if attachtotal > 0 then
---------------------------------------------------------------------------
---CREATE FILING RECORD IN FMP start---
---------------------------------------------------------------------------
tell application "FileMaker Pro Advanced"
tell database "_IanMailin"
tell table "File"
create record
go to last record
set cell "mailid" of last record to mymailid
set mypath1 to cell "_path1" of last record
set mypath2 to cell "_path2" of last record
set mypath3 to cell "_path3" of last record
set mypath4 to cell "_path4" of last record
set mypath5 to cell "_path5" of last record
set myfolder1 to cell "_folder1" of last record
set myfolder2 to cell "_folder2" of last record
set myfolder3 to cell "_folder3" of last record
set myfolder4 to cell "_folder4" of last record
end tell
end tell
end tell
---/------------------------------------------------------------------------
---CREATE FILING RECORD IN FMP stop---
---/------------------------------------------------------------------------
---------------------------------------------------------------------------
---CREATE FOLDER IN FINDER start---
---------------------------------------------------------------------------
tell application "Finder"
if not (the folder mypath2 exists) then
make new folder at mypath1 with properties {name:myfolder1 as string}
end if
if not (the folder mypath3 exists) then
make new folder at mypath2 with properties {name:myfolder2 as string}
end if
if not (the folder mypath4 exists) then
make new folder at mypath3 with properties {name:myfolder3 as string}
end if
if not (the folder mypath5 exists) then
make new folder at mypath4 with properties {name:myfolder4 as string}
end if
end tell
---/------------------------------------------------------------------------
---CREATE FOLDER IN FINDER stop---
---/------------------------------------------------------------------------
---------------------------------------------------------------------------
---LOOP TO SAVE ATTACHMENTS IN FOLDER start---
---------------------------------------------------------------------------
repeat with attachcounter from 1 to attachtotal
set attch to item attachcounter of allattachments
set attachname to name of attch
set pathname to mypath5 & ":" & attachname
-----------------------------------
tell application "Mail"
save attch in file pathname
end tell
-----------------------------------
end repeat
---/------------------------------------------------------------------------
---LOOP TO SAVE ATTACHMENTS IN FOLDER stop---
---/------------------------------------------------------------------------
end if
--------------------------------------------
---MOVE MESSAGE---
--------------------------------------------
move themessage to mailbox "TRANSFERRED"
end repeat
---/------------------------------------------------------------------------
--LOOP TO PROCESS MAIL stop---
---/------------------------------------------------------------------------
end if
-----------------------------------------------------------------
---ALERT USER---
-----------------------------------------------------------------
display dialog "Finished" giving up after (2)
-----------------------------------------------------------------
---ACTIVATE FILEMAKER---
-----------------------------------------------------------------
tell application "FileMaker Pro Advanced"
activate
end tell
end tell
Thanks in advance for any help you can give!
Model: Macbook Pro
Browser: Firefox 25.0
Operating System: Mac OS X (10.8)