On Mar 4, 2015, at 11:48 PM, Shane Stanley < email@hidden> wrote:
When I run this code in an un-saved script file, I get an empty dialog. You said I'd need to gran access to Calendars in System Preferences, but Script Editor doesn't appear in the list of apps that have requested calendar data. In fact, I see no requests at all. I've not yet used ObjC calls in a script, so I'm probably missing an obvious step. My first thought is that I must first save the file as a certain type, but I'm not sure what.
I don't think the problem is at your end. You could try running a normal Calendar.app script first to try to force the issue, but I recall having a similar problem. Of course, once an app's in there, it's hard to re-create the situation. Maybe someone will pipe in with a way to force an app to be added to the Calendars list. OK, I saved it as an applet and ran it, and I got the question. So at least it shouldn't be a problem when you distribute it, although you'll need to lock it (and it will fail first time). Meanwhile you should probably add a check for the length() of theCalendars -- if it's 0, you didn't get access. (I write this stuff in my own app, so I probably see slightly different behavior.)
A few really weird things here. :) Thanks again for pointing me to EventKit; the ability to use Cocoa in scripts seems to not be fully in my brain yet. :)
First, the length() property always gives me an error. I've tried it with and without the parentheses, but it fails either way.
Next, I did get the script to run. I switched to a different calendar name, and suddenly got an upcoming event. Best of all, the event is a repeating one set a long time ago, so I know that part is working perfectly! The problem is, as I try to modify your example so I can iterate over all the events returned, I can't access the properties of any of them. Here's the snippet, which is in place of your lines joining all the titles:
set theEvents to (theEKEventStore's eventsMatchingPredicate:thePred)
repeat with e in theEvents display dialog (e's valueForKey:"title") & ". Starts on " & (e's valueForKey:"startDate") end repeat
The script log is empty, and the error just says: Can’t make «class ocid» id «data optr000000000038C30080600000» into type list, record or text.
I've also tried:
repeat with e in theEvents's items
but am told that "Can’t get every item of «class ocid» id «data optr0000000000A0A30080600000»."
I suspect it doesn't like me iterating, meaning the NSArray isn't being given to AS in a way it understands. I'm guessing, though, and my hypothesis makes little sense. If AS and Cocoa can work together, data types would be transparent between them, right?
The other odd thing is that this suddenly started working today, once I re-checked my calendar titles (yeah, my mistake there--it helps to use calendars that, um, have upcoming events). I was never prompted to allow access to Calendars, though, or given any security warnings of any kind. I'd rather avoid distributing this as an app or applet if I could, but since I'm not being shown any security warnings, I can't say how using a script will go over on others' machines. I just can't figure out why I was never asked to grant access to Calendars. Again, I run into the odd lack of traffic on this topic when I search about it.
This is relatively new stuff, the documentation isn't always brilliant, and unless you're using my app, the lack of feedback can make writing some ASObjC harder than it should be. So it's unlikely a lot of people have tried it. -- Shane Stanley < email@hidden> < www.macosxautomation.com/applescript/apps/>
--
Have a great day, Alex Hall
|