Hi All,
I purchased Shane’s Book on AppleScript Object C Explored and this has been a great book! However, I am running into a problem use the date picker date and coercing it into a useable date for iCal. The code so far looks like this. It is simple because I am trying to test to get it to work.
script iCal_Reminder_1AppDelegate
property parent : class "NSObject"
property thePicker : missing value
on fromNStoAS_(sender)
set theNSDate to thePicker's dateValue()
set theASDate to NSDateToASDate_(theNSDate)
log theASDate as text
--simple scheduling of an iCal event
set changeDate to (theASDate as text)
set theDate to date string of changeDate
set eStart to theDate + 10 * minutes
set eEnd to theDate + 70 * minutes
set eName to "Scripted event - mail alarm"
set calendarName to "Home"
set alarmTime to 0 -- alarm at the exact moment of the event
set alarmTime1 to 2
--THIS IS THE TROUBLE AREA
tell application "iCal"
set newEvent to make new event at end of events of calendar calendarName with properties {summary:eName, start date:eStart, end date:eEnd}
set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime}
end tell
end fromNStoAS_
on NSDateToASDate_(theNSDate)
set theCal to current application's NSCalendar's currentCalendar()
-- build a list of the components we want
set theComponents to ((current application's NSYearCalendarUnit as integer) ¬
+ (current application's NSMonthCalendarUnit as integer) ¬
+ (current application's NSDayCalendarUnit as integer) ¬
+ (current application's NSHourCalendarUnit as integer) ¬
+ (current application's NSMinuteCalendarUnit as integer) ¬
+ (current application's NSSecondCalendarUnit as integer) ¬
)
-- use the calendar to get the components from the date
set theComponents to theCal's components_fromDate_(theComponents, theNSDate)
-- set variables to the various components
tell theComponents
set theYear to |year|()
set theMonth to |month|()
set theDay to |day|()
set theHour to hour()
set theMinute to minute()
set theSecond to |second|()
end tell
-- make an AS date and set the various properties
set theASDate to current date
set day of theASDate to 1
set year of theASDate to theYear
set month of theASDate to theMonth
set day of theASDate to theDay
set hours of theASDate to theHour
set minutes of theASDate to theMinute
set seconds of theASDate to theSecond
return theASDate
end NSDateToASDate_
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Basically, the coercion works, the log gives me a usable date format, but when I try to get use the date or set it to the date, it gives an error:
set changeDate to (theASDate as text)
set theDate to date string of changeDate
2011-07-14 16:40:40.850 iCal-Reminder 1[2509:903] *** -[iCal_Reminder_1AppDelegate fromNStoAS:]: Can’t get date string of “Friday, July 15, 2011 10:00:00 AM”. (error -1728)
I know I have thrown alot of code in here, but basically, after getting the date to AS from the date picker, I try to use is to create an event and it won’t recognize that format as a date.
Hi Shane,
Sorry to be in the wrong forum and will be happy to have it moved. This sort of makes sense. I altered the code a little to see if what I thought you said would work and still ran into the error. Basically, in Applescript, I can program a new event, or alarm in iCal with the following code.
set now to date "Friday, June 17, 2011 5:00:00 PM"
set eStart to now + 10 * minutes
set eEnd to now + 70 * minutes
set eName to "Scripted event - mail alarm"
set calendarName to "Reminders"
set alarmTime to 0 -- alarm at the exact moment of the event
set alarmTime1 to 2
tell application "iCal"
set newEvent to make new event at end of events of calendar calendarName with properties {summary:eName, start date:eStart, end date:eEnd}
set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime}
--adding a second alarm
set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime1}
I am trying to basically take the theASDate and of course, use it as the date in iCal.
The code still is:
on fromNStoAS_(sender)
set theNSDate to thePicker's dateValue()
set theASDate to NSDateToASDate_(theNSDate)
log theASDate as text
--simple scheduling of an iCal event
set now to date theASDate
log "This is theASDate " & theASDate
set eStart to now + 10 * minutes
set eEnd to now + 70 * minutes
set eName to "Scripted event - mail alarm"
set calendarName to "Reminders"
set alarmTime to 0 -- alarm at the exact moment of the event
set alarmTime1 to 2
tell application "iCal"
set newEvent to make new event at end of events of calendar calendarName with properties {summary:eName, start date:eStart, end date:eEnd}
set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime}
--adding a second alarm
set theAlarm to make new mail alarm at end of mail alarms of newEvent with properties {trigger interval:alarmTime1}
end tell
end fromNStoAS_
I am reading up on Myriad Helpers but any help on translating the ASDate into the proper date for the iCal script. Sorry, I am still getting my head around this. Thank you so much!
Hi Shane,
Thank you for that, and that does make sense. I do appreciate your expertise and help, and thank you for your patience as I learn a bit more. In saying that, I have run into the next issue. Basically, it seems that some of the AS classes for iCal don’t seem to work in Xcode. I use this code:
set theStart to 10 as integer
set theEnd to 70 as integer
--set myDate to (current date)
-- set myDate to date "06-17-11 5:00 PM"
set myDate to date "Friday, June 17, 2011 5:00:00 PM"
set eStart to myDate + theStart * minutes
set eEnd to myDate + 70 * minutes
set eName to "Scripted event - mail alarm"
set calendarName to "Home"
-- set alarmTime to 0 -- alarm at the exact moment of the event
-- set alarmTime1 to 2
set theFile to choose file
--log theFile
--This will be where you enter the length of show
display dialog "Please enter the length of show in minutes:" default answer ""
set showLength to text returned of result
display dialog "Please enter how often you would like to run the script in minutes:" default answer ""
set alarmSpacing to text returned of result
set alarmNumber to ((showLength / alarmSpacing) - 1)
-- set alarmNumber to 3 as integer
--set alarmSpacing to 5 as integer
set n to 1
tell application "iCal"
set newEvent to make new event at end of events of calendar calendarName with properties {summary:eName, start date:eStart, end date:eEnd}
repeat while n ≤ alarmNumber
tell newEvent
make new open file alarm at end with properties {trigger interval:n * alarmSpacing, filepath:theFile}
end tell
--This alarm will make a regular alarm with no script
--set theAlarm to make new open file alarm at end of open file alarm of newEvent with properties {trigger interval:n * alarmSpacing}
set n to n + 1
end repeat
end tell
When this is inserted into a _(sender) in Xcode, it fails with the error:
[iCalAppDelegate iCal:]: iCal got an error: Can’t make class «class wrev». (error -2710)
The final code linked to just a test button in Interface builder is:
script iCalAppDelegate
property parent : class "NSObject"
property thePicker : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
on iCal_(sender)
set theStart to 10 as integer
set theEnd to 70 as integer
--set myDate to (current date)
-- set myDate to date "06-17-11 5:00 PM"
set myDate to date "Friday, June 17, 2011 5:00:00 PM"
set eStart to myDate + theStart * minutes
set eEnd to myDate + 70 * minutes
set eName to "Scripted event - mail alarm"
set calendarName to "Home"
-- set alarmTime to 0 -- alarm at the exact moment of the event
-- set alarmTime1 to 2
set theFile to choose file
--log theFile
--This will be where you enter the length of show
display dialog "Please enter the length of show in minutes:" default answer ""
set showLength to text returned of result
display dialog "Please enter how often you would like to run the script in minutes:" default answer ""
set alarmSpacing to text returned of result
set alarmNumber to ((showLength / alarmSpacing) - 1)
-- set alarmNumber to 3 as integer
--set alarmSpacing to 5 as integer
set n to 1
tell application "iCal"
set newEvent to make new event at end of events of calendar calendarName with properties {summary:eName, start date:eStart, end date:eEnd}
repeat while n ≤ alarmNumber
tell newEvent
make new open file alarm at end with properties {trigger interval:n * alarmSpacing, filepath:theFile}
end tell
--This alarm will make a regular alarm with no script
--set theAlarm to make new open file alarm at end of open file alarm of newEvent with properties {trigger interval:n * alarmSpacing}
set n to n + 1
end repeat
end tell
end iCal_
end script
It should be no different than in “normal” AS. You’re problem above is not in creating the events and alarms, but in how you’re trying to create a date. Get that right – you had it right earlier – and the rest should work fine.