Account name

Hello People.

im a bit stuck here, this almost work like i wanted it to, but i need to get the account name to
and no mater what i do nothing will work.



using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theSender to (extract name from sender)
					set theDate to date received
					set theContents to content as text
					set theSubject to subject as text
					set theId to (eachMessage's id as string)
					
					-- Here is my problem, i need to extract the account name the mail
					-- has been send to, im using alot of accounts in mail.app and need
					-- to save specifed by /account/id/*content
					
					set theAccount to name of account of mailbox of thisMessage
					
				end tell
			end tell
			set theDate to short date string of theDate
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			set theDate to text items of theDate
			set AppleScript's text item delimiters to oldDelims
			set theDate to text items of theDate as text
			
			do shell script ("mkdir /Users/mkh/Desktop/Test/") & theId
			set TempFile to get ((path to desktop folder from user domain) as string) & "Test:" & theId & ":" & theSender & "_" & theSubject & ".txt"
			
			set theTextFile to open for access TempFile with write permission
			write theContents to theTextFile
			close access theTextFile
		end repeat
	end perform mail action with messages
end using terms from


so if somebody have a hit it would be nice…

  • mkh

Hi mkh

set theAccount to name of account of mailbox of thisMessage

is actually correct but:
¢ there is a misprint: a variable thisMessage doesn’t exist;-)
¢ it works outside the tell (eachMessage) block

try this

...
	set theSubject to subject as text
	set theId to (eachMessage's id as string)
end tell		
set theAccount to name of account of mailbox of eachMessage
...	

I usually test Mail rule scripts by commenting out the first and the last two lines and
inserting at top of the script

tell application "Mail" to set theMessages to selection

then you get error messages, if errors occur

Hello Stefan.

Thanks alot, i was not aware of the use of error checking so thanks,
regarding to the script, i was able to make it work now…
and thanks for the fast respond, thats the good thing about this forum
people are so freaking fast :O)

grrr, okay i give up, 2 days and im wasted :confused:
can’t get the damm script to work, im a newb on folders :confused:

what i need is to make a folder named account name, inside that folder
i want to make a folder named the ID of the mail, and then put mail content
and all attachments in this folder to, im so close. :frowning:

" /Account/ID/Subject/message.txt "



using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			
			tell application "Mail"
				tell eachMessage
					set theSender to (extract name from sender)
					set theDate to date received
					set theContents to content as text
					set theSubject to subject as text
					set theId to (eachMessage's id as string)
				end tell
				
				set theAccount to name of account of mailbox of eachMessage
				
			end tell
			
			set theDate to short date string of theDate
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			set theDate to text items of theDate
			set AppleScript's text item delimiters to oldDelims
			set theDate to text items of theDate as text
			
			set mailPath to do shell script ("mkdir ~/Desktop/Mails/") & theAccount
			set TempFile to get ((path to desktop folder from user domain) as string) & "Mails:" & theAccount & ":" & theSender & "_" & theSubject & ".txt"
			
			set theTextFile to open for access TempFile with write permission
			write theContents to theTextFile
			close access theTextFile
			
		end repeat
		
		tell application "Mail"
			
			set pathToAttachments to "OS X:Users:mkh:Desktop:Mails:" & theAccount & ":"
			
			repeat with theMessage in theMessages
				
				if theMessage's mail attachments is not {} then
					
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to pathToAttachments & "#" & (theMessage's id as string) & space & theAttachment's name
						
						try
							save theAttachment in theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
			
		end tell


  • mkh

Hi mkh,

try this:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set theSender to (extract name from sender)
					set theDate to date received
					set theContents to content as text
					set theSubject to subject as text
					set theId to id as Unicode text
				end tell
				set theAccount to name of account of mailbox of eachMessage
			end tell
			set theDate to short date string of theDate
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {"."}
			set theDate to text items of theDate
			set AppleScript's text item delimiters to oldDelims
			set theDate to text items of theDate as text
			set mailPath to make_folder(path to desktop, "Mails")
			set AccountFolder to make_folder(mailPath, theAccount)
			set IDFolder to make_folder(AccountFolder, theId)
			set TempFile to (IDFolder as Unicode text) & theSender & "_" & theSubject & ".txt"
			try
				set theTextFile to open for access TempFile with write permission
				write theContents to theTextFile -- throws error if contents is empty
				close access theTextFile
			end try
			
			tell application "Mail"
				repeat with theAttachment in (get eachMessage's mail attachments)
					set theFileName to (IDFolder as Unicode text) & "#" & theId & space & theAttachment's name
					try
						save theAttachment in theFileName
					on error errnum
					end try
				end repeat
			end tell
		end repeat
	end perform mail action with messages
end using terms from

on make_folder(fPath, fName)
	try
		return ((fPath as string) & fName) as alias
	on error
		tell application "Finder" to set newfolder to make new folder at fPath with properties {name:fName}
	end try
	return newfolder as alias
end make_folder

Stefan this is just PERFECT, only one tiny thing, i added it as a rule for all incomming messages and used run applescript, but i need to select the mail and then apply rule before anything happens :frowning:

I’m going to use this for a mail index and gonna add over 20 accounts so need it
to be able to run auto… but still, damm man you are fast :smiley:

maybe you have a rule before this rule with a “Stop evaluating rules” command

Hm, nope, currently i have 2 rules added where save mail is the first, the second is for growl
but that script will not be used on the same machine as the save mail script…



(*
	Display a notification of incoming email in Growl. 
	To use this script, move this script to a known location 
	(~/Library/Scripts/Mail Scripts, for example) and create a 
	rule in Mail.app that invokes this script when a new 
	message is received.
*)

using terms from application "Mail"
	on perform mail action with messages selectedMsgs
		tell application "Mail"
			set appName to "Mail Notification Script"
			set notificationName to "Mail Notification"
			set notifs to {notificationName}
			tell application "GrowlHelperApp"
				register as application appName all notifications notifs default notifications notifs icon of application "Mail"
			end tell
			
			repeat with msg in selectedMsgs
				if not junk mail status of msg then
					set theSubject to subject of msg
					set theSender to extract name from sender of msg
					set theAccount to name of account of mailbox of msg
					tell application "GrowlHelperApp"
						notify with name notificationName application name appName title "Incoming Mail " & theAccount description theSender & ":  " & theSubject
					end tell
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Tested with only save mail added in rules, and still nothing :frowning:

hm weird, no mater what i do this damm script dont work,
removed all my other rulse to see if the interfer, but nada

cansomebody tell me why, the script is working just not
auto when i get new mails i need to select dem before
the rule works.