依舊假使有一個需求当辐,很簡單黎棠,根據(jù)xcodeproj路徑打開該Xcode工程晋渺。若該工程已經(jīng)打開镰绎,則關閉后再打開。
如果是通過OC來實現(xiàn)木西,OC可以調用下面的方法來實現(xiàn)打開Xcode工程畴栖。
[[NSWorkspace sharedWorkspace] openFile:@"對應的xcodeproj路徑" withApplication:@"該Mac的Xcode名稱" andDeactivate:YES];
但是沒有辦法重新打開該工程呀,在我已知的方法里OC沒有辦法來操作關閉Xcode這樣的需求八千。那么來通過AppleScript來操作吧驶臊。AppleScript本身就是來操作Mac上的應用程序的語言。
我們先通過腳本編輯器來嘗試下如何去關閉Xcode叼丑。
首先打開 腳本編輯器 关翎,> 文件 > 打開詞典 來查看Xcode的支持的AppleScript的操作有哪些?
我這里有三個Xcode鸠信,應用程序的名字不一樣纵寝,但是這幾個關閉和打開Xcode的AppleScript的命令都是一樣的。隨便選擇一個
看到open方法星立,可以打開對應的文件爽茴。我們調用試下
tell application "Xcode8.1" -- 填寫需要打開的應用名稱
open "/Users/lujh/Test/Test.xcodeproj"
end tell
能夠成功打開。
接下來close绰垂,close可以關閉文件或者windows室奏。
tell application "Xcode"
repeat with aWindow in windows
set aDocument to document of aWindow
set currentPath to ""
try
set currentPath to path of aDocument
end try
if currentPath = "/Users/lujh/Test/Test.xcodeproj" then
close aWindow
exit repeat
end if
end repeat
end tell
遍歷所有已經(jīng)打開的Xcode的windows,判斷path與要關閉的文件路徑是否一致劲装,一致則關閉該window胧沫。
現(xiàn)在我們已經(jīng)能夠滿足關閉和打開該工程了。那么整合下占业,給出一個便于調用的接口绒怨,handler。需要兩個參數(shù)谦疾,Xcode的名稱和需要重新打開的xcodeproj文件的路徑南蹂。
AppleScript代碼
on reopenWindowWithPath(XcodeName, windowPath)
tell application XcodeName
repeat with aWindow in windows
set aDocument to document of aWindow
set currentPath to ""
try
set currentPath to path of aDocument
end try
if currentPath = windowPath then
close aWindow
exit repeat
end if
end repeat
open windowPath
end tell
return
end reopenWindowWithPath
在腳本編輯器,將文件保存為scpt格式念恍。只有這個格式才能被OC調用六剥。
通過OC調用AppleScript,創(chuàng)建NSAppleScript對象峰伙,傳入AppleScript所在的url疗疟。創(chuàng)建NSAppleEventDescriptor對象,指定suite词爬、eventID等秃嗜,這幾個宏是固定的,因為我們不需要其他的功能顿膨,所有不導入其他的庫锅锨,只重新聲明下這幾個宏定義。然后傳入指定的handler的函數(shù)名恋沃,key是固定的‘snam’必搞,并通過同樣的方法傳入需要的參數(shù)。最后調用executeAppleEvent 執(zhí)行AppleScript囊咏。
OC 代碼
+ (void)reopenXcodeWindowWithXcodeName:(NSString *)xcodeName projectPath:(NSString *)path
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"MobSDKToolScript" ofType:@"bundle"];
NSBundle *scriptBundle = [NSBundle bundleWithPath:bundlePath];
NSURL *url = [scriptBundle URLForResource:@"reopenXcode" withExtension:@"scptd" subdirectory:@"AppleScript"];
NSAppleScript * appleScript;
NSAppleEventDescriptor * thisApplication, *containerEvent;
NSDictionary * appleScriptCreationError = nil;
appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&appleScriptCreationError];
#define kASAppleScriptSuite 'ascr'
#define kASSubroutineEvent 'psbr'
#define keyASSubroutineName 'snam'
containerEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
eventID:kASSubroutineEvent
targetDescriptor:thisApplication
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
[containerEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:@"reopenWindowWithPath"]
forKeyword:keyASSubroutineName];
NSAppleEventDescriptor *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor];
NSString *object;
NSArray *scriptArgumentArray = @[[xcodeName stringByDeletingPathExtension], path];
for (object in scriptArgumentArray)
{
[arguments insertDescriptor:[NSAppleEventDescriptor descriptorWithString:object]
atIndex:([arguments numberOfItems] + 1)];
}
[containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject];
//Execute the event
NSDictionary *executionError = nil;
NSAppleEventDescriptor *result = [appleScript executeAppleEvent:containerEvent error:&executionError];
if (executionError != nil)
{
DLog(@"%@", [NSString stringWithFormat:@"error while executing script. Error %@",executionError]);
}
else
{
DLog(@"Script execution has succeed. Result(%@)",result);
}
}