所以我有一个 Apple Script 正在运行我的程序的一个功能,如下所示:
[ NSThread detachNewThreadSelector:@selector(runAppleScriptTask)
toTarget:self
withObject:nil];
使用这种方法:
-(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];
}
我有一个NSArray
(充满状态)需要传递给 Apple 脚本。现在我将文件转储到 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");
}
我可以通过 System Events plist Infrastructure 在 Apple Script 中轻松获取它,但我真的更愿意在 RAM 中完成这一切。现在我想我可以使用这里提到的属性语法,http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html但我需要它在 10.5、10.6 和 10.7 上工作所以我不能使用任何尚未发布的东西。NSArray
关于将完整或NSDicitonary
对象传递给我的 Apple Script(这将成为 Apple Script 中的列表)的基于内存的巧妙方式有什么想法吗?
如果有帮助,这是文件删除方法的 Apple Script 代码
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