// // ZGImacroUtils.m // UUTUtils // // Created by zhuge on 2017/12/26. // Copyright © 2017年 zhuge. All rights reserved. // #import "ZGImacroUtils.h" @implementation ZGImacroUtils +(void)makeIIMReadable:(NSString *)fileName { // add file head of binary NSData *data2 = [NSData dataWithContentsOfFile:fileName]; NSString *path = [[NSBundle mainBundle] pathForResource:@"ImacrosPreBinary" ofType:nil]; NSMutableData *data = [NSMutableData dataWithContentsOfFile:path]; [data appendData:data2]; [data writeToFile:fileName atomically:YES]; } // 用IIM的格式来格式化字符串 +(NSString *) stringForIIMContent:(NSString *)str { NSString *ret = [NSString stringWithFormat:@"\"%@\"", [str stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]; ret = [ret stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]; return ret; } +(NSString *) stringForIIMPath:(NSString*)str { return [str stringByReplacingOccurrencesOfString:@" " withString:@""]; } +(NSString *)iimPathOfMacOS { NSString *userHomePath = NSHomeDirectory(); return [userHomePath stringByAppendingFormat:@"/iMacros/Macros/"]; } +(void)runIIMFile:(NSString *)filePath { // NSString *cmd = @"/Applications/Firefox.app/Contents/MacOS/firefox"; NSString *arg = [NSString stringWithFormat:@"imacros://run/?m=%@", filePath]; NSString *scriptFileName = @"OpenFirefoxWithUrl.scpt"; NSArray *args = @[arg]; NSString *script = [NSString stringWithString:scriptFileName]; if ([script hasSuffix:@"scpt"]) { script = [script stringByDeletingPathExtension]; } NSString* scpt_path = [[NSBundle mainBundle] pathForResource:script ofType:@"scpt"]; NSArray *cmd = [NSArray arrayWithObject:scpt_path]; NSArray *new = [cmd arrayByAddingObjectsFromArray:args]; NSTask *currentTask = [[NSTask alloc] init]; [currentTask setLaunchPath:@"/usr/bin/osascript"]; NSMutableDictionary *environmentDict = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]]; NSString *path = [environmentDict objectForKey:@"PATH"]; path = [NSString stringWithFormat:@"%@:%@", [[NSBundle mainBundle] pathForResource:@"ApkTools" ofType:nil], path]; [environmentDict setValue:path forKey:@"PATH"]; [environmentDict setValue:@"xterm-256color" forKey:@"TERM"]; [environmentDict setValue:@"-Dfile.encoding=UTF-8" forKey:@"JAVA_TOOL_OPTIONS"]; [currentTask setEnvironment:environmentDict]; // Set arguments [currentTask setArguments:new]; [currentTask launch]; [currentTask waitUntilExit]; // [ZGScriptUtils excuteScriptFile:@"CloseFirefox" withArgs:@[@""]]; // [ZGShellUtils execAsyn:cmd args:@[arg]]; // [ZGScriptUtils excuteScriptFile:@"CloseFirefox" withArgs:@[@""]]; } + (void)createWithTemplateName:(NSString *)templateFileName outputFileName:(NSString *)outputFileName replaceDictionary:(NSDictionary *)replaceDic { if (![[templateFileName pathExtension] isEqualToString:@"js"]) { templateFileName = [templateFileName stringByAppendingString:@".js"]; } NSString *path = [[NSBundle mainBundle] pathForResource:templateFileName ofType:nil]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; for (NSString *oldString in replaceDic) { NSString *newString = [replaceDic objectForKey:oldString]; content = [content stringByReplacingOccurrencesOfString:oldString withString:newString]; } NSString *robotFileName =outputFileName; NSString *outputPath = [NSString stringWithFormat:@"%@/%@", [ZGImacroUtils iimPathOfMacOS], robotFileName]; NSString *outputPathParent = [outputPath stringByDeletingLastPathComponent]; NSFileManager *fm = [NSFileManager defaultManager]; if (![fm fileExistsAtPath:outputPathParent]) { [fm createDirectoryAtPath:outputPathParent withIntermediateDirectories:YES attributes:nil error:nil]; } [content writeToFile:outputPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; [ZGImacroUtils makeIIMReadable:outputPath]; } @end