Cocoa には NSAppleScript というクラスがあって、アプリケーションで AppleScript を使うことができる。
まず、NSAppleScript.alloc.initWithSourse(script) で AppleScript オブジェクトを作って、executeAndReturnError(error) で実行する。error は NSDictionary オブジェクト。
error = NSDictionary.alloc.init
script = NSAppleScript.alloc.initWithSource(aScript)
script.executeAndReturnError(error)
error は必要なのかどうかわからないので、 nil として、これを全部まとめて、
NSAppleScript.alloc.initWithSource(aScript).executeAndReturnError(nil)
なんてしても、同じように動くはず。
たとえば、辞書.app を起動させるには、次のようなメソッドを書いて、ボタンやメニューアイテムに割り当てて実行する。
def runAppleScript(sender)
aScript = <<-EOM
tell application "Dictionary"
activate
end tell
EOM
script = NSAppleScript.alloc.initWithSource(aScript)
script.executeAndReturnError(nil)
end
ib_action :runAppleScript
スクリプトが長かったりする場合は、awakeFromNib でインスタンス変数として定義してから、実行する場合は、そのスクリプトを実行するようにした方がいいかも(毎回スクリプトの String オブジェクトを作らなくていいように)。