Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with Mockman's answer chosen by CommunityBot
added 3 characters in body
Source Link
Dycey
  • 13
  • 6
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
    on perform mail action with messages theMessages in mailboxes mbox for rule aRule
        
        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            set thisSubject to (subject of theMessage as string)
            set thisContent to (content of theMessage as string)
            my WriteLog("Recieved email from " & thisSender & ", Subject: " & thisSubject & return)
            my addEmailToDB("mails", "mails", thisSender, "", "", "", thisSubject, thisContent)
        end repeat
        
    end perform mail action with messages
end using terms from

-- the code, from here on, has been tested and runs from Script Debugger or Script Editor
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    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 as «class utf8»
        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

on WriteLog(the_text)
    set this_file to (((path to desktop folder) as text) & "log.txt")
    my write_to_file(the_text, this_file, true)
end WriteLog

on addEmailToDB(theDB, theTable, sender, recipient, cc, bcc, subject, body)
    try
        tell application "FileMaker Pro 18 Advanced"
            activate
            show database theDB
            tell table theTable of database theDB
                set newRec to create new record
                show newRec
                tell newRec
                    set cell "from" to sender
                    set cell "to" to recipient
                    set cell "cc" to cc
                    set cell "bcc" to bcc
                    set cell "subject" to subject
                    set cell "body" to body
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end addEmailToDB
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
    on perform mail action with messages theMessages in mailboxes mbox for rule aRule
        
        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            set thisSubject to (subject of theMessage as string)
            set thisContent to (content of theMessage as string)
            my WriteLog("Recieved email from " & thisSender & ", Subject: " & thisSubject & return)
            addEmailToDB("mails", "mails", thisSender, "", "", "", thisSubject, thisContent)
        end repeat
        
    end perform mail action with messages
end using terms from

-- the code, from here on, has been tested and runs from Script Debugger or Script Editor
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    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 as «class utf8»
        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

on WriteLog(the_text)
    set this_file to (((path to desktop folder) as text) & "log.txt")
    my write_to_file(the_text, this_file, true)
end WriteLog

on addEmailToDB(theDB, theTable, sender, recipient, cc, bcc, subject, body)
    try
        tell application "FileMaker Pro 18 Advanced"
            activate
            show database theDB
            tell table theTable of database theDB
                set newRec to create new record
                show newRec
                tell newRec
                    set cell "from" to sender
                    set cell "to" to recipient
                    set cell "cc" to cc
                    set cell "bcc" to bcc
                    set cell "subject" to subject
                    set cell "body" to body
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end addEmailToDB
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
    on perform mail action with messages theMessages in mailboxes mbox for rule aRule
        
        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            set thisSubject to (subject of theMessage as string)
            set thisContent to (content of theMessage as string)
            my WriteLog("Recieved email from " & thisSender & ", Subject: " & thisSubject & return)
            my addEmailToDB("mails", "mails", thisSender, "", "", "", thisSubject, thisContent)
        end repeat
        
    end perform mail action with messages
end using terms from

-- the code, from here on, has been tested and runs from Script Debugger or Script Editor
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    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 as «class utf8»
        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

on WriteLog(the_text)
    set this_file to (((path to desktop folder) as text) & "log.txt")
    my write_to_file(the_text, this_file, true)
end WriteLog

on addEmailToDB(theDB, theTable, sender, recipient, cc, bcc, subject, body)
    try
        tell application "FileMaker Pro 18 Advanced"
            activate
            show database theDB
            tell table theTable of database theDB
                set newRec to create new record
                show newRec
                tell newRec
                    set cell "from" to sender
                    set cell "to" to recipient
                    set cell "cc" to cc
                    set cell "bcc" to bcc
                    set cell "subject" to subject
                    set cell "body" to body
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end addEmailToDB
Added expander explanation
Source Link
Dycey
  • 13
  • 6

Update:

Here's the full script, including the addEmailToDB() function. It works fine as a standalone, manually run, but doesn't respond when run from Mail.app. The Allow Apple events... extended privilege is enabled for Admin, and the FM file is set to open as Admin, with no password, and is already open before the script runs.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
    on perform mail action with messages theMessages in mailboxes mbox for rule aRule
        
        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            set thisSubject to (subject of theMessage as string)
            set thisContent to (content of theMessage as string)
            my WriteLog("Recieved email from " & thisSender & ", Subject: " & thisSubject & return)
            addEmailToDB("mails", "mails", thisSender, "", "", "", thisSubject, thisContent)
        end repeat
        
    end perform mail action with messages
end using terms from

-- the code, from here on, has been tested and runs from Script Debugger or Script Editor
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    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 as «class utf8»
        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

on WriteLog(the_text)
    set this_file to (((path to desktop folder) as text) & "log.txt")
    my write_to_file(the_text, this_file, true)
end WriteLog

on addEmailToDB(theDB, theTable, sender, recipient, cc, bcc, subject, body)
    try
        tell application "FileMaker Pro 18 Advanced"
            activate
            show database theDB
            tell table theTable of database theDB
                set newRec to create new record
                show newRec
                tell newRec
                    set cell "from" to sender
                    set cell "to" to recipient
                    set cell "cc" to cc
                    set cell "bcc" to bcc
                    set cell "subject" to subject
                    set cell "body" to body
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end addEmailToDB

Update:

Here's the full script, including the addEmailToDB() function. It works fine as a standalone, manually run, but doesn't respond when run from Mail.app. The Allow Apple events... extended privilege is enabled for Admin, and the FM file is set to open as Admin, with no password, and is already open before the script runs.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
    on perform mail action with messages theMessages in mailboxes mbox for rule aRule
        
        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            set thisSubject to (subject of theMessage as string)
            set thisContent to (content of theMessage as string)
            my WriteLog("Recieved email from " & thisSender & ", Subject: " & thisSubject & return)
            addEmailToDB("mails", "mails", thisSender, "", "", "", thisSubject, thisContent)
        end repeat
        
    end perform mail action with messages
end using terms from

-- the code, from here on, has been tested and runs from Script Debugger or Script Editor
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
    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 as «class utf8»
        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

on WriteLog(the_text)
    set this_file to (((path to desktop folder) as text) & "log.txt")
    my write_to_file(the_text, this_file, true)
end WriteLog

on addEmailToDB(theDB, theTable, sender, recipient, cc, bcc, subject, body)
    try
        tell application "FileMaker Pro 18 Advanced"
            activate
            show database theDB
            tell table theTable of database theDB
                set newRec to create new record
                show newRec
                tell newRec
                    set cell "from" to sender
                    set cell "to" to recipient
                    set cell "cc" to cc
                    set cell "bcc" to bcc
                    set cell "subject" to subject
                    set cell "body" to body
                end tell
            end tell
        end tell
        return true
    on error
        return false
    end try
end addEmailToDB
Notice added Authoritative reference needed by Dycey
Bounty Started worth 100 reputation by Dycey
Added update re mystery menu item
Source Link
Dycey
  • 13
  • 6

Update

I noticed a small gear icon appearing in the menu bar when the script was run. Through some manoeuvring I managed to right-click the icon, and capture the resulting menu. I suspect this is some automation indicator ( possibly CoreCervices/ScriptMonitor ?)

ScriptMonitor menu item?

I therefore tried enabling CoreCervices/ScriptMonitor in the FullDisk Access pane of the Security & Privacy's 'Privacy' tab... no joy.

Apart from the tccutil reset, does anyone know if there is a way to trigger a request from Mail.app for AppleEvent access of say the Finder, so that an option appears in the 'Automation' tab of Security & Privacy?


Update

I noticed a small gear icon appearing in the menu bar when the script was run. Through some manoeuvring I managed to right-click the icon, and capture the resulting menu. I suspect this is some automation indicator ( possibly CoreCervices/ScriptMonitor ?)

ScriptMonitor menu item?

I therefore tried enabling CoreCervices/ScriptMonitor in the FullDisk Access pane of the Security & Privacy's 'Privacy' tab... no joy.

Apart from the tccutil reset, does anyone know if there is a way to trigger a request from Mail.app for AppleEvent access of say the Finder, so that an option appears in the 'Automation' tab of Security & Privacy?

Source Link
Dycey
  • 13
  • 6
Loading