ZGScriptUtils.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // ZGScriptUtils.m
  3. // ZGKeywordOperation
  4. //
  5. // Created by zhuge on 19/6/14.
  6. // Copyright (c) 2014年 com.woodenperon. All rights reserved.
  7. //
  8. #import "ZGScriptUtils.h"
  9. @implementation ZGScriptUtils
  10. +(void)excuteScriptFile:(NSString *)scriptFileName withArgs:(NSArray *)args {
  11. // [NSApp activateIgnoringOtherApps:false];
  12. NSString *script = [NSString stringWithString:scriptFileName];
  13. if ([script hasSuffix:@"scpt"]) {
  14. script = [script stringByDeletingPathExtension];
  15. }
  16. NSString* scpt_path = [[NSBundle mainBundle] pathForResource:script ofType:@"scpt"];
  17. NSArray *cmd = [NSArray arrayWithObject:scpt_path];
  18. NSArray *new = [cmd arrayByAddingObjectsFromArray:args];
  19. // [ZGShellUtils execSync:@"/usr/bin/osascript" args:new];
  20. NSTask *currentTask = [[NSTask alloc] init];
  21. [currentTask setLaunchPath:@"/usr/bin/osascript"];
  22. NSMutableDictionary *environmentDict = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
  23. NSString *path = [environmentDict objectForKey:@"PATH"];
  24. path = [NSString stringWithFormat:@"%@:%@", [[NSBundle mainBundle] pathForResource:@"ApkTools" ofType:nil], path];
  25. [environmentDict setValue:path forKey:@"PATH"];
  26. [environmentDict setValue:@"xterm-256color" forKey:@"TERM"];
  27. // [environmentDict setValue:@"/workspace/android-ndk-r9d" forKey:@"NDK_ROOT"];
  28. [environmentDict setValue:@"-Dfile.encoding=UTF-8" forKey:@"JAVA_TOOL_OPTIONS"];
  29. [currentTask setEnvironment:environmentDict];
  30. // Set arguments
  31. [currentTask setArguments:new];
  32. // set input
  33. [currentTask launch];
  34. [currentTask waitUntilExit];
  35. }
  36. +(void) excuteScriptString:(NSString *)script {
  37. NSAppleScript *as = [[NSAppleScript alloc] initWithSource:script];
  38. NSDictionary *errors = nil;
  39. [as executeAndReturnError:&errors];
  40. if (errors) {
  41. NSLog(@"some error occured");
  42. }
  43. }
  44. @end