public
Created

  • Download Gist
animate_xib.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env macruby
framework 'AppKit'
 
require 'tempfile'
require 'shellwords'
 
 
class WindowController < NSWindowController
def awakeFromNib
@timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector:'onTimer', userInfo:nil, repeats:true)
end
 
def applicationShouldTerminateAfterLastWindowClosed(app)
true
end
 
def onTimer
window = NSApp.windows[0]
 
return unless window
 
window.setLevel(NSStatusWindowLevel)
 
views = window.contentView.subviews
views.each do |view|
if view.respond_to?(:startAnimation)
view.startAnimation(self)
end
 
if view.class.to_s =~ /slider/i
view.setDoubleValue(rand(100))
end
 
if view.respond_to?(:setState)
view.setState(rand > 0.5 ? NSOnState : NSOffState)
end
 
if view.respond_to?(:highlight)
view.highlight(rand > 0.9)
end
 
if view.respond_to?(:setBezelStyle)
view.setBezelStyle(rand(15) + 1)
end
end
end
end
 
# path for .xib
xib_path = ARGV.shift
 
# create .nib
tmpfile = Tempfile.new('tmp.nib')
system("ibtool #{Shellwords.escape(xib_path)} --compile #{Shellwords.escape(tmpfile.path)}")
 
nib = NSNib.alloc.initWithContentsOfURL(NSURL.fileURLWithPath(tmpfile.path))
owner = WindowController.new
app = NSApplication.sharedApplication
app.setDelegate(owner)
nib.instantiateNibWithOwner(owner, topLevelObjects:nil)
 
NSApp.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.