Menubar App in AppleScriptObjC

This is my first post here. I am, apologies, a n00b.

That said, I think my question/request will be clear enough:

I’m trying to make a menubar app using AppleScriptObjC, and I’ve been looking at some older posts which describe how to do it in Applescript Studio
http://macscripter.net/viewtopic.php?id=21036
http://macscripter.net/viewtopic.php?id=21885

and can’t get much beyond them in terms of translating into ASOC. It seems like having a menubar app template (which both posts basically give) written in ASOC would be really helpful.

Any ideas/answers/solutions would be really appreciated.

Thanks.

To help discussion I’m going to repost Stadsman’s code (which is from http://macscripter.net/viewtopic.php?id=21036)

Assuming the ObjC code is OK, the only thing you’ll need to do different is to replace the call method line with:

tell current application's class "Menulet"'s showMenuIcon()

it works. Well, actually it needs to be something like this:

tell current application
			class "Menulet"'s showMenuIcon()
		end tell

But all it does is appear for a second and then disappear. This is calling this tell either from AwakeFromNiB() or from a handler in my script. I see the icon and can click it and even use its “Quit” Menu item… but then it disappears.

Ideally I can put this in AwakeFromNib, so that the menubar icon/menu appears on launch and it stays until I quit the app.

Also: I can kind of figure out how to add menu bar items to the the new icon’s menu in Obj-c in Menulet.m. But it would be much better if I could hook this up to an IB menu. In IB I can grab Menulet’s blue cube, but it has no methods…

Have you seen Jonathan Nathan’s Menuapp example?

http://www.jonn8.com/html/MenuApp.html

It has a lot of OBJ-C code for working with a status menu.

Rob

I had not seen it. Thanks for the reference.

Unfortunately, it is not written in ASOC, so most of the applescript part of it would need to be changed to work with an ASOC project.

And, since it uses some carbon (I can’t tell how much, only that it does), I’m not sure it’s capable of working with ASOC at all.

I’ve pretty much figured out a simpler, ASOC-compatible, solution. It’s got a few kinks, which I’m working on, and it uses someone else’s code, so I’m waiting to hear from them before posting it.

Update-- made some edits in the instructions on how to connect up your AppDelegate

Download menuletTemplate XCode Project here : http://is.gd/4WMQM

OK.
I figured it out. Using this tutorial http://www.mactech.com/articles/mactech/Vol.22/22.02/Menulet/index.html, I was able to put together a menubar app and get it working with my ASOC project. The download above is a template XCode project based on that, and it’s explicitly set up as an ApplescriptObjC project, with an AppDelegate set up already.

Implementing the menubar part of the application involves no code at all. You can use the template itself, or copy Menulet.m and menulet.h into your project and that Obj-C class pretty much does it all.

There’s some Interface builder work, because the menubar icon connects to an IB menu which you can use to connect to your applescript methods.

If you don’t want to download the template, I’ll paste in the code here and explain how to use it. Otherwise, the template includes some instructions and is pretty simple.

The instructions below assume you’ve already got an XCode project started.

  1. Right click on the Classes folder and choose Add–> New File… In the dialog, choose Cocoa Class type and then Objective-C Class. in the creation dialog, name your class Menulet.m and make sure the box is checked to “Also create Menulet.h”. This should then create both Menulet.m (the class) and Menulet.h (the header) in your class folder. Go to Menulet.h and replace the default code with this:
  1. Go to Menulet.m and replace the default code with this:
  1. Create an icon for your menubar, make it 64px by 64px @300pixels/inch or 32px by 32px @150pixels/inch. Name it “MenuIcon.png” (I recommend keeping the background transparent so it looks right on the menubar) and save it in your Resources folder.

  2. In your AppDelegate.applescript class (which is your main applescript in the project) create a new property:

  1. Open your MainMenu.xib with Interface Builder. You need to create a new Menu (which you can find by searching in your library under classes). Add however many Menu items you want, connecting their Selector to your applescript handlers just as you would in any other ApplescriptObjC project. This will be the menu that drops down from your menubar icon.

  2. In IB, search in your Library’s classes for the Menulet class (which we added to the project in step 2. its a blue cube) and drag it onto your project. Right-click, or control-click, on the Menulet blue cube and connect its “theMenu” outlet to the Menu you just created in step 5.

7a. Right-click on the icon for your AppDelegate (if you don’t have it, search for it in the Library>>Classes; this is your main applescript delegate in XCode you added a property to in Step 4) and grab New Referencing Outlet and connect it to the Application icon in your MainMenu.xib window.

7b. Right-click on the icon for your AppDelegate again and grab aNotifcation (the property you created in step 4), and connect it to the Menulet blue cube.

  1. Save your MainMenu.xib and close Interface builder.

If you want your application to be a Menubar ONLY application (meaning it won’t appear in your Dock or in the command-tab appswitcher) then there’s one more thing to do. Otherwise, you’re done (at least with implementing the menubar part of your app). To make your app Menubar only:

  1. Go back into XCode and go to your AppName-info.plist (which is the plist with information like what version your application is and what your company’s name is). Select any row in the plist and right-click. Choose Add Row. When the new Row appears there will be a drop down list of potential plist entries. Scroll up to “Application is agent (UIElement)” and hit return twice. Then click in the field next to it, a check box should appear. If the box is checked your application will be menubar only.

I’m no expert, but this seems to work pretty well.

this work for me too!!! :slight_smile:

Let me add some code in order to let change the icon to another when some working is done

So when you want to activate the alternative icon in your AppDelegate.applescript

and to return the original one

Gabo

That’s great. Does anyone know how to make a menu bar icon respond to a click?

I want a normal (left) mouse click on the menu icon to activate my application, and a right-click on the icon to drop down a menu.

Anyone know how to do this?

I’ve revised my MenuApp project and updated it from AppleScript Studio to AppleScriptObjC. For more information, see:

http://www.jonn8.com/html/MenuApp_ASOC.html

Jon

I try to create menubar app too.

These instructions are for older Xcode, since these instructions requires Interface Builder.

How i can do this in Xcode 4.x?

Thanks

In Xcode 4 Interface Builder is built into Xcode. Click on a .xib file in the sidebar and the Editor will change to an interface editor with the same features as IB.