dsg
dsg

Reputation: 13004

Using Ruby to close windows on Mac OS X

I want to programmatically close a window using Ruby on Mac OS X (i.e. send "COMMAND+W" to the window, or click on the red X button in the top left corner).

I think there are things like this for Windows (e.g. win32api), but I do not know how to do it on Mac OS X.

Upvotes: 0

Views: 468

Answers (1)

dsg
dsg

Reputation: 13004

After some googling, the answer is: appscript.

Install it using ruby-gems:

$ sudo gem install rb-appscript

Make sure the program you are trying to control supports AppleScripting. I'm trying to control Google Sketchup, so I type the following into the bash prompt:

$ defaults write /Applications/Google\ SketchUp\ 8/SketchUp.app/Contents/Info NSAppleScriptEnabled -bool YES

If you were controlling Preview, for instance, you would instead type the following:

$ defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool YES

Then make your script with the appropriate headers for appscript:

#!/usr/bin/ruby
require 'rubygems'
require 'appscript'
include Appscript   # note the lack of quotes

app('SketchUp').windows[0].close   # closes the window

Upvotes: 1

Related Questions