Sunday, November 24, 2024

finder – how do I automatically send an email (w/ attachment) to recipients when an xlsx file with their address is added to a folder?

I’m using macOS 14.6.1 and Outlook 16.89.3. I can’t quite find an answer to this question on the forum.

I’m trying to create a folder actions service/script to monitor a particular folder for any new excel files (only excel files would be added to this folder anyway) and, when a file is added, send that file, via Outlook, to the user whose email address is found in cell B2 of the spreadsheet.

I’ve tried using chatgpt for this. It gives me a script that works when using Apple Mail, but not Outlook.

Here is one example of what chatgpt says I should use…which keeps failing. When I drop a file in the folder, I see the little gear icon turning in the menu bar, but there are no dialog boxes that appear, no error messages, and no created/sent emails in Outlook.

on adding folder items to this_folder after receiving added_items
    repeat with added_item in added_items
        set filePath to POSIX path of added_item
        -- Check if the file is an Excel file
        if filePath ends with ".xlsx" then
            display dialog "File added: " & (name of added_item) -- Debugging message
            try
                -- Open the Excel file to read the email address
                tell application "Microsoft Excel"
                    open filePath
                    delay 1 -- Wait for Excel to open
                    set recipientEmail to value of cell "B2" of active sheet
                    close active workbook saving no -- Close the workbook without saving
                end tell
                
                -- Ensure recipientEmail is valid
                if recipientEmail is not missing value then
                    -- Prepare the email
                    tell application "Microsoft Outlook"
                        set newMessage to make new outgoing message with properties {subject:"Your Requested Excel File", content:"Hello,\n\nPlease find the requested Excel file attached.\n\nBest regards,\nYour Name"}
                        make new recipient at newMessage with properties {email address:{address:recipientEmail}}
                        make new attachment at newMessage with properties {file:added_item}
                        send newMessage
                    end tell
                else
                    display dialog "No email address found in cell B2 of " & (name of added_item)
                end if
            on error errMsg
                display dialog "Error processing file: " & errMsg
            end try
        end if
    end repeat
end adding folder items to

Thanks for your time.

Related Articles

Latest Articles