ZGImacroUtils.m 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // ZGImacroUtils.m
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2017/12/26.
  6. // Copyright © 2017年 zhuge. All rights reserved.
  7. //
  8. #import "ZGImacroUtils.h"
  9. @implementation ZGImacroUtils
  10. +(void)makeIIMReadable:(NSString *)fileName {
  11. // add file head of binary
  12. NSData *data2 = [NSData dataWithContentsOfFile:fileName];
  13. NSString *path = [[NSBundle mainBundle] pathForResource:@"ImacrosPreBinary" ofType:nil];
  14. NSMutableData *data = [NSMutableData dataWithContentsOfFile:path];
  15. [data appendData:data2];
  16. [data writeToFile:fileName atomically:YES];
  17. }
  18. // 用IIM的格式来格式化字符串
  19. +(NSString *) stringForIIMContent:(NSString *)str {
  20. NSString *ret = [NSString stringWithFormat:@"\"%@\"", [str stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]];
  21. ret = [ret stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
  22. return ret;
  23. }
  24. +(NSString *) stringForIIMPath:(NSString*)str {
  25. return [str stringByReplacingOccurrencesOfString:@" " withString:@"<SP>"];
  26. }
  27. +(NSString *)iimPathOfMacOS {
  28. NSString *userHomePath = NSHomeDirectory();
  29. return [userHomePath stringByAppendingFormat:@"/iMacros/Macros/"];
  30. }
  31. +(void)runIIMFile:(NSString *)filePath {
  32. // NSString *cmd = @"/Applications/Firefox.app/Contents/MacOS/firefox";
  33. NSString *arg = [NSString stringWithFormat:@"imacros://run/?m=%@", filePath];
  34. NSString *scriptFileName = @"OpenFirefoxWithUrl.scpt";
  35. NSArray *args = @[arg];
  36. NSString *script = [NSString stringWithString:scriptFileName];
  37. if ([script hasSuffix:@"scpt"]) {
  38. script = [script stringByDeletingPathExtension];
  39. }
  40. NSString* scpt_path = [[NSBundle mainBundle] pathForResource:script ofType:@"scpt"];
  41. NSArray *cmd = [NSArray arrayWithObject:scpt_path];
  42. NSArray *new = [cmd arrayByAddingObjectsFromArray:args];
  43. NSTask *currentTask = [[NSTask alloc] init];
  44. [currentTask setLaunchPath:@"/usr/bin/osascript"];
  45. NSMutableDictionary *environmentDict = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
  46. NSString *path = [environmentDict objectForKey:@"PATH"];
  47. path = [NSString stringWithFormat:@"%@:%@", [[NSBundle mainBundle] pathForResource:@"ApkTools" ofType:nil], path];
  48. [environmentDict setValue:path forKey:@"PATH"];
  49. [environmentDict setValue:@"xterm-256color" forKey:@"TERM"];
  50. [environmentDict setValue:@"-Dfile.encoding=UTF-8" forKey:@"JAVA_TOOL_OPTIONS"];
  51. [currentTask setEnvironment:environmentDict];
  52. // Set arguments
  53. [currentTask setArguments:new];
  54. [currentTask launch];
  55. [currentTask waitUntilExit];
  56. // [ZGScriptUtils excuteScriptFile:@"CloseFirefox" withArgs:@[@""]];
  57. // [ZGShellUtils execAsyn:cmd args:@[arg]];
  58. // [ZGScriptUtils excuteScriptFile:@"CloseFirefox" withArgs:@[@""]];
  59. }
  60. + (void)createWithTemplateName:(NSString *)templateFileName outputFileName:(NSString *)outputFileName replaceDictionary:(NSDictionary *)replaceDic {
  61. if (![[templateFileName pathExtension] isEqualToString:@"js"]) {
  62. templateFileName = [templateFileName stringByAppendingString:@".js"];
  63. }
  64. NSString *path = [[NSBundle mainBundle] pathForResource:templateFileName ofType:nil];
  65. NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  66. for (NSString *oldString in replaceDic) {
  67. NSString *newString = [replaceDic objectForKey:oldString];
  68. content = [content stringByReplacingOccurrencesOfString:oldString withString:newString];
  69. }
  70. NSString *robotFileName =outputFileName;
  71. NSString *outputPath = [NSString stringWithFormat:@"%@/%@", [ZGImacroUtils iimPathOfMacOS], robotFileName];
  72. NSString *outputPathParent = [outputPath stringByDeletingLastPathComponent];
  73. NSFileManager *fm = [NSFileManager defaultManager];
  74. if (![fm fileExistsAtPath:outputPathParent]) {
  75. [fm createDirectoryAtPath:outputPathParent withIntermediateDirectories:YES attributes:nil error:nil];
  76. }
  77. [content writeToFile:outputPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  78. [ZGImacroUtils makeIIMReadable:outputPath];
  79. }
  80. @end