14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでAppleScriptを使う方法

Last updated at Posted at 2015-11-16

##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)
	}
}

descriptorAtIndex1から始まることに注意

14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?