-- Apply darcs patch from selectd mail(s) body -- MacAvity Software, 2005 -- full darcs path property pDarcs : "/usr/local/bin/darcs" -- options for the darcs apply command property pDarcsApplyOptions : "" -- this is the location where temporary files will be created property pTemporaryFilePath : "/tmp/" -- entry point tell application "Mail" set theSelection to the selection if the length of theSelection is less than 1 then display dialog "One or more messages must be selected." end if repeat with theMessage in theSelection set theSubject to "Apply '" & (subject of theMessage) & "'?" set isOK to button returned of (display dialog theSubject buttons {"OK", "Skip"} default button "OK") if isOK is "OK" then set theFile to my saveMessageFile(theMessage) tell me to applyDarcsPatch(theFile) tell me to deleteFile(theFile) end if end repeat end tell -- save the message as a file in the temporary directory -- its name will be the message subject on saveMessageFile(theMessage) using terms from application "Mail" set theSubject to subject of theMessage set theContent to content of theMessage end using terms from set theFileName to my makeFileName(theSubject) set theFileRef to open for access (POSIX file theFileName) with write permission write theContent to theFileRef as string close access theFileRef return theFileName end saveMessageFile -- apply darcs patch on applyDarcsPatch(fileName) try set theProject to choose folder with prompt "Pick a project" set theCmd to "cd " & quoted form of (POSIX path of theProject) & "; cat " & (quoted form of fileName) & "|" & pDarcs & " apply " & pDarcsApplyOptions -- display dialog theCmd display dialog (do shell script theCmd) end try end applyDarcsPatch -- clean the filaname and append it to the temporary path on makeFileName(baseName) set baseName to my cleanFileName(baseName) set theName to pTemporaryFilePath & baseName return theName end makeFileName -- get rid of problematic characters in the filename -- todo - should probably also truncate if necessary on cleanFileName(theName) set cleanedName to "" repeat with theChar in every character of theName if (theChar as string = "/" or theChar as string = ":") then set theChar to "-" set cleanedName to cleanedName & theChar end repeat return cleanedName end cleanFileName -- delete the specific file (POSIX) on deleteFile(theFile) -- can't find a standard extension to delete a file, so this unfortunately requires Finder tell application "Finder" to delete (POSIX file theFile) as string end deleteFile