-
-
Save Rajan/f5642833a68a8d964b50218454d813d3 to your computer and use it in GitHub Desktop.
Calling AppleScript from Swift App, passing a parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift version of https://developer.apple.com/library/mac/technotes/tn2084/_index.html | |
@IBAction func testButtonPushed(sender: AnyObject) { | |
let URL = NSBundle.mainBundle().URLForResource("SendFinderMessage", withExtension: "scpt")! | |
var errors: NSDictionary? | |
let script = NSAppleScript(contentsOfURL: URL, error: &errors)! | |
let message = NSAppleEventDescriptor(string: "Message from App") | |
let parameters = NSAppleEventDescriptor.listDescriptor() | |
parameters.insertDescriptor(message, atIndex: 1) | |
var psn = ProcessSerialNumber(highLongOfPSN: UInt32(0), lowLongOfPSN: UInt32(kCurrentProcess)) | |
let target = NSAppleEventDescriptor(descriptorType: DescType(typeProcessSerialNumber), bytes:&psn, length:sizeof(ProcessSerialNumber)) | |
let handler = NSAppleEventDescriptor(string: "show_message") | |
let event = NSAppleEventDescriptor.appleEventWithEventClass(AEEventClass(kASAppleScriptSuite), eventID: AEEventID(kASSubroutineEvent), targetDescriptor: target, returnID: AEReturnID(kAutoGenerateReturnID), transactionID: AETransactionID(kAnyTransactionID)) | |
event.setParamDescriptor(handler, forKeyword: AEKeyword(keyASSubroutineName)) | |
event.setParamDescriptor(parameters, forKeyword: AEKeyword(keyDirectObject)) | |
// ObjC version compares this to nil, but it is non optional! | |
let descriptor = script.executeAppleEvent(event, error: &errors) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment