Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu bar icon in OS X for script running as daemon?

Tags:

macos

ruby

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?

like image 241
daemonza Avatar asked Apr 19 '12 12:04

daemonza


2 Answers

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
like image 191
Phrogz Avatar answered Nov 15 '22 18:11

Phrogz


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

like image 29
pmerino Avatar answered Nov 15 '22 19:11

pmerino