Objective-C:如何使用管理员权限删除多个文件?

时间:2016-02-02 11:45:49

标签: objective-c applescript-objc

我正在尝试使用Apple脚本删除多个文件(如下所示),但它无法正常工作并发出以下错误:

  

预期表达但发现未知令牌。

这是我的代码:

{
    ///this string will be a contact of all the paths saperated by ' '
    NSString* removingLocationInString = @"";
    ///for loop in order to concat all the string(paths) in one string
    for (NSString* str in locations) {

        removingLocationInString = [NSString stringWithFormat:@"%@ \"%@\"", removingLocationInString, str];
    }

    ///creating the command that will be run from apple script
    ///e.g. rm "~/user/vikas/desktop/file.txt" "~/user/vikas/desktop/file2.txt"
    NSString* command = [NSString stringWithFormat:@"rm %@", removingLocationInString];


    [self runScriptAsAdmin:command];

}

-(BOOL)runScriptAsAdmin:(NSString*) fullScript
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-c" ,
                          [NSString stringWithFormat:@"%@", fullScript],
                          nil];


    NSString * output = nil;
    NSString * processErrorDescription = nil;



    NSDictionary *errorInfo = [NSDictionary new];
    NSString *script =  [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];

    NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
    NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];

    // Check errorInfo
    if (! eventResult)
    {
        // Describe common errors
        NSString *errorDescription = nil;
        if ([errorInfo valueForKey:NSAppleScriptErrorNumber])
        {
            NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber];
            if ([errorNumber intValue] == -128)
                errorDescription = @"The administrator password is required to do this.";
        }

        // Set error message from provided message
        if (errorDescription == nil)
        {
            if ([errorInfo valueForKey:NSAppleScriptErrorMessage])
                errorDescription =  (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage];
        }

        return NO;
    }
    else
    {
        // Set output to the AppleScript's output
        NSString *output = [eventResult stringValue];
        return YES;

    }

    return NO;
}

以下是正在生成的脚本

  

做shell脚本“rm”/Users/vikas/.Trash/.DS_Store“   “/Users/vikas/.Trash/SimpleCocoaBrowser 2.zip”   “/Users/vikas/.Trash/SimpleCocoaBrowser 3.zip”   “/Users/vikas/.Trash/SimpleCocoaBrowser 4 4.55.07 pm.zip”   “/Users/vikas/.Trash/SimpleCocoaBrowser 4.zip”“与管理员   特权

1 个答案:

答案 0 :(得分:2)

id name subject marks 1 xyz a1 12 2 xyz1 a2 22 3 xyz a3 33 4 xyz1 a4 11 5 xyz a5 55 6 xyz1 a6 10 函数中添加以下行:

runScriptAsAdmin

实际的苹果脚本应该如下:

  

做shell脚本" rm \" /Users/vikas/.Trash/SimpleCocoaBrowser 2.zip \"   " /Users/vikas/.Trash/SimpleCocoaBrowser 3.zip \"   \" /Users/vikas/.Trash/SimpleCocoaBrowser 4.zip \""与管理员   特权