(* $Id: OpenExmhMessageWithAppleMail.applescript,v 1.10 2007/07/25 17:02:13 baaden Exp $ (c) 2007, Marc Baaden , http://baaden.free.fr/dev The purpose of this script is according to its name to open an email message from the EXMH email program with the Apple Mail.app software. This script accepts the path to an email message as argument. It expects this to be a message from mh/nmh/exmh and will write it into a temporary mailbox directory, move it to the Mail.app folder "exmh" and open it. I stole a whole lot of code from the Burst digest messages.scpt - Version 1.1 script available at http://homepage.mac.com/aamann/Misc_Scripts.html. So tons of credit should go to Andreas Amann http://homepage.mac.com/aamann/ Andy Fragen Some of the more interesting bits that I added is - delete the ("white") import folder - open the message in mail It's one of my first applescripts, so it's probably quite ugly and messy. Sorry. TODOs: - add some safety checks - make sure the opened message is *not* considered as spam and images are loaded Copyright Marc Baaden baaden@smplinux.de http://baaden.free.fr This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info". As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license, users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability. In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions as regards security. The fact that you are presently reading this means that you have had knowledge of the CeCILL license and that you accept its terms. *) on run argv -- exit if running on anything prior to 10.4.0 if ((get system attribute "sysv") < 4160) then -- 4160 = 0x1040 is Sytem version 10.4.0 (Tiger) display dialog "This script requires MacOS X 10.4 or higher." buttons "OK" default button 1 with icon caution return end if -- ********************** -- VARIABLES AND SETTINGS -- ********************** -- which is the message we are supposed to open? set exmhMessageFile to item 1 of argv set exmhFolderName to "exmh" -- Get message to be read from this path (my exmh tcl script put it there) set MboxPath to "/tmp/" & exmhFolderName & ".mbox" -- make sure we have the folder as required set importCount to 0 -- ************** -- COPY FROM EXMH -- ************** -- remove anything left from the last run of the script so we start out clean. Ignore errors in case no old data is foundÉ try do shell script "rm -fr " & MboxPath end try do shell script "mkdir -p " & MboxPath do shell script "cp " & exmhMessageFile & " " & MboxPath & "/mbox" --return "Trying: cp " & exmhMessageFile & " " & MboxPath & "/mbox" -- ************** -- MESSAGE IMPORT -- ************** -- TODO: we could make some safety checks -- the import folder stuff is adapted from Burst digest messages.scpt by Andreas Amann -- get list of top-level folders whose name starts with "Import" so we can figure out where the new messages were imported to set theOriginalImportFolders to getImportFolders() tell application "Mail" to import Mail mailbox at POSIX path of MboxPath -- get the list of Import folders again to figure out which one we just created set theNewImportFolders to getImportFolders() if (count of theNewImportFolders) ­ (count of theOriginalImportFolders) then -- we have a new import folder so the import probably succeeded repeat with eachFolder in theNewImportFolders -- figure out the name of the new import folder if eachFolder is not in theOriginalImportFolders then exit repeat end repeat -- wait for Mail to be done importing/indexing the new messages before we mess with them waitForMail() tell application "Mail" if exists mailbox (eachFolder & "/" & exmhFolderName) then -- we actually managed to get it imported tooÉ tell application "Mail" to move messages of mailbox (eachFolder & "/" & exmhFolderName) to mailbox exmhFolderName -- wait for Mail to finish moving the messages before deleting their orignal mailbox my waitForMail() tell application "Mail" to delete mailbox (eachFolder & "/" & exmhFolderName) -- wait for Mail to finish deleting the child mailbox before going on to delete the root mailbox my waitForMail() set importCount to importCount + 1 end if end tell -- clean up after us deleteWhiteMailbox(eachFolder) end if -- number of import folders has changed - import probably succeeded if importCount = 0 then display dialog "The import seems to have failed for some reason." buttons "OK" default button 1 with icon caution -- clean up the /tmp folder - we shouldn't be leaving user data in a global areaÉ try do shell script "rm -fr " & MboxPath end try -- ************** -- MESSAGE VIEWER -- ************** -- now let us see the newly imported message tell application "Mail" set themailbox to mailbox exmhFolderName as list tell front message viewer set selected mailboxes to themailbox set sort column to date sent column set selected messages to {} if exists (first message whose read status is false) then set current_message to first message whose read status is false --set background color of current_message to none open current_message end if end tell --waitForMail() --activate window 2 activate end tell end run -- ************** -- SUBROUTINES -- ************** -- ok, some heavy workaround here to delete a white mailbox -- see http://www.macworld.com/weblogs/macosxhints/2006/11/mailcolor/index.php on deleteWhiteMailbox(mboxName) do shell script "rm -rf ~/Library/Mail/Mailboxes/" & mboxName tell application "Mail" to make with properties {name:mboxName} new mailbox tell application "Mail" to delete mailbox mboxName end deleteWhiteMailbox -- from Burst digest messages.scpt by Andreas Amann on getImportFolders() set AppleScript's text item delimiters to " " set theImportFolderList to text items of (do shell script "cd ~/Library/Mail/Mailboxes;echo Import*") set AppleScript's text item delimiters to "" if theImportFolderList = {"Import*"} then set theImportFolderList to {} return theImportFolderList end getImportFolders -- from Burst digest messages.scpt by Andreas Amann on waitForMail() tell application "Mail" repeat while background activity count > 0 delay 1 end repeat end tell end waitForMail