AppleScript Messages not Working in Mail

I have a simple AppleScript that I am trying to build that will save an email attachment to a file folder on my system. In trying to debug it I am finding the script appears to crash on trying to get any values from the message.


Here is the full script that I want to run:


set attachmentsFolder to "/Volumes/Folder" as text

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			repeat with each_message in these_messages
				repeat with theAttachment in each_message's mail attachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & ":" & originalName
					try
						save theAttachment in savePath
					end try
				end repeat				
			end repeat
		end tell
	end perform mail action with messages
end using terms from


I have created a simple example that fails to run:


using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			repeat with each_message in these_messages
				set theSubject to subject of each_message
				display dialog theSubject
			end repeat
		end tell
	end perform mail action with messages
end using terms from


In the second script, the "display dialog theSubject" never displays. In the first script the failure happens when trying to execute the "repeat with theAttachment in each_message's mail attachments" line. I have tried sending a "display dialog" in front of the lines and behind them. The script never gets to the second "display dialog" call which tells me the failure is crashing the script at that point.


Does anyone know what is going on with AppleScript or where I find the AppleScript Mail reference?


MacBook Pro 16″, macOS 14.1

Posted on Dec 31, 2023 1:46 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 2, 2024 3:01 PM

Turns out to be more syntactical of an issue. To get and use the network path I had to use:


set attachmentsFolder to ("Volumes:Accounting:Expenses:" as string)


Setting it to String rather than Text, and placing it inside of the on perform mail action.


The first time I ran it there was a security prompt asking if I wanted to allow access to the network folder.


The final script is:


using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		set attachmentsFolder to ("Volumes:Accounting:Expenses:" as string)
		tell application "Mail"
			repeat with theMsg in (get selection)
				set theAttachments to theMsg's mail attachments
				repeat with theAttachment in theAttachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & originalName
					save theAttachment in file savePath with replacing
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Now I have to figure out how to get it to work consistently. That will be a different post though.



Similar questions

4 replies
Question marked as Top-ranking reply

Jan 2, 2024 3:01 PM in response to SchneiderIS4Mac

Turns out to be more syntactical of an issue. To get and use the network path I had to use:


set attachmentsFolder to ("Volumes:Accounting:Expenses:" as string)


Setting it to String rather than Text, and placing it inside of the on perform mail action.


The first time I ran it there was a security prompt asking if I wanted to allow access to the network folder.


The final script is:


using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		set attachmentsFolder to ("Volumes:Accounting:Expenses:" as string)
		tell application "Mail"
			repeat with theMsg in (get selection)
				set theAttachments to theMsg's mail attachments
				repeat with theAttachment in theAttachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & originalName
					save theAttachment in file savePath with replacing
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Now I have to figure out how to get it to work consistently. That will be a different post though.



Jan 2, 2024 9:46 AM in response to SchneiderIS4Mac

Next evolution of insight. Looks like this is an issue of saving to a network location rather than locally on the Mac. If I use the following everything works fine.


set attachmentsFolder to (((path to home folder) as string))


Note: at this point I am using this thread to document my experience hoping to help others. I am looking forward to any contributions that others can provide.

Dec 31, 2023 1:59 PM in response to SchneiderIS4Mac

Some additional detail. I added a Try catch around the conflict line and then "display dialog" the error string. That gives me the following.


Can’t get «class mssg» id 825282 of «class mbxp» "INBOX/Expenses" of «class mact» id "BB40674E-77B4-40B3-AD07-90C3BE3F8719".


The Expenses subfolder of INBOX is where I am running the script against with Apply Rules.


Jan 1, 2024 12:26 PM in response to SchneiderIS4Mac

I have managed to make progress on this one, though the approach might need some reworking when it comes to the final script run from Mail Rules. There is still a problem though with the very last step of saving to the file system.


Here is the new version of the original script based on a version that uses the selected message and then Apply Rules in the Messages menu of Mail.


using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		set attachmentsFolder to (((path to home folder) as string) & "Volumes:Accounting:Expenses:")
		tell application "Mail"
			set theCount to count of (get selection) -- If there are more than one we need to iterate through them
			repeat with theMsg in (get selection)
				set theAttachments to theMsg's mail attachments
				set theAttachementCount to count of theAttachments
				repeat with theAttachment in theAttachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & originalName
					try
						save theAttachment in file savePath
					on error errStr number errorNumber
						display dialog "Error On Save Attachments:" & linefeed & linefeed & errStr
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Some points to note that I am still learning is how the file system works, which might be the part causing my my issues in this current script. When I got to run the following:


save theAttachment in file savePath


I get the error: Mail got an error: AppleEvent handler failed.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript Messages not Working in Mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.