Objective-C: 如何删除具有管理员权限的多个文件?
Objective-C: How to remove multiple files with admin privileges?
我正在尝试使用 apple 脚本(如下所示)删除多个文件,但它不起作用并出现以下错误:
Expected expression but found unknown token.
这是我的代码:
{
///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;
}
这是正在生成的脚本
do shell script "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"" with administrator
privileges
在 runScriptAsAdmin
函数中添加以下行:
/// if there are " in script then then make it \"
fullScript = [fullScript stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];
实际的apple脚本应该是这样的:
do shell script "rm \"/Users/vikas/.Trash/SimpleCocoaBrowser 2.zip\"
\"/Users/vikas/.Trash/SimpleCocoaBrowser 3.zip\"
\"/Users/vikas/.Trash/SimpleCocoaBrowser 4.zip\"" with administrator
privileges
我正在尝试使用 apple 脚本(如下所示)删除多个文件,但它不起作用并出现以下错误:
Expected expression but found unknown token.
这是我的代码:
{
///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;
}
这是正在生成的脚本
do shell script "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"" with administrator privileges
在 runScriptAsAdmin
函数中添加以下行:
/// if there are " in script then then make it \"
fullScript = [fullScript stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];
实际的apple脚本应该是这样的:
do shell script "rm \"/Users/vikas/.Trash/SimpleCocoaBrowser 2.zip\" \"/Users/vikas/.Trash/SimpleCocoaBrowser 3.zip\" \"/Users/vikas/.Trash/SimpleCocoaBrowser 4.zip\"" with administrator privileges