PDA

View Full Version : Script turning sender defined emails into iCal events (almost there).




TVPost
Feb 18, 2009, 09:19 PM
User "lee1210"gets the credit for the following script. Given that it is almost what I need, I thought I'd make a new post to finish the script.

Need: Plug in the email address of a particular sender and from all emails from that sender create one event for each email. The script below works, but only for the first email it encounters.

I have 6 email accounts, so I need all my inboxes and out boxes searched by sender or recipient for this script. I suspect only one of my inbox accounts is being searched as well.

LASTLY (and not addressed in the script below) I need the event to hyperlink to the email that geneerated the event or, ideally, create a pdf, in a designated folder on he desktop of the email that created the event (perhaps putting the date and sender as the document name) and link the event to that.

Again, thank you "lee1210" for doing what no one else could thus far. If Lee or anyone else can finish this script, I'd be grateful.

P

Lee's script:



display dialog "Address to generate events for: " default answer "user@domain.com"
set diagRet to result
set searchString to text returned of diagRet
tell application "Mail"
set myInbox to mailbox "INBOX" of account 1
set myMessages to every message of myInbox where sender is searchString

repeat with theMessage in myMessages
set alt to sender of theMessage
set sdr to sender of theMessage
set dt to date received of theMessage
set sm to id of theMessage
set sub to subject of theMessage
tell application "iCal"
set myCal to calendar "Test"
make new event at the end of events of myCal with properties {summary:"Email from " & sdr & ": " & sub, description:sm as text, start date:dt, end date:dt}
display dialog ("Creating " & dt)
end tell
end repeat
end tell



lee1210
Feb 19, 2009, 09:21 PM
display dialog "Address to generate events for: " default answer "user@domain.com"
set diagRet to result
set searchString to text returned of diagRet
tell application "Mail"
set accts to (every account of application "Mail")
repeat with acct in accts
set mbxs to (every mailbox in acct)
repeat with mbx in mbxs
set myMessages to (every message of mbx where sender is searchString)

repeat with theMessage in myMessages
set alt to sender of theMessage
set sdr to sender of theMessage
set dt to date received of theMessage
set sm to id of theMessage
set sub to subject of theMessage
tell application "iCal"
set myCal to calendar "Test"
make new event at the end of events of myCal with properties {summary:"Email from " & sdr & ": " & sub, description:sm as text, start date:dt, end date:dt}
end tell
end repeat
end repeat
end repeat
end tell


I have no idea about the link. That may be a 10.5 thing only. Also, creating a PDF i have no idea on doing that. It would be trivial to grab the unique id that i stuck into the description, and use that to tell mail to open the event, but it's no link.

-Lee

TVPost
Feb 20, 2009, 12:30 AM
Thank's Lee

I tried this with three different email addresses with confirmed mailboxes and got the same result 1 or no results in iCal.

Can you plug this script into script editor and see if it works for you? Maybe I'm entering something somewhere wrong, but the only thing I'm chaning in your script is he name of the mailbox and the email address.

If it works for you, then it is likely I should be entering soething without quotes or the persons name, or something like that?

Thanks,

P

lee1210
Feb 20, 2009, 09:29 AM
I certainly tried it on my machine and it worked properly. I wouldn't post it without trying it out. =)

The most recent version goes through every mailbox of every account, so you shouldn't need to change the mailbox name. When i was entering addresses, it was without quotes... just
user@domain.com
with the relevant information plugged in, and it seemed to work fine.

There's a possibility that there were changes in 10.5 that altered the dictionary for Mail or iCal. I can't really do much about that, as I don't have a leopard machine handy.

-Lee

TVPost
Feb 20, 2009, 11:10 PM
Lee, thanks for all the help. I just can't get this to work comprehensively. It does create a random event, but not comprehensive. I've tried tweaking your work to see if I can just get an event for every email but no go. I don't know if I'm entering the email with or without quotes properly, the target calendar should be different or what. But if anyone wants to spell out exactly (with examples) how to plug in the info to this script, I'd appreciate it.

For the sake of example, say I want to plug in the email address pete@home.com and I want all events to go into a calendar labelled "Pete emails".

What would the script look like with those two variables plugged in?

Thanks again all

lee1210
Feb 21, 2009, 12:25 PM
display dialog "Address to generate events for: " default answer "user@domain.com"
set diagRet to result
set searchString to text returned of diagRet
tell application "iCal"
set theCalendarNumber to (make calendar at end of calendars with properties {title:searchString})
end tell
tell application "Mail"
set accts to (every account of application "Mail")
repeat with acct in accts
set mbxs to (every mailbox in acct)
repeat with mbx in mbxs
set myMessages to (every message of mbx where sender is searchString)

repeat with theMessage in myMessages
set alt to sender of theMessage
set sdr to sender of theMessage
set dt to date received of theMessage
set sm to id of theMessage
set sub to subject of theMessage
tell application "iCal"
set myCal to theCalendarNumber
make new event at the end of events of myCal with properties {summary:"Email from " & sdr & ": " & sub, description:sm as text, start date:dt, end date:dt}
end tell
end repeat
end repeat
end repeat
end tell


If you run this (i just did so from script editor) you will get a dialog asking for an email address. Enter one. It should create a calendar with that email address as the title. It should then find any email in any mailbox of any account in mail from that address, and create events for each. If this isn't working i guess i'd say start adding:
display dialog ("I am here")

within each repeat, etc. and see what's going on.

-Lee

TVPost
Feb 21, 2009, 03:40 PM
I tried your suggestion. Still the same result, although this time it still only creates one email. I can't see the pattern either. The other odd thing. The "I am here" parks itself on an email from the proper sender, but NOT the email that created an event.

If you'd like to email me at toss2006@dslextreme.com, I'll send you my ichat info and perhaps I can solicit your input in real time?

Thanks a lot for ALL your help.

Thanks,

P

TVPost
Feb 21, 2009, 03:50 PM
correction toss2007