Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created August 31, 2011 15:03
Show Gist options
  • Save iloveitaly/1183766 to your computer and use it in GitHub Desktop.
Save iloveitaly/1183766 to your computer and use it in GitHub Desktop.
Lion Applescript Mail.applescript
-- Mail.applescript
-- iCal
-- !!! Note that this script is not compiled by iCal.xcodeproj, which uses a compiled version instead
-- !!! so changes to this file will not be reflected in iCal behavior unless manually recompiled.
-- (Not compiled because of <rdar://problem/3675416> osacompile fails in a chrooted environment)
on show_mail_sbrs(subjectLine, messageText, myrecipients)
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
repeat with i from (count of myrecipients) to 1 by -1
tell mymail to make new to recipient at beginning of to recipients with properties {address:(item i of myrecipients)}
end repeat
set visible of mymail to true
activate
end tell
end show_mail_sbrs
on show_mail_sbr(subjectLine, messageText, myrecipient, myrecipientname)
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail to make new to recipient at beginning of to recipients with properties {address:myrecipient, name:myrecipientname}
set visible of mymail to true
activate
end tell
end show_mail_sbr
on send_mail_sb(subjectLine, messageText)
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
set visible of mymail to true
activate
end tell
end send_mail_sb
on send_mail_sbr(subjectLine, messageText, myrecipient, myrecipientname)
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail to make new to recipient at beginning of to recipients with properties {address:myrecipient, name:myrecipientname}
send mymail
end tell
end send_mail_sbr
on send_mail_sbrp(subjectLine, messageText, myrecipient, myrecipientname, invitationPath)
set pfile to POSIX file invitationPath
set myfile to pfile as alias
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail to make new to recipient at beginning of to recipients with properties {address:myrecipient, name:myrecipientname}
tell mymail
tell content
make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
end tell
end tell
send mymail
end tell
end send_mail_sbrp
on send_mail_sbp(subjectLine, messageText, invitationPath)
set pfile to POSIX file invitationPath
set myfile to pfile as alias
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail
tell content
make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
end tell
end tell
set visible of mymail to true
activate
end tell
end send_mail_sbp
-- Outlook 2003 is still not RFC 2445 compliant, so this script strips out the VTIMEZONE and TZID portions of the the .ics file that confuse Outlook (causing, for example, the "Gregorian"/"Lunar" import error in Outlook 2003)
-- I've restricted the script to recipients in a specific domain because I don't want to affect notifications to potential non-Outlook recipients. Feel free to change or remove that logic to fit your particular needs.
-- Note that the invitation times will only be accurate for Outlook users in the same time zone as the sender. This is because Outlook doesn't know how to handle time zones (it expects GMT (i.e. Zulu) times). Ideally, this script would translate all times to GMT, but that's more than I need right now, so the script just strips out all time zone information, and Outlook assumes the imported times refer to local time.
-- In Terminal, open /Applications/iCal.app/Contents/Resources/Mail.scpt
-- ## Remember to back up your original Mail.scpt file!
-- Replace the send_mail_sbrp function with the following. Keep all other functions as they are.
-- Mail.scpt hack for Outlook compatibility - Bryan's version
on send_mail_sbr(subjectLine, messageText, myrecipient)
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail to make new to recipient at beginning of to recipients with properties {address:myrecipient}
send mymail
end tell
end send_mail_sbr
-- Mail.scpt hack for Outlook compatibility - Bryan's version
on send_mail_sbrp(subjectLine, messageText, myrecipient, myrecipientname, invitationPath)
set pfile to POSIX file invitationPath
set myfile to pfile as alias
try
-- if appointment going to Thy, then parse for outlook
-- define a carriage return
set cr to (ASCII character 13) & (ASCII character 10)
-- open and read the ical event file to insert into an e-mail
set myEventFileHandle to ¬
open for access myfile without write permission
set myEventFileContent to read myEventFileHandle
close myEventFileHandle
-- remove the timezone info from the calendar event
-- to make compatable with outlook
set toss to false
set newEventFileContent to ""
set last_line to false
repeat with theLine in paragraphs of myEventFileContent
if theLine contains "BEGIN:VTIMEZONE" then
set toss to true
else if theLine contains "END:VTIMEZONE" then
set last_line to true
set toss to false
end if
if toss is false then
if last_line is true then
set last_line to false
else
if theLine contains ";TZID=US/Pacific" then
set theLine to searchReplace(theLine, ";TZID=US/Pacific", "")
end if
set newEventFileContent to newEventFileContent & theLine & cr
end if
end if
end repeat
-- create a random event file name
set aliasTempMail to "/tmp/" & (random number from 1 to 1000000) & ".ics"
-- write the new event to a temp file
set myEventFileHandle to ¬
open for access (POSIX file aliasTempMail as string) with write permission
write newEventFileContent starting at 1 to myEventFileHandle as «class utf8»
close myEventFileHandle
set pfile to POSIX file aliasTempMail
set myfile to pfile as alias
-- display dialog "ready for Outlook"
on error errMsg
display dialog errMsg
end try
try
tell application "Mail"
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail to make new to recipient at beginning of to recipients with properties {address:myrecipient}
tell mymail
tell content
make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
end tell
end tell
send mymail
end tell
on error errMsg
display dialog errMsg
end try
end send_mail_sbrp
to searchReplace(thisText, searchTerm, replacement)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment