Thursday, April 17, 2014

#1 2011-07-14 05:08:45 pm

pickup
Member
Registered: 2009-09-22
Posts: 83

iCal and Date picker - Shane Staley's NS to AS

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.

Applescript:




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.

Thank you!

Offline

 

#2 2011-07-15 12:39:35 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3371

Re: iCal and Date picker - Shane Staley's NS to AS

I think you meant to post next door. Nonetheless, you have this:


Applescript:

set changeDate to (theASDate as text)
set theDate to date string of changeDate

The first line makes changeDate a string, and you can't ask a string for its "date string". You need:


Applescript:

set changeDate to (theASDate as text)
set theDate to date string of theASDate

But your code won't get any further than that, because then you're trying to treat theData as a date, when it's a string.

I can't see the need for these two lines at all -- you need to deal with theASDate.

And really, you can save yourself a lot of code if you use Myriad Helpers to do the NSDate<->AS date conversions.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2011-07-15 10:21:38 am

pickup
Member
Registered: 2009-09-22
Posts: 83

Re: iCal and Date picker - Shane Staley's NS to AS

Hi Shane,
Sorry to be in the wrong forum and will be happy to have it moved.  This sort of smile 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.

Applescript:


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:

Applescript:


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!

Offline

 

#4 2011-07-15 05:53:29 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3371

Re: iCal and Date picker - Shane Staley's NS to AS

You're changing variable names mid-script. You need to use either "theASDate" or "now" throughout.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2011-07-16 10:56:05 am

pickup
Member
Registered: 2009-09-22
Posts: 83

Re: iCal and Date picker - Shane Staley's NS to AS

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, smile  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:

Applescript:


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:


Applescript:


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



Offline

 

#6 2011-07-16 04:41:11 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3371

Re: iCal and Date picker - Shane Staley's NS to AS

Applescript:

set myDate to date "Friday, June 17, 2011 5:00:00 PM"

That doesn't work -- read the second chapter on AppleScriptObjC Gotchas.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2011-07-16 11:07:03 pm

pickup
Member
Registered: 2009-09-22
Posts: 83

Re: iCal and Date picker - Shane Staley's NS to AS

Hi Shane,
Thank you, so what would be the best way to create events and alarms in iCal with Xcode?

Thanks again,

Offline

 

#8 2011-07-16 11:44:48 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3371

Re: iCal and Date picker - Shane Staley's NS to AS

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2011-07-17 07:37:02 pm

pickup
Member
Registered: 2009-09-22
Posts: 83

Re: iCal and Date picker - Shane Staley's NS to AS

I gotcha, thank you.  I will work on that part.

Thanks again,

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.054 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)