Sorry to ask again help, but I do not understand why I get an error and why the alarm is always set to 30 minutes before instead of what comes from the dialog buttons.
Can someone help?
set defCalendar to "My calendar"
--global ICactive
set theDate to short date string of (current date)
--Get event Summary
display dialog "Enter event name:" default answer " Appointment"
set eName to text returned of the result
--Get date
display dialog "Enter event date:" default answer (theDate as text)
set eDate to text returned of the result
--get time
display dialog "Enter begin time:" default answer "18:00"
set eTime to text returned of the result
set eStart to date (eDate & space & eTime)
set theDateEnd to short date string of (current date)
--Get event Summary
--Get date
display dialog "Enter end date:" default answer (theDateEnd as text)
set eDateEnd to text returned of the result
--get time
display dialog "Enter end time:" default answer "18:00"
set eTimeEnd to text returned of the result
set eEnd to date (eDateEnd & space & eTimeEnd)
--get location
display dialog "Which calendar? (Home, Work, ...)" default answer defCalendar
set theCal to text returned of the result
--Set alarm
display dialog "Display alarm how much time before?" default answer "7" buttons {"hours", "days", "minutes"} default button 2
set {text returned:theText, button returned:theButton} to the result
if theButton is "days" then
try
set alarmTime to (theText as number) * -60 * 24
on error
set alarmTime to 0
end try
else
if theButton is "hours" then
try
set alarmTime to (theText as number) * -60
on error
set alarmTime to 0
end try
else
if theButton is "minutes" then
try
set alarmTime to (theText as number)
on error
set alarmTime to 0
end try
end if
end if
end if
--Set alarm 2
display dialog "Display second alarm how much time before?" default answer "7" buttons {"hours", "days", "minutes"} default button 3
set {text returned:theText, button returned:theButton} to the result
if theButton is "days" then
try
set alarmTime2 to (theText as number) * -60 * 24
end try
else
if theButton is "hours" then
try
set alarmTime2 to (theText as number) * -60
end try
else
if theButton is "minutes" then
try
set alarmTime2 to (theText as number)
end try
end if
end if
end if
tell application "Calendar"
activate
try
set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
-- if alarmTime 0 then make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound name:"Basso"}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime2}
on error
--set myCals to name of every calendar whose writable is true
--set theCal to (choose from list myCals with prompt "Attach to which Calendar?" without multiple selections allowed and empty selection allowed) as text
--set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
display dialog "Error"
end try
--if we opened iCal, we'll close it
--if not ICactive then quit
end tell
tell application "Calendar"
activate
try
set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
-- if alarmTime 0 then make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound name:"Basso"}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime2}
on error
--set myCals to name of every calendar whose writable is true
--set theCal to (choose from list myCals with prompt "Attach to which Calendar?" without multiple selections allowed and empty selection allowed) as text
--set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
display dialog "Error"
end try
try -- ADDED
close calendar theCal with saving -- ADDED
-- or -- ADDED
--save calendar theCal -- ADDED
end try -- ADDED
--if we opened iCal, we'll close it
--if not ICactive then quit
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 19 juin 2020 18:49:43
I found the problem, it was coming from the third “else” if theButton is “minutes”, I just replace by “else”
set defCalendar to "Work"
set theDate to short date string of (current date)
--Get event Summary
display dialog "Enter event name:" default answer " Appointment"
set eName to text returned of the result
--Get date
display dialog "Enter event date:" default answer (theDate as text)
set eDate to text returned of the result
--get time
display dialog "Enter begin time:" default answer "18:00"
set eTime to text returned of the result
set eStart to date (eDate & space & eTime)
set theDateEnd to short date string of (current date)
--Get event Summary
--Get date
display dialog "Enter end date:" default answer (theDateEnd as text)
set eDateEnd to text returned of the result
--get time
display dialog "Enter end time:" default answer "19:00"
set eTimeEnd to text returned of the result
set eEnd to date (eDateEnd & space & eTimeEnd)
--get location
display dialog "Which calendar? (Home, Work, ...)" default answer defCalendar
set theCal to text returned of the result
--Set alarm
display dialog "Display alarm how much time before?" default answer "7" buttons {"minutes", "hours", "days"} default button 2
set {text returned:theText, button returned:theButton} to the result
if theButton is "days" then
try
set alarmTime to (theText as number) * -60 * 24
end try
else
if theButton is "hours" then
try
set alarmTime to (theText as number) * -60
end try
else
try
set alarmTime to (-theText as number)
end try
end if
end if
--Set alarm 2
display dialog "Display second alarm how much time before?" default answer "7" buttons {"minutes", "hours", "days"} default button 3
set {text returned:theText2, button returned:theButton2} to the result
if theButton2 is "days" then
try
set alarmTime2 to (theText2 as number) * -60 * 24
end try
else
if theButton2 is "hours" then
try
set alarmTime2 to (theText2 as number) * -60
end try
else
try
set alarmTime2 to (-theText2 as number)
end try
end if
end if
tell application "Calendar"
activate
try
set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound name:"Sosumi"}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime2, sound name:"Sosumi"}
end try
try -- ADDED
close calendar theCal with saving -- ADDED
-- or -- ADDED
-- save calendar theCal -- ADDED
end try
end tell
The script above works (even if we don’t close and save)
Thank you for the feedback.
I didn’t thought that there may be a problem in the code defining the needed parameters.
Here is a version using a cleaner code for the first part.
Just by curiosity I disabled the instruction close calendar theCal with saving and enabled the instruction save calendar theCal.
set defCalendar to "Work"
set theDate to short date string of (current date)
--Get event Summary
display dialog "Enter event name:" default answer " Appointment"
set eName to text returned of result
--Get date
display dialog "Enter event date:" default answer theDate -- theDate IS a string !
set eDate to text returned of result
--get time
display dialog "Enter begin time:" default answer "18:00"
set eTime to text returned of result
set eStart to date (eDate & space & eTime)
set theDateEnd to short date string of (current date)
--Get event Summary
--Get date
display dialog "Enter end date:" default answer theDateEnd -- theDateEnd IS a string !
set eDateEnd to text returned of result
--get time
display dialog "Enter end time:" default answer "19:00"
set eTimeEnd to text returned of result
set eEnd to date (eDateEnd & space & eTimeEnd)
--get location
display dialog "Which calendar? (Home, Work, ...)" default answer defCalendar
set theCal to text returned of result
--Set alarm
display dialog "Display alarm how much time before?" default answer "7" buttons {"minutes", "hours", "days"} default button 2
set {text returned:theText, button returned:theButton} to result
try
set theNumber to theText as number
if theButton is "days" then
set alarmTime to theNumber * -60 * 24
else if theButton is "hours" then
set alarmTime to theNumber * -60
else -- if theButton is "minutes" then
set alarmTime to theNumber
end if
on error
set alarmTime to 0 -- in case of error it wasn't defined
end try
--Set alarm 2
display dialog "Display second alarm how much time before?" default answer "7" buttons {"minutes", "hours", "days"} default button 3
set {text returned:theText2, button returned:theButton2} to result
try
set theNumber2 to theText2 as number
if theButton2 is "days" then
set alarmTime2 to theNumber2 * -60 * 24
else if theButton2 is "hours" then
set alarmTime2 to theNumber2 * -60
else -- if theButton2 is "minutes" then
set alarmTime2 to theNumber2
end if
on error
set alarmTime2 to 0 -- in case of error it wasn't defined
end try
tell application "Calendar"
activate
try
set newEvent to make new event at end of events of calendar theCal with properties {summary:eName, start date:eStart, end date:eEnd}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime, sound name:"Sosumi"}
make new sound alarm at end of sound alarms of newEvent with properties {trigger interval:alarmTime2, sound name:"Sosumi"}
end try
try -- ADDED
-- close calendar theCal with saving -- ADDED
-- or -- ADDED
save calendar theCal -- ADDED
end try
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 20 juin 2020 12:13:32