Description
Based on Hans Erik Hazelhorst's Apple Mail – Export to Text File w/ Basic Header script, this variation collects only the basic headers of all of the selected email messages and writes them to a "MessageDigest.txt" file in the Temp Export folder on the desktop.The Code
(*
Veritrope.com
Apple Mail -- Text File Exporter - Single file w/ Only Basic Header Info.
Version 1.0
Sept 15, 2011
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/apple-mail-headers-to-a-text-file
// CHANGELOG:
1.0 Initial Release
// RECOMMENDED INSTALLATION INSTRUCTIONS:
FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/NAME OF APP
--Set up your keyboard shortcut
*)
(*
======================================
// PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)
property growlName : "MailTextExport"
property appName : "Mail"
property successCount : "0"
(*
======================================
// MAIN PROGRAM
======================================
*)
--CHECK FOR GROWL
tell application "System Events"
set processnames to name of every process
end tell
if appIsRunning("GrowlHelperApp") then
set isRunning to true
tell application "GrowlHelperApp"
set allNotificationsList to {"Success Notification", "Failure Notification"}
set enabledNotificationsList to {"Success Notification", "Failure Notification"}
register as application growlName all notifications allNotificationsList default notifications enabledNotificationsList icon of application appName
end tell
end if
--TEMP FILES PROCESSED ON THE DESKTOP
tell application "Finder"
if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
if not (the folder "Temp Export Folder" exists) then
set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
end if
set SaveLoc to FolderLoc as string
end tell
--APPLE MAIL ITEM SELECTION
tell application "Mail"
try
if (selection is not {}) then
set theMessages to selection
set msgnr to 1
repeat with thisMessage in theMessages
set recipientlist to (to recipients of thisMessage)
(*Adding the Recipients in a separate variable *)
set theRecipients to "To: "
repeat with CurrentRecipient in recipientlist
set theRecipients to (theRecipients & " " & (name of CurrentRecipient) as rich text) & " "
end repeat
--…and also the cc-Recipients in a separate variable
set CcRecipientList to cc recipients of thisMessage
set theCcRecipients to ""
if the (count of CcRecipientList) > 0 then
set theCcRecipients to "CC: "
repeat with CurrentCCRecipient in CcRecipientList
set theCcRecipients to (theCcRecipients & " " & (name of CurrentCCRecipient) as rich text) & " "
end repeat
set theCcRecipients to (theCcRecipients & return)
end if
set myTitle to the subject of thisMessage
set myContent to the content of thisMessage as rich text
set ReplyAddr to the reply to of thisMessage
set afzender to the sender of thisMessage
set EmailDate to the date received of thisMessage
set theSource to the source of thisMessage
--Custom header with some standard data
set CustomHeader to "Date: " & EmailDate & return & ¬
"Subject: " & myTitle & return & ¬
"From: " & afzender
--CLEAN TITLE FOR FILE
set previousDelimiter to AppleScript's text item delimiters
set potentialName to myTitle
set legalName to {}
set illegalCharacters to {".", ",", "/", ":", "[", "]"}
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as rich text
if thisCharacter is not in illegalCharacters then
set the end of legalName to thisCharacter
end if
end repeat
set AppleScript's text item delimiters to ""
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as rich text
else
set legalName to legalName as rich text
end if
set AppleScript's text item delimiters to previousDelimiter
set theFileName to legalName
--EXPORT TO TXT FILE
set theText to myContent
-- set theFilePath to (SaveLoc & msgnr & "_" & theFileName & ".txt")
set theFilePath to (SaveLoc & "MessageDigest.txt")
set msgnr to msgnr + 1
set theFileReference to open for access theFilePath with write permission
write (CustomHeader & return & theRecipients & return & theCcRecipients & "----------" & return) starting at (1 + (get eof theFileReference)) to theFileReference as «class utf8»
-- write s2 starting at (1 + (get eof theFileReference)) to f
close access theFileReference
set successCount to successCount + 1
end repeat
-- GROWL
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL SUCCESS
notify with name ¬
"Success Notification" title ¬
"Export Success" description "Successfully Exported " & successCount & ¬
" Messages to Export Folder!" application name growlName
end tell
set successCount to 0
end if
else if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
notify with name ¬
"Failure Notification" title ¬
"Export Failure" description ¬
"No Messages or Notes Selected!" application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
display dialog "No Messages or Notes Selected!" with icon 0
end if
(* ERROR HANDLING *)
on error errText number errNum
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
notify with name ¬
"Failure Notification" title ¬
"Import Failure" description "Failed to export " & return & myTitle & ¬
" due to the following error: " & return & errText ¬
application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
display dialog "Item Failed to Export: " & errNum & return & errText with icon 0
end if
end try
end tell
(*
======================================
// PREPARATORY SUBROUTINES
======================================
*)
--MAKE LIST OF RUNNING APPS
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
(*
======================================
// UTILITY SUBROUTINES
======================================
*)
--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
set theStringParts to text items of theString
if (count of theStringParts) is greater than 1 then
set theString to text item 1 of theStringParts as string
repeat with eachPart in items 2 thru -1 of theStringParts
set theString to theString & theNewString & eachPart as string
end repeat
end if
set AppleScript's text item delimiters to od
return theString
end replaceString
Veritrope.com
Apple Mail -- Text File Exporter - Single file w/ Only Basic Header Info.
Version 1.0
Sept 15, 2011
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/apple-mail-headers-to-a-text-file
// CHANGELOG:
1.0 Initial Release
// RECOMMENDED INSTALLATION INSTRUCTIONS:
FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/NAME OF APP
--Set up your keyboard shortcut
*)
(*
======================================
// PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)
property growlName : "MailTextExport"
property appName : "Mail"
property successCount : "0"
(*
======================================
// MAIN PROGRAM
======================================
*)
--CHECK FOR GROWL
tell application "System Events"
set processnames to name of every process
end tell
if appIsRunning("GrowlHelperApp") then
set isRunning to true
tell application "GrowlHelperApp"
set allNotificationsList to {"Success Notification", "Failure Notification"}
set enabledNotificationsList to {"Success Notification", "Failure Notification"}
register as application growlName all notifications allNotificationsList default notifications enabledNotificationsList icon of application appName
end tell
end if
--TEMP FILES PROCESSED ON THE DESKTOP
tell application "Finder"
if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
if not (the folder "Temp Export Folder" exists) then
set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
end if
set SaveLoc to FolderLoc as string
end tell
--APPLE MAIL ITEM SELECTION
tell application "Mail"
try
if (selection is not {}) then
set theMessages to selection
set msgnr to 1
repeat with thisMessage in theMessages
set recipientlist to (to recipients of thisMessage)
(*Adding the Recipients in a separate variable *)
set theRecipients to "To: "
repeat with CurrentRecipient in recipientlist
set theRecipients to (theRecipients & " " & (name of CurrentRecipient) as rich text) & " "
end repeat
--…and also the cc-Recipients in a separate variable
set CcRecipientList to cc recipients of thisMessage
set theCcRecipients to ""
if the (count of CcRecipientList) > 0 then
set theCcRecipients to "CC: "
repeat with CurrentCCRecipient in CcRecipientList
set theCcRecipients to (theCcRecipients & " " & (name of CurrentCCRecipient) as rich text) & " "
end repeat
set theCcRecipients to (theCcRecipients & return)
end if
set myTitle to the subject of thisMessage
set myContent to the content of thisMessage as rich text
set ReplyAddr to the reply to of thisMessage
set afzender to the sender of thisMessage
set EmailDate to the date received of thisMessage
set theSource to the source of thisMessage
--Custom header with some standard data
set CustomHeader to "Date: " & EmailDate & return & ¬
"Subject: " & myTitle & return & ¬
"From: " & afzender
--CLEAN TITLE FOR FILE
set previousDelimiter to AppleScript's text item delimiters
set potentialName to myTitle
set legalName to {}
set illegalCharacters to {".", ",", "/", ":", "[", "]"}
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as rich text
if thisCharacter is not in illegalCharacters then
set the end of legalName to thisCharacter
end if
end repeat
set AppleScript's text item delimiters to ""
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as rich text
else
set legalName to legalName as rich text
end if
set AppleScript's text item delimiters to previousDelimiter
set theFileName to legalName
--EXPORT TO TXT FILE
set theText to myContent
-- set theFilePath to (SaveLoc & msgnr & "_" & theFileName & ".txt")
set theFilePath to (SaveLoc & "MessageDigest.txt")
set msgnr to msgnr + 1
set theFileReference to open for access theFilePath with write permission
write (CustomHeader & return & theRecipients & return & theCcRecipients & "----------" & return) starting at (1 + (get eof theFileReference)) to theFileReference as «class utf8»
-- write s2 starting at (1 + (get eof theFileReference)) to f
close access theFileReference
set successCount to successCount + 1
end repeat
-- GROWL
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL SUCCESS
notify with name ¬
"Success Notification" title ¬
"Export Success" description "Successfully Exported " & successCount & ¬
" Messages to Export Folder!" application name growlName
end tell
set successCount to 0
end if
else if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
notify with name ¬
"Failure Notification" title ¬
"Export Failure" description ¬
"No Messages or Notes Selected!" application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
display dialog "No Messages or Notes Selected!" with icon 0
end if
(* ERROR HANDLING *)
on error errText number errNum
if "GrowlHelperApp" is in processnames then
tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
notify with name ¬
"Failure Notification" title ¬
"Import Failure" description "Failed to export " & return & myTitle & ¬
" due to the following error: " & return & errText ¬
application name growlName
end tell
else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
display dialog "Item Failed to Export: " & errNum & return & errText with icon 0
end if
end try
end tell
(*
======================================
// PREPARATORY SUBROUTINES
======================================
*)
--MAKE LIST OF RUNNING APPS
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
(*
======================================
// UTILITY SUBROUTINES
======================================
*)
--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
set theStringParts to text items of theString
if (count of theStringParts) is greater than 1 then
set theString to text item 1 of theStringParts as string
repeat with eachPart in items 2 thru -1 of theStringParts
set theString to theString & theNewString & eachPart as string
end repeat
end if
set AppleScript's text item delimiters to od
return theString
end replaceString