So I have an Apple Script that is running one of the functions of my program like so:
[ NSThread detachNewThreadSelector:@selector(runAppleScriptTask)
toTarget:self
withObject:nil];
Using this method:
-(void)runAppleScriptTask
{
mainBundle = [NSBundle bundleForClass:[self class]];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
NSString *scriptPath = [[NSBundle mainBundle] pathForResource: @"AttemptToRepair"
ofType: @"scpt"];
NSLog(@"Found AppleScript Path:%@",scriptPath);
// Run the Apple Script
NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath: scriptPath]
error:&errorDict];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
NSLog(@"Return Discriptor,%@",returnDescriptor);
NSString *returnValue = @"User Canceled";
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
if ([ returnDescriptor stringValue]) {
returnValue = [ returnDescriptor stringValue];
[ returnDict setValue:returnValue forKey:@"returnValue"];
}
else {
if (errorDict) {
returnValue = [ returnDescriptor stringValue];
[ returnDict setValue:errorDict forKey:@"errorDict"];
}
}
NSLog(@"Found Return Value: %@",returnValue);
[scriptObject release];
// Notify
[[NSNotificationCenter defaultCenter]
postNotificationName:AttemptToRepairCompleteNotification
object:self
userInfo:returnDict];
[pool drain];
}
I have a NSArray
(Full of Statuses) that I need to pass to the Apple Script. Right now I am dumping the file to a plist:
// File Drop the Global Status Array
BOOL gsaWroteSuccess = [ issueFile writeToFile:@"/private/tmp/gsa.plist" atomically:YES];
if (gsaWroteSuccess) {
NSLog(@"Wrote the current Global Status Array to file");
// Let objects know the Global Status is being updated
NSMutableDictionary *globalStatusUpdate = [[NSMutableDictionary alloc] init];
// Pass the mutated Data to our NSTable
[ globalStatusUpdate setValue:issueFile forKey:@"globalStatusArray"];
[[NSNotificationCenter defaultCenter]
postNotificationName:StatusUpdateNotification
object:self
userInfo:globalStatusUpdate];
}
else {
NSLog(@"Unable to write Global Status Array to file");
}
Which I can easily pick back up in the Apple Script via System Events plist infrastructure , but I would really rather do this all in RAM. Now I think I could use the property syntax mentioned here , http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html but I need this to work on 10.5,10.6 and 10.7 so I can't use anything that was not released yet. Any thoughts on a slick in memory based way to pass an NSArray
full or NSDicitonary
objects to my Apple Script (which will become a list in the Apple Script)?
Here is the Apple Script code for the file drop methodology right now if it helps
script AttemptToRepair
property parent : class "NSObject"
activate
set thePListPath to POSIX path of "/tmp/gsa.plist"
tell application "System Events"
set the plist_path to "/tmp/gsa.plist"
set the plist_file to property list file plist_path
set itemNodes to property list items of property list item "globalStatusArray" of plist_file
repeat with i from 1 to number of items in itemNodes
set itemNode to item i of itemNodes
set discription to value of property list item "discription" of itemNode
set metric to value of property list item "metric" of itemNode
set reason to value of property list item "reason" of itemNode
set status to value of property list item "status" of itemNode
display dialog "discription:" & discription & return & ¬
"metric:" & metric & return & ¬
"reason:" & reason & return & ¬
"status:" & status
end repeat
end tell
end script
run AttemptToRepair