Export mailbox to the finder

I want to export selected emails from a given mailbox to the finder. I found this script on the internet and altered it to my likes. But it doen’t seems to export. Does somebody know where it is going wrong? I get the error messages -10004 and -1700. I googled online but i can’t find a solution.

tell application "Mail"
	set msgs to message of mailbox "Test" of account "Info"
	
	if length of msgs is not 0 then
		display dialog "Export selected message(s)?"
		if the button returned of the result is "OK" then
			
			-- set up month parsing value for French Vanilla algorithm
			set fixedDate to current date --or any other date
			set month of fixedDate to January
			
			
			-- set theFolder to "Macintosh HD:Users:rajsingh:Desktop:" as alias
			set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles
			
			repeat with msg in msgs
				-- get path to message
				set mb to mailbox of msg
				set mba to account of mb
				-- mtype is returning 'constant **** ETIM' when it should be imap (for OGC account)
				-- set mtype to (account type) of mba
				set mtype to "imap"
				set accountpath to account directory of account of mb
				set fullpath to accountpath & name of mb & "." & mtype & "mbox:Messages:"
				
				-- figure out message name
				set msgfilename to id of msg
				set atts to number of mail attachments of msg
				if atts > 0 then
					set msgfilename to msgfilename & ".partial.emlx"
				else
					set msgfilename to msgfilename & ".emlx"
				end if
				set fullpath to fullpath & msgfilename
				
				set theFile to fullpath as Unicode text
				
				-- create new name prefix based on date
				set msgDate to date received of msg
				
				-- parse date
				-- use French Vanilla algorithm to get month number
				set theMonth to (2 + (msgDate - fixedDate + 1314864) div 2629728) as rich text
				if length of theMonth < 2 then
					set theMonth to "0" & theMonth
				end if
				
				set theDay to day of msgDate as rich text
				if length of theDay < 2 then
					set theDay to "0" & theDay
				end if
				
				set msgDate to year of msgDate & theMonth & theDay & "at" & hours of msgDate & minutes of msgDate & seconds of msgDate
				
				set comparison_string to ":/"
				set replacement_string to "->"
				set msgSubject to ""
				repeat with c in (subject of msg as string)
					set x to the offset of c in comparison_string
					if x is not 0 then
						set msgSubject to (msgSubject & character x of replacement_string) as string
					else
						set msgSubject to (msgSubject & c) as string
					end if
				end repeat
				set msgSubject to my replaceText("Re- ", "", msgSubject)
				set msgSubject to my replaceText("Re-", "", msgSubject)
				--set msgSubject to text 1 thru 49 of msgSubject
				
				set newFile to (msgSubject & "_" & msgDate & ".emlx") as Unicode text
				--set newFile to (msgSubject & "_" & msgDate & "_" & msgfilename) as Unicode text
				
				-- copy mail message to the folder and prepend date-time to file name
				tell application "Finder"
					try
						set intFile to duplicate theFile to theFolder
						onerror
						display dialog "couldn't duplicate " & intFile
					end try
					-- rename file
					try
						set name of intFile to newFile
						oneerror
						display dialog "couldn't set name of " & intFile & " to " & newFile
					end try
					-- reveal intFile
					-- open theFile as alias
				end tell
				
			end repeat
			
			beep 2
			display dialog "Done exporting " & length of msgs & " messages."
		end if -- OK to export msgs
	end if -- msgs > 0
end tell

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

Model: iMac
AppleScript: 2.4.3.
Browser: Safari 537.17
Operating System: Mac OS X (10.8)

Hi,

since Leopard there is an unique text class as synonyms for string, text and Unicode text,
rich text is outdated and would throw an error outside the Mail tell block.

Replace all occurrences of rich text, Unicode text and string with text.
The lines

 repeat with c in (subject of msg as string)
                
set msgSubject to (msgSubject & character x of replacement_string) as string
                   
set msgSubject to (msgSubject & c) as string
                  
set newFile to (msgSubject & "_" & msgDate & ".emlx") as Unicode text

don’t need any coercions at all.

This line is problematic


 set msgDate to year of msgDate & theMonth & theDay & "at" & hours of msgDate & minutes of msgDate & seconds of msgDate

because the leftmost operand (and therefore the result) is an integer and “at” could not be coerced to integer
Try


set msgDate to (year of msgDate as text) & .

Hope it helps

Hi. Since your target is a known mailbox and account, I wouldn’t keep obtaining the mailbox, account, type, and directory; that information should be static and reside outside the loop. Perhaps this has changed in Mountain Lion, but account directory returns a POSIX path for me. I think accountpath might need to be coerced (“as posix file as text”).

Hi Marc and Stefan,

Thanks for your replys! Sorry for the late response, i was on a holiday. Just worked on the project and made the changes you said (hope i did this right?).

@Stefan: I noticed that when i change rich text, Unicode text and string with text, it automaticly changes it back to rich text. Here’s my updated applescript, and i also posted the error log.

@ Marc: The mailbox will eventually be dynamic. The script will run in filemaker with dynamic mailboxes. The accountname stays the same. And how do i coerce the accountpath?

Updated script:

tell application "Mail"
	set msgs to message of mailbox "Test" of account "Info"
	
	if length of msgs is not 0 then
		display dialog "Export selected message(s)?"
		if the button returned of the result is "OK" then
			
			-- set up month parsing value for French Vanilla algorithm
			set fixedDate to current date --or any other date
			set month of fixedDate to January
			
			
			-- set theFolder to "Macintosh HD:Users:rajsingh:Desktop:" as alias
			set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles
			
			repeat with msg in msgs
				-- get path to message
				set mb to mailbox of msg
				set mba to account of mb
				-- mtype is returning 'constant **** ETIM' when it should be imap (for OGC account)
				-- set mtype to (account type) of mba
				set mtype to "imap"
				set accountpath to account directory of account of mb
				set fullpath to accountpath & name of mb & "." & mtype & "mbox:Messages:"
				
				-- figure out message name
				set msgfilename to id of msg
				set atts to number of mail attachments of msg
				if atts > 0 then
					set msgfilename to msgfilename & ".partial.emlx"
				else
					set msgfilename to msgfilename & ".emlx"
				end if
				set fullpath to fullpath & msgfilename
				
				set theFile to fullpath as rich text
				
				-- create new name prefix based on date
				set msgDate to date received of msg
				
				-- parse date
				-- use French Vanilla algorithm to get month number
				set theMonth to (2 + (msgDate - fixedDate + 1314864) div 2629728) as rich text
				if length of theMonth < 2 then
					set theMonth to "0" & theMonth
				end if
				
				set theDay to day of msgDate as rich text
				if length of theDay < 2 then
					set theDay to "0" & theDay
				end if
				
				set msgDate to (year of msgDate as rich text) & theMonth & theDay & "at" & hours of msgDate & minutes of msgDate & seconds of msgDate
				
				set comparison_string to ":/"
				set replacement_string to "->"
				set msgSubject to ""
				repeat with c in subject of msg as rich text
					set x to the offset of c in comparison_string
					if x is not 0 then
						set msgSubject to msgSubject & character x of replacement_string as rich text
					else
						set msgSubject to msgSubject & c as rich text
					end if
				end repeat
				set msgSubject to my replaceText("Re- ", "", msgSubject)
				set msgSubject to my replaceText("Re-", "", msgSubject)
				--set msgSubject to text 1 thru 49 of msgSubject
				
				set newFile to msgSubject & "_" & msgDate & ".emlx" as rich text
				--set newFile to (msgSubject & "_" & msgDate & "_" & msgfilename) as text
				
				-- copy mail message to the folder and prepend date-time to file name
				tell application "Finder"
					try
						set intFile to duplicate theFile to theFolder
						onerror
						display dialog "couldn't duplicate " & intFile
					end try
					-- rename file
					try
						set name of intFile to newFile
						oneerror
						display dialog "couldn't set name of " & intFile & " to " & newFile
					end try
					-- reveal intFile
					-- open theFile as alias
				end tell
				
			end repeat
			
			beep 2
			display dialog "Done exporting " & length of msgs & " messages."
		end if -- OK to export msgs
	end if -- msgs > 0
end tell

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

Output:

tell application "Mail"
	get message of mailbox "Test" of account "Info"
		--> {message id 78028 of mailbox "Test" of account "Info", message id 78030 of mailbox "Test" of account "Info", message id 78029 of mailbox "Test" of account "Info"}
	display dialog "Export selected message(s)?"
		--> {button returned:"OK"}
	current date
		--> error number -10004
end tell
tell current application
	current date
		--> date "woensdag 13 februari 2013 11:28:09"
end tell
tell application "Mail"
	choose folder with prompt "Save Exported Messages to..." without invisibles
		--> alias "Macintosh HD:Users:tomgoedhart:Desktop:test:"
	get mailbox of message id 78028 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78028 of mailbox "Test" of account "Info"
		--> 78028
	count every mail attachment of message id 78028 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78028 of mailbox "Test" of account "Info"
		--> date "zaterdag 2 februari 2013 10:11:14"
	get subject of message id 78028 of mailbox "Test" of account "Info"
		--> "Het ideale cadeau voor jouw Valentijn!"
	offset of "H" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "H" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "t" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "t" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "i" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "i" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "d" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "d" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "l" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "l" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "c" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "c" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "d" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "d" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "u" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "u" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "v" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "v" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "o" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "o" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "o" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "o" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "r" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "r" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "j" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "j" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "o" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "o" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "u" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "u" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "w" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "w" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "V" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "V" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "l" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "l" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "t" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "t" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "i" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "i" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "j" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "j" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "!" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "!" in ":/"
		--> 0
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78028.emlx" to item
end tell
tell application "Mail"
	get mailbox of message id 78030 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78030 of mailbox "Test" of account "Info"
		--> 78030
	count every mail attachment of message id 78030 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78030 of mailbox "Test" of account "Info"
		--> date "zaterdag 2 februari 2013 04:48:26"
	get subject of message id 78030 of mailbox "Test" of account "Info"
		--> "Arnhem: 6 passende woningen"
	offset of "A" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "A" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "r" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "r" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "h" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "h" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "m" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "m" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of ":" in ":/"
		--> error number -10004
end tell
tell current application
	offset of ":" in ":/"
		--> 1
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "6" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "6" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "p" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "p" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "s" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "s" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "s" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "s" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "d" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "d" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "w" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "w" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "o" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "o" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "i" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "i" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "g" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "g" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78030.emlx" to item
end tell
tell application "Mail"
	get mailbox of message id 78029 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78029 of mailbox "Test" of account "Info"
		--> 78029
	count every mail attachment of message id 78029 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78029 of mailbox "Test" of account "Info"
		--> date "vrijdag 1 februari 2013 04:46:33"
	get subject of message id 78029 of mailbox "Test" of account "Info"
		--> "Arnhem: 6 passende woningen"
	offset of "A" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "A" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "r" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "r" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "h" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "h" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "m" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "m" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of ":" in ":/"
		--> error number -10004
end tell
tell current application
	offset of ":" in ":/"
		--> 1
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "6" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "6" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "p" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "p" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "a" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "a" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "s" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "s" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "s" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "s" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "d" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "d" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of " " in ":/"
		--> error number -10004
end tell
tell current application
	offset of " " in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "w" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "w" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "o" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "o" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "i" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "i" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "g" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "g" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "e" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "e" in ":/"
		--> 0
end tell
tell application "Mail"
	offset of "n" in ":/"
		--> error number -10004
end tell
tell current application
	offset of "n" in ":/"
		--> 0
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78029.emlx" to item
end tell
tell application "Mail"
	beep 2
		--> error number -10004
end tell
tell current application
	beep 2
end tell
tell application "Mail"
	display dialog "Done exporting 3 messages."
		--> {button returned:"OK"}
end tell
Resultaat:
{button returned:"OK"}

The problem is clear… Mountain lion doesn’t follow the parenting chain as SL does. The code works in SL because offset command is eventually send to current application as it should be. Offset command is send to application mail (read; target application AppleScript) while the current application should receive the AppleScript event. To make it work you should replace the line:

set x to the offset of c in comparison_string

with

tell current application to set x to the offset of c in comparison_string

However I think the whole construction to find out if a char

          set msgSubject to ""
          repeat with c in (subject of msg as string)
                   set x to the offset of c in comparison_string
                   if x is not 0 then
                       set msgSubject to (msgSubject & character x of replacement_string) as string
                   else
                       set msgSubject to (msgSubject & c) as string
                   end if
           end repeat

Because you’re already text replacing, this would be an easy alernative:

set msgSubject to my replaceText(":", "-", msgSubject)
set msgSubject to my replaceText("/", ">", msgSubject)

Almost getting there. A lot of the errors disappeared. Still the emails won’t export to the folder. THis is the output now:

tell application "Mail"
	get message of mailbox "Test" of account "Info"
		--> {message id 78028 of mailbox "Test" of account "Info", message id 78030 of mailbox "Test" of account "Info", message id 78029 of mailbox "Test" of account "Info"}
	display dialog "Export selected message(s)?"
		--> {button returned:"OK"}
	current date
		--> error number -10004
end tell
tell current application
	current date
		--> date "woensdag 13 februari 2013 16:20:22"
end tell
tell application "Mail"
	choose folder with prompt "Save Exported Messages to..." without invisibles
		--> alias "Macintosh HD:Users:tomgoedhart:Desktop:test:"
	get mailbox of message id 78028 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78028 of mailbox "Test" of account "Info"
		--> 78028
	count every mail attachment of message id 78028 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78028 of mailbox "Test" of account "Info"
		--> date "zaterdag 2 februari 2013 10:11:14"
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78028.emlx" to item
end tell
tell application "Mail"
	get mailbox of message id 78030 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78030 of mailbox "Test" of account "Info"
		--> 78030
	count every mail attachment of message id 78030 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78030 of mailbox "Test" of account "Info"
		--> date "zaterdag 2 februari 2013 04:48:26"
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78030.emlx" to item
end tell
tell application "Mail"
	get mailbox of message id 78029 of mailbox "Test" of account "Info"
		--> mailbox "Test" of account "Info"
	get account of mailbox "Test" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "Test" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "Test" of account "Info"
		--> "Test"
	get id of message id 78029 of mailbox "Test" of account "Info"
		--> 78029
	count every mail attachment of message id 78029 of mailbox "Test" of account "Info"
		--> 0
	get date received of message id 78029 of mailbox "Test" of account "Info"
		--> date "vrijdag 1 februari 2013 04:46:33"
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:Test.imapmbox:Messages:78029.emlx" to item
end tell
tell application "Mail"
	beep 2
		--> error number -10004
end tell
tell current application
	beep 2
end tell
tell application "Mail"
	display dialog "Done exporting 3 messages."
		--> {button returned:"OK"}
end tell
Resultaat:
{button returned:"OK"}

Your try catch are wrong too…


try

on error --> not onerror

end try

If then the copy still doesn’t work, you get at least a proper error (script works on my machine).

Hi Dj,

I updated the script and i get a proper error now! It says that the variable intFile isn’t defined? The full script does work on your machine? With version of mac do you have?

Tom

tell application "Mail"
	get message of mailbox "test1" of account "Info"
		--> {message id 78733 of mailbox "test1" of account "Info", message id 78734 of mailbox "test1" of account "Info", message id 78735 of mailbox "test1" of account "Info", message id 78736 of mailbox "test1" of account "Info"}
	display dialog "Export selected message(s)?"
		--> error number -1708
	«event ascrgdut»
		--> error number -1708
	display dialog "Export selected message(s)?"
		--> {button returned:"OK"}
	current date
		--> error number -10004
end tell
tell current application
	current date
		--> date "woensdag 20 februari 2013 09:35:51"
end tell
tell application "Mail"
	choose folder with prompt "Save Exported Messages to..." without invisibles
		--> alias "Macintosh HD:Users:tomgoedhart:Desktop:test:"
	get mailbox of message id 78733 of mailbox "test1" of account "Info"
		--> mailbox "test1" of account "Info"
	get account of mailbox "test1" of account "Info"
		--> account "Info"
	get account directory of account of mailbox "test1" of account "Info"
		--> file "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:"
	get name of mailbox "test1" of account "Info"
		--> "test1"
	get id of message id 78733 of mailbox "test1" of account "Info"
		--> 78733
	count every mail attachment of message id 78733 of mailbox "test1" of account "Info"
		--> 0
	get date received of message id 78733 of mailbox "test1" of account "Info"
		--> date "woensdag 13 februari 2013 17:47:06"
end tell
tell application "Finder"
	-- 'core'\'clon'{ 'insh':'alis'($000000000110000200010C4D6163696E746F7368204844000000000000000000000000000000C90C4D42482B00000005D81C0474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009AD58DDCD32BD0F0000000000000000FFFFFFFF00004920000000000000000000000000001000080000C90C3F320000001100080000CD32AEFF0000000E000A00040074006500730074000F001A000C004D006100630069006E0074006F007300680020004800440012001E55736572732F746F6D676F6.
		--> error number -1700 from "Macintosh HD:Users:tomgoedhart:Library:Mail:V2:IMAP-infomail@server.local:test1.imapmbox:Messages:78733.emlx" to item
Resultaat:
error "De variabele intFile is niet gedefinieerd." number -2753 from "intFile"