AppleScript in Mail: Save attachments with Save dialog

Hi everyone,


I am trying to create a Mail rule which executes an AppleScript. The script so far works fine, but only with a hardcoded path, in this example it's my Downloads folder:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with oneMessage in theMessages
				set {mail attachment:theAttachments} to oneMessage
				repeat with oneAttachment in mail attachments of oneMessage
					save oneAttachment in ("Macintosh SSD:Users:bjoern:Downloads:") & (name of oneAttachment)
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from



I would now like to bring up a save dialog which asks for the folder to save to, but I already fail at my intermediary step of using a variable as the save path. This code works on its own:


set folderName to "Downloads"
set homePath to (path to home folder) as text
set saveFolder to homePath & folderName & ":"


If I run it, the result shows:


"Macintosh SSD:Users:bjoern:Downloads:"


But if I replace the hardcoded path with the variable saveFolder in my upper script, nothing happens.


Any ideas what I’m missing here?

Posted on May 30, 2024 8:35 AM

Reply
4 replies

May 30, 2024 12:57 PM in response to Björn Herrmann

You could shorten that to

path to downloads folder

But, I don't know if that will resolve the problem.

I think Downloads falls in with Desktop and Documents being protected in some way by the OS and you need to allow access to it. I would think it would present some error.

Have you tried setting it up to save a file in that path without the Mail rule interface?

May 30, 2024 2:25 PM in response to Barney-15E

Hi Barney,


I know that I could shorten this. It’s just something I tried. Problem is that neither way it will work. I don’t think it’s an access privilege based problem, because hardcoding the path as in my upper script works fine: It saves the attachments to my Downloads folder.


I'd like a bit more flexibility so ultimately it should bring up a save dialog to select the save path. Since this didn’t work I tried this intermediary solution by exchanging the hardcoded path with a variable. But it fails to save anything and I don’t know why.


Debugging is difficult, because the script is run from within Mail and can not be tested in ScriptEditor. Any ideas what to try next?

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 in Mail: Save attachments with Save dialog

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