I have a little script I have been working on to plan out my student’s day.
My wish is that I can get this to first ask which student and places the event in that students calendar in iCal, then ask which class (1-6 and are hopefully tied to a specific time frame, class 1 is from 8:00am to 9:00am, class 2 is from 9:00am to 10:00am and so on). I am having a hard time trying to easily enter the date without the time and then having what ever the user chooses as the class number decide the time frame for the event. I would also like to check to see if there is already an event at the selected time and prompt the user that there is already something scheduled for that class.
Here is what I have so far…
set theCals to {}
set iCalendars to {}
set theDate to current date
set myDate to date string of theDate
set time of theDate to 0
set mainList to {"Student1", "Student2"}
set callList to {"Class 1", "Class 2", "Class 3", "Class 4", "Class 5", "Class 6"}
set userInput to display dialog "What is due?" & return & return default answer "" with icon note
if button returned of userInput is "OK" then
set theSummary to text returned of userInput
end if
set userInput to display dialog "What note added to event in calendar?" & return & return default answer "" with icon note
if button returned of userInput is "OK" then
set theDescrip to text returned of userInput
end if
set userInput to display dialog "Enter date & time for event" default answer ((current date) as string)
if button returned of userInput is "OK" then
set theDate to date (text returned of userInput)
end if
tell application "iCal"
set theCals to calendars whose writable is true
repeat with i from 1 to count of theCals
copy title of item i of theCals to end of iCalendars
end repeat
end tell
set theChoice to choose from list mainList with prompt "Choose student"
if theChoice is not false then
set theCalChoice to item 1 of theChoice
end if
set theCall to choose from list callList with prompt "Choose class"
if theCall is not false then
set theCallChoice to item 1 of theCall
end if
-- Run through our Calendar list and find the iCal id of the Stock Quotes Calendar
set i to 1
repeat with anItem in iCalendars
if item i of iCalendars is equal to theCalChoice then
set theNum to i
exit repeat
end if
set i to i + 1
end repeat
set currentCal to item theNum of theCals
tell application "iCal"
make event at end of events of currentCal with properties {start date:theDate, summary:theSummary}
summary:theSummary, description:theDescrip}
end tell
Model: MacBook Pro 15
AppleScript: 2.3 (118)
Browser: Safari 534.52.7
Operating System: Mac OS X (10.7)
set mainList to {"Student1", "Student2"}
set callList to {"Class 1", "Class 2", "Class 3", "Class 4", "Class 5", "Class 6"}
set {text returned:theSummary} to display dialog "What is due?" & return & return default answer "" with icon note
set {text returned:theDescrip} to display dialog "What note added to event in calendar?" & return & return default answer "" with icon note
repeat
try
set shortDateString to short date string of (current date)
set {text returned:theDate} to display dialog "Enter date for event (format " & shortDateString & ")" default answer shortDateString
set asDate to date theDate
exit repeat
on error number n
if n = -128 then return -- user cancelled
end try
end repeat
set theChoice to choose from list mainList with prompt "Choose student"
if theChoice is false then return
set theCalChoice to item 1 of theChoice
set theCall to choose from list callList with prompt "Choose class"
if theCall is false then return
set theCallChoice to item 1 of theCall
set classNumber to last word of theCallChoice as integer
set classStartDate to asDate + ((7 + classNumber) * hours)
tell application "iCal"
try
set currentCal to calendar theCalChoice
set allEventsStartDate to start date of events of currentCal
if classStartDate is in allEventsStartDate then
display dialog "Event at " & (classStartDate as text) & " already exists" buttons {"Cancel"} default button 1
else
make new event at end of events of currentCal with properties {start date:classStartDate, end date:classStartDate + hours, summary:theSummary}
--summary:theSummary, description:theDescrip}
end if
on error e number n
if n = -128 then return
display dialog "an error occured: " & e
end try
end tell
the date format uses the current international date format settings.
The script assumes that the number of the class in callList is separated by a space character. display dialog throws an error “User cancelled” if the user presses Cancel.
This causes the script to terminate
property mainList : {"Student1", "Student2"}
property callList : {"Class 1", "Class 2", "Class 3", "Class 4", "Class 5", "Class 6"}
property summerCallList : {"Class 1", "Class 2", "Class 3", "Class 4"}
property summerSchedule : {{30600, 1}, {36000, 4}, {43200, 3}, {54000, 2}}
set isSummer to ((month of (current date)) as integer) is in {6, 7, 8, 9}
set {text returned:theSummary} to display dialog "What is due?" & return & return default answer "" with icon note
set {text returned:theDescrip} to display dialog "What note added to event in calendar?" & return & return default answer "" with icon note
repeat
try
set shortDateString to short date string of (current date)
set {text returned:theDate} to display dialog "Enter date for event (format " & shortDateString & ")" default answer shortDateString
set asDate to date theDate
exit repeat
on error number n
if n = -128 then return -- user cancelled
end try
end repeat
set theChoice to choose from list mainList with prompt "Choose student"
if theChoice is false then return
set theCalChoice to item 1 of theChoice
if isSummer then
set classList to summerCallList
else
set classList to callList
end if
set theCall to choose from list classList with prompt "Choose class"
if theCall is false then return
set theCallChoice to item 1 of theCall
set classNumber to last word of theCallChoice as integer
if isSummer then
set {startTime, duration} to item classNumber of summerSchedule
set classStartDate to asDate + startTime
else
set classStartDate to asDate + ((7 + classNumber) * hours)
set duration to 1
end if
tell application "iCal"
try
set currentCal to calendar theCalChoice
set allEventsStartDate to start date of events of currentCal
if classStartDate is in allEventsStartDate then
display dialog "Event at " & (classStartDate as text) & " already exists" buttons {"Cancel"} default button 1
else
make new event at end of events of currentCal with properties {start date:classStartDate, end date:classStartDate + (duration * hours), summary:theSummary}
--summary:theSummary, description:theDescrip}
end if
on error e number n
if n = -128 then return
display dialog "an error occured: " & e
end try
end tell