##AppleScriptの返り値がStringの場合
import Cocoa
let appleScript = "tell application \"iTunes\"\n"
+ "set trackName to name of current track\n"
+ "return trackName\n"
+ "end tell"
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: appleScript) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error) {
print(output.stringValue) // Optional("TrackName")
}
if error != nil {
print(error)
}
}
##AppleScriptの返り値がListの場合
import Cocoa
let appleScript = "tell application \"iTunes\"\n"
+ "set trackName to name of current track\n"
+ "set trackArtist to artist of current track\n"
+ "return {trackName,trackArtist}\n"
+ "end tell"
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: appleScript) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(&error) {
print(output.descriptorAtIndex(1)?.stringValue) // Optional("TrackName")
print(output.descriptorAtIndex(2)?.stringValue) // Optional("ArtistName")
}
if error != nil {
print(error)
}
}
descriptorAtIndex
が1
から始まることに注意