Skip to content

Instantly share code, notes, and snippets.

@ccstone
Created June 25, 2013 19:29
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 ccstone/5861581 to your computer and use it in GitHub Desktop.
Save ccstone/5861581 to your computer and use it in GitHub Desktop.
This script will find the selected email message with Spotlight and open it in BBEdit for editing.<p> (Change "BBEdit" to "TextWrangler" to use the free version of BBEdit instead.)
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <junksieve@thestoneforge.com>
# dCre: 2012-07-18 : 05:51
# dMod: 2013-06-25 : 14:26
# Appl: Mail & BBEdit
# Task: Open selected message's file in BBEdit for editing.
# Deps: Uses only OSX components.
# Test: Lion & Mountain Lion (might work in Snow Leopard but I cannot test).
# Tags: @Applescript, @Mail, @BBEdit, @Edit, @Message
-------------------------------------------------------------------------------------------
--» #### { NOTES } ########################################################################
-------------------------------------------------------------------------------------------
(*
TO-DO:
• Fix Spotlight Query.
+ Do 2nd query if 1st query fails.
*)
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on REPL(_str, _find, _repl)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, _find}
set _str to text items of _str
set AppleScript's text item delimiters to _repl
set _str to _str as text
set AppleScript's text item delimiters to oldTIDS
return _str
end REPL
-------------------------------------------------------------------------------------------
on textToBBEdit(_data)
if _data starts with "/" then
try
set _data to alias POSIX file _data
end try
end if
tell application "BBEdit"
activate
if class of _data = alias then
open _data
else if class of _data = text then
set newDoc to make new document with properties {text:_data}
end if
set bounds of front window to {303, 44, 1617, 1196}
end tell
end textToBBEdit
-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------
try
set AppleScript's text item delimiters to {""}
tell application "Mail"
set _sel to selection
if length of _sel = 1 then
set _msg to item 1 of _sel
else
error "Too many messages selected."
end if
# Use account to find mail-folder in attempt to be compatible back to Snow Leopard.
set acnt1Dir to (account directory of account 1) as string
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set mailFldrLion to POSIX path of (text items 1 thru -3 of acnt1Dir as string)
set AppleScript's text item delimiters to oldTIDS
tell _msg
set mbxName to name of its mailbox
set mID to its id
set mSub to its subject
end tell
end tell
# Escape single quotes in subject for spotlight query string.
if mSub contains "'" then
set mSub to REPL(mSub, "'", "'\"'\"'")
end if
# Find email
set _cmd to "mdfind -onlyin " & mailFldrLion & " 'kMDItemDisplayName == \"" & mSub & "\"cd && kMDItemFSName == \"" & mID & ".emlx\"cd'"
--» •••• Debug Code ••••
# textToBBEdit(_cmd)
# return
set emlFile to do shell script _cmd
if emlFile ≠ "" then
textToBBEdit(emlFile)
else
error "No message was returned by Spotlight!"
end if
on error e number n
set e to e & return & return & "Num: " & n
tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
if button returned of dDlg = "Copy" then set the clipboard to e
end try
-------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment