Skip to content

Instantly share code, notes, and snippets.

@acsr
Created January 8, 2024 16:40
Show Gist options
  • Save acsr/363dd667bba8184ef2222d7887083ce4 to your computer and use it in GitHub Desktop.
Save acsr/363dd667bba8184ef2222d7887083ce4 to your computer and use it in GitHub Desktop.
get the full IMAP path of an selected email in Apple Mail.app with the accountname as prefix. Display result as dialog with option to copy the text to the clipboard
(*
Applescript:
Get_IMAP_account_subfolder_path.scpt
V 1.0.0 Copyright © 2009-2024 ACSR Industrialdesign. 20240108 17:21:17 acsr Armin Carl Stroß-Radschinski
You may incorporate this code into your program(s) without
restriction. This ACSR sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this ACSR sample code as "ACSR sample code" after having
made changes. If you're going to redistribute the code, we require that
you make it clear that the code was descended from ACSR sample code,
but that you've made changes.
*)
(*
This script takes the information from a selected mail item in
apple mail and concatenates the names of the containing IMAP folder hierachy
and the mail.app display account name (not the mailaddress!)
to a POSX style path notation as text.
Remark: There is no current exception handling at all
e.g. if you are using POP or Local Accounts.
*)
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "Keine eMail ausgewählt" message "Wählen Sie eine eMail aus bevor das Script starten!"
else
set theMessage to item 1 of selectedMessages
set theMailbox to the mailbox of theMessage
set theAccountName to name of account of theMailbox
set theMailboxName to name of the theMailbox
set theMessageFolderPath to theMailboxName
-- Initialize theParentMailbox to theMailbox
set theParentMailbox to container of theMailbox
set theParentMailboxName to name of theParentMailbox
-- Iterate through parent mailboxes recursively
repeat while theParentMailboxName is not equal to theAccountName
set theMessageFolderPath to theParentMailboxName & "/" & theMessageFolderPath
set theParentMailbox to container of theParentMailbox
set theParentMailboxName to name of theParentMailbox
end repeat
display dialog theAccountName & "/" & theMessageFolderPath buttons {"to clipboard!", "OK"} default button 2
if the button returned of the result is "to clipboard!" then
set the clipboard to theAccountName & "/" & theMessageFolderPath as rich text
end if
end if
end tell
@acsr
Copy link
Author

acsr commented Jan 8, 2024

Warning: Actually the current script does not work with local folders and drops an uncatched error „account of mailbox “[path]" cannot be read.“ But in fact, when the script is run from the Script Editor, the error message actually contains the full path to the local mailbox. This may be enough for frequent use cases to figure out the location of a mail in the Apple Mail search results (which is our main purpose: troubleshooting orphaned mails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment