標籤:macos objective-c applescript
在Objective-C裡其實也可以運行AppleScript
第一種方式是Source 將指令碼寫到變數字串裡
NSAppleEventDescriptor *eventDescriptor = nil; NSAppleScript *script = nil; NSBundle *bunlde = [NSBundle mainBundle]; NSString *scriptSource = @"tell application \"Finder\"\r" "display dialog \"test\"\r" "end tell"; if (scriptSource) { script = [[NSAppleScript alloc] initWithSource:scriptSource]; if (script) { eventDescriptor = [script executeAndReturnError:nil]; if (eventDescriptor) { NSLog(@"%@", [eventDescriptor stringValue]); } } }
第二種方式是將File, 將指令碼寫到檔案裡
NSAppleEventDescriptor *eventDescriptor = nil; NSAppleScript *script = nil; NSBundle *bunlde = [NSBundle mainBundle]; NSString *scriptPath = @"/Users/exchen/Documents/test.scpt"; if (scriptPath) { script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:scriptPath] error:nil]; if (script) { eventDescriptor = [script executeAndReturnError:nil]; if (eventDescriptor) { NSLog(@"%@", [eventDescriptor stringValue]); } } }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Objective-C 運行AppleScript指令碼