Created
October 25, 2010 20:28
-
-
Save nilswloka/645688 to your computer and use it in GitHub Desktop.
Create message link for org-mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Puts org-mode links to selected mails into clipboard and moves selected mails to archive | |
-- Replace {MAILBOX} and {ACCOUNT} below | |
tell application "Mail" | |
set theLinks to "" | |
set theSelection to selection | |
repeat with aMessage in theSelection | |
set theId to ((content of header "Message-Id" of aMessage) as text) | |
set theSubject to ((subject of aMessage) as text) | |
set theSender to sender of aMessage | |
set theLinks to (theLinks & (createLinkTo(theId, theSender, theSubject) of me)) | |
move aMessage to mailbox "{MAILBOX}" of account "{ACCOUNT}" | |
end repeat | |
set the clipboard to theLinks | |
end tell | |
-- Create link to a message | |
-- theId is the message id to be linked | |
-- theSender is the sender of the message | |
-- theSubject is the display text for the org-mode link | |
on createLinkTo(theId, theSender, theSubject) | |
return "[[message://" & onlyIdIn(theId) & "][Mail from " & withoutSquareBrackets(theSender) & ": " & withoutSquareBrackets(theSubject) & "]] " | |
end createLinkTo | |
-- Returns its argument with "[" and "]" replaced by "|" | |
-- aText is a text that might contain offending characters | |
on withoutSquareBrackets(aText) | |
set AppleScript's text item delimiters to "[" | |
set theTextItems to text items of aText | |
set AppleScript's text item delimiters to "|" | |
set theResult to theTextItems as string | |
set AppleScript's text item delimiters to "]" | |
set theTextItems to text items of theResult | |
set AppleScript's text item delimiters to "|" | |
set theResult to theTextItems as string | |
set AppleScript's text item delimiters to {""} | |
return theResult | |
end withoutSquareBrackets | |
-- Returns the message id without leading < and trailing > | |
-- aMessageId is a message id with the format <someId> | |
on onlyIdIn(aMessageId) | |
return text 2 thru ((length of aMessageId) - 1) of aMessageId | |
end onlyIdIn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with Emacs org-mode and Mail.app to create links to messages. This is my first attempt at writing AppleScript, feel free to comment and correct.