After some searches, the answer is: appscript .
Install it using ruby-gems :
$ sudo gem install rb-appscript
Make sure that the program you are trying to control supports AppleScripting . I am trying to control Google Sketchup, so I will introduce the following at the bash prompt:
$ defaults write /Applications/Google\ SketchUp\ 8/SketchUp.app/Contents/Info NSAppleScriptEnabled -bool YES
Preview, , :
$ defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool YES
script appscript:
require 'rubygems'
require 'appscript'
include Appscript
app('SketchUp').windows[0].close