Google Analytics

Tuesday, May 12, 2009

Hiding windows with Ruby and appscript on OS X

At the moment I'm writing a Ruby script that interacts with iTunes on OS X. Fortunately this is fairly easy to do using the AppleScript bridge rb-appscript.

My problem was that I needed to hide the iTunes window (the same as using the ⌘H shortcut) so that the Ruby application didn't lose focus. Unfortunately it took me a long time to find out how to do it.

I didn't need to deal with OSAX. Here's a snippet of code that demonstrates the necessary incantation:
require 'rubygems'
require 'appscript'

itunes ='iTunes')
# Hide the iTunes window'System Events').processes['iTunes'].visible.set(false)
Here is what the actual AppleScript (which I was able to find easily) looks like:
tell application "System Events"
    set visible of process "iTunes" to false
end tell
You can see how the 'Rubyised' version maps to it, although it took a lot of Googling to translate!

No comments: