I have searched all over with no avail. I need to know how to have applescript call up iCal and if today is listed as an all day event (example Christmas Day) or any events for the next week labeled as all ady events. How can I do this simply?
Hopefully this will get you started.
tell application "iCal"
launch
set todaysList to {}
set comingList to {}
set currentDate to current date
set yesterday to currentDate - 1 * days
set oneWeekOut to currentDate + 7 * days
repeat with thisCalendar in calendars
get (events of thisCalendar whose allday event is true)
repeat with thisEvent in result
set eventStart to start date of thisEvent
if (eventStart < currentDate) and (eventStart > yesterday) then
set todaysList's end to summary of thisEvent
else if (eventStart > currentDate) and (eventStart < oneWeekOut) then
set comingList's end to summary of thisEvent
end if
end repeat
end repeat
quit
end tell
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 13}
set todaysList to todaysList as Unicode text
set comingList to comingList as Unicode text
set AppleScript's text item delimiters to ASTID
if todaysList is not "" then
display dialog ("Today's Events:" & return & return & todaysList) buttons {"OK"} default button 1
end if
if comingList is not "" then
display dialog ("Coming Events:" & return & return & comingList) buttons {"OK"} default button 1
end if
Side note: Check out Apple’s iCal Library.
OK, now if I have 2 all day events scheduled. Say a friends birthday and Christmas how can I add the phrase “and” between the events?
I see you have two threads running, but I’ll put both of my contributions here. I have a calendar called Birthdays for my extended family, and I have one called Medical/Dental for those and similar appointments. These scripts use Growl to announce the results, but you could just as well drop out the Growl stuff and use display dialog.
I should warn you that I wrote both of these when I was just learning AppleScript, so they are neither optimal nor short. Apologies for the long post.
for Medical/Dental Appointments Stored in their own Calendar so I don’t have to sort them out from other things:
property Author : "Adam Bell"
-- This script shows upcoming Medical or Dental appointments.
-- The script uses Growl to display the result.
set today to current date
-- Build the Medical List "MAppt" {name, date, name, date, ...}
tell application "iCal"
close window 1
set MCals to every calendar whose title contains "Medical"
set mCal to item 1 of MCals
set toGo to {}
-- Collect the date/name list as mdCal
set mCount to count of events of mCal
set mdCal to {}
repeat with n from 1 to mCount
-- Get summary and start date of each
set thisApt to {summary of event n of mCal, start date of event n of mCal}
if ((item 2 of thisApt) - today) > 0 then
set mdCal to mdCal & thisApt
end if
end repeat
-- quit
end tell
-- Sort mdCal by date
set newList to {}
set eventCount to (count of mdCal) / 2
repeat (eventCount - 1) times
set LowItem to Closest(mdCal)
set end of newList to item (LowItem - 1) of mdCal
set end of newList to item LowItem of mdCal
set mdCal to Rem(mdCal, newList)
end repeat
set end of newList to item 1 of mdCal
set end of newList to item 2 of mdCal
set mdCal to newList
-- set up the Growl notification
set r to return
set sp to space
set m to ASCII character 240
set msg to ""
repeat with k from 2 to count of mdCal by 2
set msg to (msg & r & m & sp & sp & item (k - 1) of mdCal & " on " & item k of mdCal as string) & r
end repeat
Growl_It("Medical & Dental Appointments", msg)
--tell application "iCal" to quit
-- Handlers --
on Growl_It(gTitle, gMessage)
tell application "GrowlHelperApp"
notify with name "MedDent" title gTitle description gMessage application name "MiCal" icon of application "iCal" with sticky
end tell
end Growl_It
----
on Closest(ItemsAndDates)
set lowDate to date "Sunday, January 1, 1950 12:00:00 AM"
set idx to 0
repeat with i from 2 to the count of ItemsAndDates by 2
set thisDate to item i of the ItemsAndDates
if the lowDate is date "Sunday, January 1, 1950 12:00:00 AM" then
set lowDate to thisDate
set idx to i
else if thisDate < lowDate then
set lowDate to item i of ItemsAndDates
set idx to i
end if
end repeat
return idx
end Closest
----
on Rem(OrigList, TakeOut)
-- Set up Properties
script theProp
property Original : OrigList
property inList : {}
property OutList : TakeOut
end script
-- Do the removal
repeat with i from 1 to count of (theProp's Original)
set anyItem to ((theProp's Original)'s item i)
if anyItem is in theProp's OutList then
else
set end of theProp's inList to anyItem
end if
end repeat
return theProp's inList
end Rem
And for family birthdates, in their own Calendar, always stored as single all-day events:
property Author : "Adam Bell"
-- This script calculates the days remaining before the next four birthdays in an iCal calendar named "Birthdays".
-- Each birthday is entered as an all day event
-- The script uses Growl to display the result.
-- Get today's date with time set to midnight. Later days to go subtractions must have a time of day in common or differences can be off by one day.
set today to current date
set time of today to 0 -- seconds after midnight
-- Get the Birthday List "bDays" {name, date, name, date, ...}, then correct for GMT, then subtract today after correcting year to get { name, daysToGo, name, daysToGo, ...}
tell application "iCal"
close window 1
set bdCals to every calendar whose title contains "Birthdays"
set bCal to item 1 of bdCals
set toGo to {}
-- Collect the date/name list as bCal
set bCount to count of events of bCal
repeat with n from 1 to bCount
-- Start date is the birthday for an all day event.
-- Summary should be the person's name.
-- iCal stores times of events in GMT even though it presents them in local time. They must be shifted back to a midnight base before days to go are calculated.
set thisDay to {summary of event n of bCal, start date of event n of bCal}
set thisTime to item 2 of thisDay
if time of thisTime = 75600 then -- 9:00 PM if ADST
set time of thisTime to (time of thisTime) + 10800
else -- 8:00 PM if AST
set time of thisTime to (time of thisTime) + 14400
end if
-- adjust the calendar year of the birthday ahead of now
repeat until (thisTime - today) > 0
set year of thisTime to (year of thisTime) + 1
end repeat
set daysLeft to ((thisTime - today) / days) as integer
set item 2 of thisDay to daysLeft
set toGo to toGo & thisDay
end repeat
--quit
end tell
set msgs to nearest_4(toGo, bCount)
set msg_C to " days until "
set AllBDNotes to ""
set r to return
set rr to r & r
set sp to space
repeat with mm from 1 to 4
set msg_A to (item 1 of (item mm of msgs)) as text
set msg_B to (item 2 of (item mm of msgs))
-- add possessives for names: soAndso's Birthday
if last character of msg_B = "s" then
set msg_B to msg_B & "'"
else
set msg_B to msg_B & "'s"
end if
set BDNote to msg_A & msg_C & msg_B
set AllBDNotes to AllBDNotes & BDNote & rr
end repeat
Growl_It("Birthdays Coming Soon", AllBDNotes)
-- Handlers --
on Growl_It(gTitle, gMessage)
tell application "GrowlHelperApp"
notify with name "Next4BD" title gTitle description (return & gMessage) application name "Birthdays" with sticky
end tell
end Growl_It
----
on nearest_4(values_list, entries)
set theNearest4 to {{}, {}, {}, {}}
set scratchList to values_list
repeat with m from 1 to 4
set the low_amount to 0
set itemNum to 0
-- sort by disances to go ignoring names
repeat with i from 2 to entries by 2
set this_item to item i of the scratchList
if the low_amount = 0 then
set the low_amount to this_item
set itemNum to i
else if this_item < low_amount then
set the low_amount to item i of scratchList
set itemNum to i
end if
end repeat
-- add new entry to result and get the name to go with it
set item m of theNearest4 to {low_amount, item (itemNum - 1) of scratchList}
set newList to {}
if itemNum = 2 then
repeat with jj from 3 to count of scratchList
newList = newList & item jj of scratchList
end repeat
else
repeat with kk from 1 to itemNum - 2
set newList to newList & item kk of scratchList
end repeat
repeat with nn from itemNum + 1 to count of scratchList
set newList to newList & item nn of scratchList
end repeat
end if
set scratchList to newList
end repeat
return theNearest4
end nearest_4
Thank you, Adam. I too am learning Applescript and am fortunate to have such a wonderful forum to help me learn.
In case this is more than an exercise, please try OmniGrowl on version tracker or macupdate. thanks.