1

I'm trying to get a simple AppleScript to run from a rule in Mail.app under Catalina. The script is very basic:

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

using terms from application "Mail"
    on perform mail action with messages messageList in mailboxes mbox for rule aRule

        repeat with theMessage in theMessages
            set thisSender to (sender of theMessage as string)
            my WriteLog("Recieved email from " & thisSender)
        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

I have added the following rule to Mail.app:

Mail.app rule dialog

I have also enabled full disk access for Mail.app:

enter image description here

The rule runs - the messages are moved to the filing folder - but the log file is not updated.

What else should I be looking at, to try to solve this issue?


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:

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)
            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
  • Look under Files and Folders too and see if anything stands out not checked that should be. Also, it may be a sandboxing issue where it can't write to the Desktop when called from Mail so try e.g. set this_file to ((path to downloads folder as text) & "log.txt") as the Downloads folder should be allowed. – user3439894 Mar 4 at 18:51
  • No, sorry both of those suggestions had no (obvious) effect. :-( Thanks though – Dycey Mar 4 at 20:54
  • To be clear, yes, I checked them, and gave Mail.app Full Disk Access, and I tried your suggestion for saving to downloads instead. – Dycey Mar 4 at 21:06
1
+100

Change 'messageList' to 'theMessages' and try it again:

on perform mail action with messages theMessages in mailboxes mbox for rule theRule

Two small points on the WriteLog line… you have a typo in your 'Recieved email from'. Also, purely as a preference, I found it easier to notice the changes in the log file when I added '& return' to your log text, as in:

my WriteLog("Received email from " & thisSender & return)
| improve this answer | |
  • This certainly fixes this issue with writing the log. However, calling a 3rd party app (e.g. FileMaker) still seems blocked. Thanks anyway – Dycey Mar 11 at 12:30
  • 1
    My pleasure. Based on the original question, I'm not really sure what you mean by 3rd party apps being blocked. Can you elaborate? – Mockman Mar 11 at 12:49
  • Sure, no problem. Instead of just writing to a log file, I wanted to create a record in FileMaker. I have a script for doing that, which works when called manually, but fails when being called by mail.app. I'll attach the full script if you're interested, but honestly, I feel you've earned the bounty ;-) – Dycey Mar 12 at 10:54
  • I can't say for sure but I'd bet not being able to call a third party app is a TCC problem. Apps need permission to send apple events other apps in Mojave and above—this is separate from Full Disk Access, and it's controlled via the Automation tab in Security & Privacy. I guess Mail doesn't display the prompts when it runs Applescripts via rules. I absolutely loath this system and it's one of the reasons I sayed on High Sierra for so long. It's implemented terribly and it cripples Applescript. – Wowfunhappy Mar 12 at 16:11
  • Thanks. It was your original question, after all. Regarding your filemaker issue… have you tried saving the script as an app and then giving that 'script app' the necessary permissions? NB you would likely want to first place it in Mail's script folder (or wherever you have the existing script). I don't think you can move (or edit) it and have it retain its special permissions. I'm running Sierra so maybe nothing happens but it's what I did to get accessibility control over some System Preferences changes. Mail isn't the app that needs permissions here. – Mockman Mar 12 at 16:37
1

Edit: Looks like I spoke too soon with the comment below. I was able to get your script to run in Snow Leopard and to write records to an open database. I haven't re-tested whether I can open the database from the script but I couldn't before.

Trying to debug these rule action scripts is horrible because, as far as I can tell, the rule action handler swallows up any error messages, including try-on error messages, and the script wanders off to some random place before exiting.

I don't think the problem relates to permissions, etc. I have the >exact same problem with FileMaker Pro Advanced 12 in Snow >Leopard. Interestingly, you can "tell" FileMaker to get a >database's name but telling it to open a database and/or write a >record fails without an error message. And, as with your >experience, the exact same code runs perfectly when run manually, >ie not invoked as a rule action. I have reached the conclusion >that the inability to script another app from within a mail rule script is an undocumented "feature" of Mail :(

| improve this answer | |
0

i encountered your post after i run in the same issue with Apple Mail

I could solve why this problem occurs.

I also setting up a Rule in Apple Mail with multiple actions who moved the mail to another folder/mailbox and then executing a Applescript - (i was archiving attachments with the script - nothing fancy)

It now happens in Catalina that Apple Mail running in a kind of race condition in applying the rule actions. So if the script runs it trys to read the contend from the mails from a certain disk location but the rule action from Apple Mail is moving the mail away simultaneously. So the script fails to read the mails. You can't even use a simple set mySubject to subject of theMessage because even this simple access will try to read the mail contend from disk.

The only solution at the moment is to set up a rule who only execute the applescript (or may be make simple tasks like setting a tag) and try to sorting/moving the mails to its new locations in the script itself; which will make the script more complex and heavy to maintain...

I don't know if this should be filed as bug to Apple or if this is intended behavior.

hope this helps a bit

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .