How to Integrate applescript in cocoa application.

Hello,
I am making an application where i need all the events available in iCal.
using calendarstore frame work available i can get the events for 4 years only. i need to input start date and end date.
i think the apple script will give me all the events from iCal.
I need script to do following:
it get all the calendar names from iCal first and then it returns me the uniqueId of all the events in one Array.
And This array must be NSArray object.
it is possible to integrate applescript with coca objectice C class.
if Yes then How can i do it ?

i am able to get all the calendar using apple script.
here is my script :

global ICactive
--check if iCal is active
tell application "System Events"
	if exists application process "iCal" then
		set ICactive to true
	else
		set ICactive to false
	end if
end tell
tell application "iCal"
	set myCals to name of every calendar whose writable is true
end tell

Hi,

NSAppleScript can do this

[code]NSAppleScript *script;
NSAppleEventDescriptor *descriptor;
NSDictionary *error;
NSString *errorString;

script = [[NSAppleScript alloc] initWithSource:@“\ntell application "iCal"\n”
“launch\n”
“set myCals to name of every calendar whose writable is true\n”
“set theEvents to {}\n”
“repeat with oneCal in myCals\n”
“set theEvents to theEvents & uid of events of calendar oneCal\n”
“end repeat\n”
“quit\n”
“end tell\n”
“return theEvents\n”];
descriptor = [script executeAndReturnError:&error];
[script release];
if (error == nil){
int count = [descriptor numberOfItems];
int x = 0;
NSMutableArray *eventUIDs = [NSMutableArray arrayWithCapacity:count];
while( x++ < count ) {
[eventUIDs addObject:[[descriptor descriptorAtIndex:x] stringValue]];
}
}
else {
errorString = [error objectForKey:NSAppleScriptErrorMessage];
// do some error handling
}[/code]
the result is in the MSMutableArray eventUIDs

Thank You very much for your reply.

Actually my target is to make an Array of the object that contains the event uid , date stamp(last modified) , and occurance or start date of the event.
So, I need to have an array that have NSMutableDictionary with three key value pair for uid , date stamp and occurance.

i am trying it but i am not getting it.

can you tell me how can i implement this ?

Thanks.

xmax.

try this

[code] NSAppleScript *script;
NSAppleEventDescriptor *descriptor;
NSDictionary *error;
NSString *errorString;
NSMutableArray *resultArray;
NSMutableDictionary *dict;

script = [[NSAppleScript alloc] initWithSource:@"\ntell application \"iCal\"\n"
		  "launch\n"
		  "set myCals to name of every calendar whose writable is true\n"
		  "set theEvents to {}\n"
		  "repeat with oneCal in myCals\n"
		  "repeat with oneEvent in events of calendar oneCal\n"
		  "tell oneEvent\n"
		  "set end of theEvents to uid\n"
		  "set end of theEvents to stamp date as text\n"
		  "set end of theEvents to start date as text\n"
		  "end tell\n"
		  "end repeat\n"
		  "end repeat\n"
		  "quit\n"
		  "end tell\n"
		  "return theEvents\n"];
descriptor = [script executeAndReturnError:&error];
[script release];
if (error == nil){
	int count = [descriptor numberOfItems];
	int x = 0; 
	resultArray = [NSMutableArray arrayWithCapacity:count / 3];
	while( x < count ) {
		dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[[descriptor descriptorAtIndex:x] stringValue], @"UID", 
																	[[descriptor descriptorAtIndex:x+1] stringValue], @"stampDate", 
																	[[descriptor descriptorAtIndex:x+2] stringValue], @"startDate", nil];
		[resultArray addObject:dict];
		[dict release];
		x = x + 3;
	}
//	NSLog(@"%@", resultArray);
}
else {
	 errorString = [error objectForKey:NSAppleScriptErrorMessage];
	// do some error handling
}[/code]

Sorry for asking question which are of the same type ?
but i am not getting how to get it ?

I thought i would made it from the previous suggestions. but i could not.

You have given me the array of the objects with uid, date stamp and its occurence.
Now this array should contain excluded dates as array with key “exceptions”. i am not able to integrate it because the type of excluded dates is list.
So the object would have four key value pairs.

can you tell me how can i implement this ?

Thanks.

Sorry, I have no idea what you mean with “excluded dates”

Excluded dates are the exception dates associated with recurring events.
Apple script provide this property for events when the recurring events has exceptions. these are list of dates on which event has exceptions. There might be a value for this dates or it may be empty if it has no exceptions.

for more detail you can look iCal dictionary in Script Editor (File → Open Dictionary… iCal) you can look at the properties for ‘event’ (iCal suite > event).

Thanks…

xmax.

got it, but I’m afraid this is beyond my skills.
I don’t know how to pass nested lists from AppleScript to Cocoa

I came across this apple project when I was trying to learn to use applescripts in my cocoa projects. So I downloaded it and studied it. I now can pass very complex stuff back-and-forth between cocoa and applescript. It’s a little complicated to figure out but once you do the techniques used in this project can be really useful.

Take a look and see if you it can help you too. The project by itself is cool too.
http://developer.apple.com/samplecode/AttachAScript/

Specifically with your problem, there’s a class in that project called AEDescUtils that converts what applescript is returning back into a cocoa object that you can use in your cocoa code. That’s what will help you return more complicated things back from applescript… you can even add custom methods to that file to return stuff that that class can’t handle as is.