将填充了NSDictionaries的NSArray传递给NSAppleScript

时间:2011-08-25 20:42:54

标签: nsarray nsdictionary scripting-bridge applescript-objc osascript

所以我有一个Apple脚本正在运行我的程序的一个功能:

[ 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];
}

我需要传递给Apple脚本的NSArray(Full of Statuses)。现在我将文件转储到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基础架构轻松地在Apple Script中备份,但我真的宁愿在RAM中完成这一切。现在我想我可以使用这里提到的属性语法http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html但是我需要这个来处理10.5,10.6和10.7所以我不能使用任何尚未发布的东西。有关基于内存的光滑方式的想法,将NSArray完整或NSDicitonary个对象传递给我的Apple脚本(将成为Apple脚本中的列表)?

如果有帮助,这里是文件删除方法的Apple脚本代码

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

0 个答案:

没有答案