Mac APP OC 安装Ruby Package

解决 Mac APP 使用Ruby的框架,需要调用ruby安装gem 命令

调用方式

1
[FZScriptExecute scriptAdminExecute:YES scriptPath:@"" argvments:@[@"gem install xcodeproj"] output:&outputStr error:&errorStr];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
具体
+ (BOOL)scriptAdminExecute:(BOOL)isAdmin scriptPath:(NSString *)scriptPath argvments:(NSArray *)argvments output:(NSString **)outputStr error:(NSString **)errorStr {
NSString *argvmentsStr = [argvments componentsJoinedByString:@" "];
NSString *isAdminPre = @"";
if (isAdmin) {
isAdminPre = @"with administrator privileges";
}
NSString *scriptPathAndArgv = [NSString stringWithFormat:@"%@ %@",scriptPath,argvmentsStr];
NSDictionary *errorInfo = [NSDictionary dictionary];
NSString *executeScript = [NSString stringWithFormat:@"do shell script \"%@\" %@", scriptPathAndArgv,isAdminPre];
// NSLog(@"execute script = %@",executeScript);
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:executeScript];
NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];
if (!eventResult)
{
if ([errorInfo valueForKey:NSAppleScriptErrorNumber])
{
NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber];
if ([errorNumber intValue] == -128)
*errorStr = @"The administrator password is required to do this.";
}
// Set error message from provided message
if (*errorStr == nil)
{

if ([errorInfo valueForKey:NSAppleScriptErrorMessage])
*errorStr = (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage];
}
return NO;
}
else
{
// Set output to the AppleScript's output
*outputStr = [eventResult stringValue];
return YES;
}

}