Implement "choose message" in Apple Mail, like Choose File

Once I open Apple Mail in AppleScript, I want to be able to let the user manually select a message (like Choose File but for mail), so I can open an attachment and perform a SaveAs with a programmatically determined meaningful name.

Is there code that allows the user to manually select an Apple Mail message?


Apps with potentially hugh databases (iTunes, iPhoto, Mail) lack choose [object] commands, probably because it’s quite expensive to read the object hierarchy from the database on the fly.
The common way is to first select the object(s) then run the script to process the selected object(s)

That sounds promising.

Assuming Apple Mail is open, the mail message is open, the attachment is open, could you direct me to a reference or a sample of code that would enable me to identify that open attachment object so that I can manipulate it, i.e. save as?


Here is a snippet you may have a look at, maybe you can adjust it to your needs.

I have a different approach for a different problem. I have multiple
e-mail accounts; i read all together emails in my inbox, then i 
select some of them and i want to file them in a folder
'read' specific for the account. So each message must be filed in
the specific 'read' folder of the account that received it. If a
message has a flag, must remain in the inbox folder.

Well i wrote a small applescript that i run with FastScript. Here it
 is: (please note that the folder name 'read' is hardcoded but can be
  changed easily)

tell application "Mail"
	set the_selection to selection
	if (count of the selection) is equal to 0 then
		display dialog "Please select a message in Mail first, then run this script again."
		repeat with eachItem in the_selection
			if (not the flagged status of eachItem) then -- non muove i messaggi flaggati
				set thATTch to name of item 1 of mail attachments of eachItem
				log "" & thATTch
				--	set theAccount to account of mailbox of eachItem
				--	set mailbox of eachItem to mailbox "Read" of theAccount
				set itsID to (get id of thATTch)
				--		tell application "Finder" to open file thATTch
			end if
		end repeat
	end if
end tell

I sense that you haven’t been to exposed to AppleScript. So my advice to you is to open the dictionary of the library of AppleScript Editor, and look at the various objects, and the properties they posess while you are reading the script. You can also download a 20-day trial copy of script debugger, it depicts the object graph really well.
If you are going to Applescript at work, try to formalize it, so they’ll pay for Script Debugger, IMHO, the fastest way to get working scripts, escpecially for a freshman.

By the way, this script works from the fact, that you already have a messages list, so you just select the messages from within mail, and then run the script. It may err if the view is threaded. It worked under Snow Leopard

Thanks but I found a simple snippet which identifies the open file in Preview where the attachment will be opened because it is an image file.

tell application “System Events”
tell process “Preview”
tell (1st window whose value of attribute “AXMain” is true) --document file path and name
set fileURL to value of attribute “AXDocument” – document title
set windowTitle to value of attribute “AXTitle”
end tell
end tell
end tell

My question has been resolved.