AppleScript for folder action stops working when I make a minor change

I have a folder action that runs an AppleScript. All it does is just grab the files in a folder and “embeds” the file in an email (i.e. embedding images inline in an email vs as an “attachment”).

I’ve used it successfully for years and through different macOS versions (I’m currently still on Mojave).

But I tried to change the sent-to email address today, and the script stopped working. Now it’ll open a new email. fill in the subject and recipient, but stops there and doesn’t embed the picture and doesn’t send the email.

Also, it still doesn’t work after I revert the change. I had to restore the applescript file from a backup to get it to work again.

Does anyone know why it would do this and how I can fix it?

Here’s the script:

on adding folder items to thisFolder after receiving theseItems
    tell application "Mail" to activate
    repeat with thisItem in theseItems
        tell application "Finder" to set thisName to name of thisItem
        tell application "Mail"
            set theMessageBody to ""
            set theMessage to make new outgoing message with properties {subject:thisName, content:theMessageBody}
            tell theMessage
                make new to recipient with properties {name:"xyz", address:"xyz@domain.com"}
                make new attachment with properties {file name:thisItem} at after the last word of the last paragraph
                delay 1
                send
            end tell
        end tell
    end repeat
end adding folder items to

edjusted. I ran your script as a regular script (just to simplify testing) and it threw an error because theMessageBody contains no text or paragraphs. There are a number of ways to fix this, but modifying the following line as shown is probably the simplest:

make new attachment with properties {file name:thisItem}

After changing the above and increasing the delay slightly, the script seemed to work OK with one email sent for each file. If you want all files attached to one email, a slightly different approach is needed:

set theAttachments to choose file with multiple selections allowed

tell application "Mail"
	activate
	tell (make new outgoing message with properties {subject:"File Attachments"})
		make new to recipient with properties {name:"xyz", address:"xyz@domain.com"}
		repeat with anAttachment in theAttachments
			make new attachment of content with properties {file name:anAttachment} at after the last paragraph
			delay 1 -- test with different values
		end repeat
		delay 1 -- test with different values
		send
	end tell
end tell

That fixed it. Thanks!