Skip to content

Instantly share code, notes, and snippets.

@stevenrick
Created March 21, 2023 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenrick/99dd7035efe82b7e9ea1871b11a09624 to your computer and use it in GitHub Desktop.
Save stevenrick/99dd7035efe82b7e9ea1871b11a09624 to your computer and use it in GitHub Desktop.
Apple Script to extract Mail messages to txt files
using terms from application "Mail"
--save selected messages to text files
on perform mail action with messages messageList
set theFolder to choose folder with prompt "Save Exported Messages to..." without invisibles
set the message_count to the count of messageList
display dialog "Found " & (message_count) & " messages."
repeat with i from 1 to the message_count
set this_message to item i of messageList
set theFile to (((theFolder) as Unicode text) & (i) as Unicode text) & ".txt"
set theContent to (content of this_message) as Unicode text
try
set theFileID to open for access theFile with write permission
write theContent to theFileID
close theFileID
on error
display dialog "Can't write message"
end try
end repeat
display dialog "Done exporting " & (message_count) & " messages."
end perform mail action with messages
end using terms from
using terms from application "Mail"
on run
tell application "Mail" to set mySelection to selection
tell me to perform mail action with messages (mySelection)
end run
end using terms from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment