2006年 11月 04日

Ruby/Cocoa でステータスアイテムを追加してみる

メニューバーの右のほうのアイコンとかのことをステータスアイテムというらしい。Windows でいうところのタスクトレイ?

require 'osx/cocoa'

include OSX

class Test < NSObject

	def applicationDidFinishLaunching(aNotification)
		menu = NSMenu.alloc.initWithTitle "TEST"
		menu.setDelegate self
		menu.setAutoenablesItems false

		item = menu.addItemWithTitle "TEST", :action, "menuclicked_", :keyEquivalent, ""
		item.setEnabled true

		# new image and resize
		img = NSWorkspace.sharedWorkspace.iconForFileType "unknown"
		size = NSSize.new
		size.width = 16
		size.height = 16
		img.setSize size

		# create status item
		bar = NSStatusBar.systemStatusBar
		item = bar.statusItemWithLength -1
		item.setTitle "Test"
		item.setToolTip "TESTEST"
		item.setImage img
		item.setHighlightMode true
		item.setMenu menu
	end

	def menuclicked(sender)
		p sender
		true
	end
end


app = NSApplication.sharedApplication
app.setDelegate(Test.alloc.init)
p app

trap('SIGINT') { exit }
app.run

ちょーかんたん!!