Linking to a Contact from iCal

Jump to First Reply
Posted:
in Genius Bar edited January 2014
I am trying to set up a phone call in iCal. I'd like the alarm to go off ten minutes beforehand and open up the card for a specific person so I have their phone number there rather than having to look for it.



How do I link to the card file for him? I am sure this is easy, but I'm not finding it.

Comments

  • Reply 1 of 8
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by varian72

    I am trying to set up a phone call in iCal. I'd like the alarm to go off ten minutes beforehand and open up the card for a specific person so I have their phone number there rather than having to look for it.



    How do I link to the card file for him? I am sure this is easy, but I'm not finding it.




    1. You mean the person's card in the Address Book.app?

    2. Where is the person's name stored that you want the card for? Is it in iCal somewhere, or do you want to hard-code it?
     0Likes 0Dislikes 0Informatives
  • Reply 2 of 8
    varian72varian72 Posts: 36member
    Quote:

    Originally posted by lundy

    1. You mean the person's card in the Address Book.app?

    2. Where is the person's name stored that you want the card for? Is it in iCal somewhere, or do you want to hard-code it?




    Yes, I mean the card in Address Book.app. The person's name is stored in Address Book. I don't want to hard code it....I want to simply attach the file from within the alarm function in iCal.
     0Likes 0Dislikes 0Informatives
  • Reply 3 of 8
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by varian72

    Yes, I mean the card in Address Book.app. The person's name is stored in Address Book. I don't want to hard code it....I want to simply attach the file from within the alarm function in iCal.



    I'm working on it - so the idea is to match the "attendee" name in an iCal event to a Person Name in Address Book? At first glance it is giving me trouble, but I will consult MacScripter and see what they have to offer.
     0Likes 0Dislikes 0Informatives
  • Reply 4 of 8
    lundylundy Posts: 4,466member
    OK - let me first say that Apple's iCal team needs to do some work, LOL. This is the hardest thing I have had to script in quite a while. It turns out that the iCal AppleScript dictionary doesn't provide any way to know what event triggered the script. So we have to use a heuristic approach, and assume that the event that has the start time closest to the current time was the one that called our script. We need to know which event called us so that we can get the name of the attendee for that event and look him up in the Address Book.



    Here it is: copy and paste into Script Editor, save into ~/Library/Scripts, and then set it as the Alarm action in the Event that you want it to call.



    Code:




    property calendarName : "Home"

    tell application "iCal"

    set thisCalendar to first calendar whose title is calendarName

    set theEvents to (every event of thisCalendar)

    set theStartDates to (start date of every event of thisCalendar)

    set currDate to (current date)

    set todaysDateString to date string of currDate

    set closest to currDate - (date "Thursday, January 1, 1970 12:00:00 AM")

    repeat with i from 1 to count theStartDates

    set thisStartDate to item i of theStartDates

    set thisEvent to item i of theEvents

    set thisCloseness to currDate - thisStartDate

    if thisCloseness is not less than 0 then -- not in the future

    if thisCloseness is less than closest then

    set closest to thisCloseness

    set closestEvent to thisEvent

    end if

    end if

    end repeat

    set theName to (display name of first attendee of closestEvent)

    end tell





    tell application "Address Book"

    try

    set ABFound to (first person whose name is theName)

    set selection to ABFound

    activate

    on error

    display dialog "Name of Attendee for Alarm not found in the Address Book."

    end try

    end tell









    NOTES:

    - the attendee name MUST be identical to what it is in the Address Book. Just use the popup in iCal to set the attendee from the Address Book.



    - in this version, the script will run when the event Start Time happens. There is a bug in iCal that leaves all the Trigger Times blank. If this is an issue, I can kludge up my own Trigger Time by subtracting the Trigger Interval from the Start Time of the event.



    - Before saving the script, replace the calendar name "Home" in the first line with the name of your calendar.
     0Likes 0Dislikes 0Informatives
  • Reply 5 of 8
    lundylundy Posts: 4,466member
    Here is the final version that takes into account that two events whose start times are the same may have different alarm times due to the "ten minutes before" etc. (the "trigger interval").



    Code:




    property calendarName : "Home"

    tell application "iCal"

    set thisCalendar to first calendar whose title is calendarName

    set theEvents to (every event of thisCalendar)

    set theStartDates to (start date of every event of thisCalendar)

    set currDate to (current date)

    set todaysDateString to date string of currDate

    set closest to currDate - (date "Thursday, January 1, 1970 12:00:00 AM")

    set thisTriggerInterval to 0

    repeat with i from 1 to count theStartDates

    set thisStartDate to item i of theStartDates

    set thisEvent to item i of theEvents

    try -- see if event has a trigger interval, and if so, save it

    set thisTriggerInterval to trigger interval of first open file alarm of thisEvent

    end try

    set thisCloseness to currDate - thisStartDate - thisTriggerInterval

    set thisTriggerInterval to 0

    if thisCloseness is not less than 0 then -- not in the future

    if thisCloseness is less than closest then

    set closest to thisCloseness

    set closestEvent to thisEvent

    end if

    end if

    end repeat

    set theName to (display name of first attendee of closestEvent)

    end tell





    tell application "Address Book"

    try

    set ABFound to (first person whose name is theName)

    set selection to ABFound

    activate

    on error

    display dialog "Name of Attendee for Alarm not found in the Address Book."

    end try

    end tell









    There is no way to solve the prefix/suffix/middle name problem. Just type the Attendee into iCal exactly as it appears in the Address Book (e.g., "Meyer Paul MD", not just "Meyer Paul"), and it will find it.
     0Likes 0Dislikes 0Informatives
  • Reply 6 of 8
    regreg Posts: 832member
    Quote:

    Nice script. I tend to use the email feature already in iCal but this is something that should also already be in there.



    I never liked iCal. It just doesn't seem obvious to me how to use the thing. I don't understand why a ToDo item can't be a telephone call complete with the person to call, etc. I don't understand why all the calendars show through each other. I don't understand why the only choices you have for an Event is either All Day or a specific time. You would think that it would always pull all the contact info from Address Book for ToDos and Events that include a Person, but not only does it not do that automatically, as this script shows, you can't even script it because it won't tell you what Event called your script!!



    Quote:

    That said, the script only calls the first of all the attendees.



    That's more of an AppleScript syntax thing than anything else. When you use a construct like "name of every person whose first name is 'Dubya'", then it returns a list, even if there is only one item in the list. So you have to use the construct "first xxx" or "item 1 of xxx" to coerce it to not be a list - otherwise you get an error that it can't make it into a reference, which is true. For setting the selection in Address Book, a list may work, but a single-item list usually won't work for many other apps. I don't know why AppleScript returns a list when there is only one item.

    Quote:

    This is because it is opening address book and that will only show one contact at a time. What I would find most helpful would be a new window opening that has:

    1. Event name and time

    2. A list of attendees with their phone number and email



    The reason I think a new window would be good is because if ical is hidden, the address book will open and the card will show but no reason. That and it only showing one attendee make having a new window necessary.



    I suppose we could have the script put a message into the "Note:" field of the person's Address Book card that had the details of the event. What I really wanted to do was figure out how to access the "Large Type" menu item of the phone number field and have that showing when the card was displayed. I think the only way to do it is with GUI scripting though.

     0Likes 0Dislikes 0Informatives
  • Reply 7 of 8
    lundylundy Posts: 4,466member
    WUPS -



    Reg, I hit the wrong button when trying to reply to your post. My sincere apologies.
     0Likes 0Dislikes 0Informatives
  • Reply 8 of 8
    regreg Posts: 832member
    No problem. I have only done a little bit of scripting and then usually find that someone else has done what I want and has done it better with more functions. The best part of iCal for me is it syncing with my phone, treo 650. All the calendars are combined and all of the events for the day are displayed. I think it has always been that way for palm products. I agree that iCal is limited with what can be done with it so...... I'll fire off a feed back to apple suggesting they include some of the ideas in a future release. I had also tried the macosxhints but that only did "To Do" items. Still I and almost everyone I deal with uses email so this is not a major problem or hassle.



    Again nice script.



    reg
     0Likes 0Dislikes 0Informatives
Sign In or Register to comment.