8

I have a ruby script(https://github.com/daemonza/MacBak) that runs on my macbook as a daemon and monitors a bunch of directories for file changes and rsync any changes that happens. I was wondering would i be able to let it create a icon in the menu bar at the top? Just so that I know it's actually running, without having to check for it with ps.

Maybe later if needed I might want to be able to control the script from there, simple drop down with stop and status entries, etc.

It seems from ObjectC I can call NSStatusItem to get the icon, but I really just want to do it easily from my Ruby script. Perhaps maybe some applescript call that I can do?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
daemonza
  • 527
  • 5
  • 16
  • Look at RubyCocoa. I've never used it, but I think it works pretty well for using Cocoa from Ruby. – Linuxios Apr 19 '12 at 13:08
  • @Linux_iOS See [this answer](http://stackoverflow.com/a/695353/405017) noting that RubyCocoa is the past, and MacRuby is the future. – Phrogz Apr 19 '12 at 16:08
  • Hm. I never dealt with it, so I wouldn't know. Thanks for clarifying. – Linuxios Apr 19 '12 at 23:06

2 Answers2

6

This MacRuby script creates a status bar icon:
https://github.com/ashchan/gmail-notifr

So does this one:
https://github.com/isaac/Stopwatch

Here's a Gist including code that does it:
https://gist.github.com/1480884

# We build the status bar item menu
def setupMenu
  menu = NSMenu.new
  menu.initWithTitle 'FooApp'
  mi = NSMenuItem.new
  mi.title = 'Hellow from MacRuby!'
  mi.action = 'sayHello:'
  mi.target = self
  menu.addItem mi

  mi = NSMenuItem.new
  mi.title = 'Quit'
  mi.action = 'quit:'
  mi.target = self
  menu.addItem mi

  menu
end

# Init the status bar
def initStatusBar(menu)
  status_bar = NSStatusBar.systemStatusBar
  status_item = status_bar.statusItemWithLength(NSVariableStatusItemLength)
  status_item.setMenu menu 
  img = NSImage.new.initWithContentsOfFile 'macruby_logo.png'
  status_item.setImage(img)
end

# Menu Item Actions
def sayHello(sender)
    alert = NSAlert.new
    alert.messageText = 'This is MacRuby Status Bar Application'
    alert.informativeText = 'Cool, huh?'
    alert.alertStyle = NSInformationalAlertStyle
    alert.addButtonWithTitle("Yeah!")
    response = alert.runModal
end

def quit(sender)
  app = NSApplication.sharedApplication
  app.terminate(self)
end

app = NSApplication.sharedApplication
initStatusBar(setupMenu)
app.run
Phrogz
  • 296,393
  • 112
  • 651
  • 745
1

You could look at MacRuby. It's a way of developing OS X apps using Ruby instead of Objective-C. It includes a number of improvements, such as getting rid of header files, so yu just have "implementation" files in Ruby. You can use IB for building windows too

pmerino
  • 5,900
  • 11
  • 57
  • 76