Saturday, October 3, 2009

Mail.app compose script, my first AppleScript experience!

How can I get to do automation mail composing like FreeBSD and Thunderbird doing? This weekend I have tried to make auto mail composing script by AppleScript. This is my first AppleScript experience! How do you feel of following script? It looks pretty good ;-)
#!/usr/bin/osascript

on run argv
 set argc to count item of argv
 
 repeat with i from 1 to argc
  set targetlist to textToList(item i of argv, "=")
  set target to item 1 of targetlist
  set value to item 2 of targetlist
  
  if target is "to" then
   set mailto to textToList(value, ",")
  else if target is "cc" then
   set mailcc to textToList(value, ",")
  else if target is "sender" then
   set sender to value
  else if target is "subject" then
   set subject to value
  else if target is "attach" then
   set attach to value
  else if target is "body" then
   set body to value
  end if
 end repeat
 
 return newMail(mailto, mailcc, sender, subject, attach, body) of me
end run

on newMail(mailto, mailcc, sender, subject, attach, body)
 global newMail_mailto
 global newMail_mailcc
 global newMail_sender
 global newMail_subject
 global newMail_attach
 global newMail_body
 
 set newMail_mailto to mailto
 set newMail_mailcc to mailcc
 set newMail_sender to sender
 set newMail_subject to subject
 set newMail_attach to attach
 set newMail_body to body
 
 tell application "Mail"
  set composeWindow to make new outgoing message
  set visible of composeWindow to true
  
  tell composeWindow
   set sender to newMail_sender
   set subject to newMail_subject
   set content to newMail_body
   
   set limit to count item of newMail_mailto
   repeat with i from 1 to limit
    make new to recipient with properties {address:item i of newMail_mailto}
   end repeat
   
   set limit to count item of newMail_mailcc
   repeat with i from 1 to limit
    make new cc recipient with properties {address:item i of newMail_mailcc}
   end repeat
   
   make new attachment with properties {file name:newMail_attach} at after last paragraph
  end tell
  
  (* Signature should be added after attachment treatment complete.
  If signature is added before attachment work complete, attachment
  treatment overwrites signature config and it turns into "none" signature. 
  I guess it's a bug of Mail.app. Following delay command is workaround of 
  this issue. *)
  delay 1.5
  set message signature of composeWindow to signature newMail_sender
 end tell
end newMail

on textToList(target, delimiters)
 set resultlist to {}
 
 set delimiters_org to text item delimiters of AppleScript
 set text item delimiters of AppleScript to delimiters
 set limit to count text item of target
 repeat with i from 1 to limit
  set end of resultlist to i
  set item i of resultlist to text item i of target
 end repeat
 set text item delimiters of AppleScript to delimiters_org
 return resultlist
end textToList
License is 2-term BSD License.

No comments: