Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / August 2007



Tip: Looking for answers? Try searching our database.

what sort of app is this

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mlabs - 20 Aug 2007 03:57 GMT
I want to write the equivalent of a windows 'tray application' .. but
for the mac.

So far i've found out that the little icons on the right side of the
menu bar (near the clock) are NSStatusItem's

Q: how do I write an app that only has an NSStatusItem with a context
menu? It should have nothing else .. no menu items on the left side of
the menu bar - no main window...

Q: preferably I want to avoid learning objective C and stick with c++
- so I guess Carbon is the way to go here... are there any examples of
this sort of application written in Carbon?

TIA
Tom Harrington - 20 Aug 2007 05:03 GMT
> I want to write the equivalent of a windows 'tray application' .. but
> for the mac.
[quoted text clipped - 5 lines]
> menu? It should have nothing else .. no menu items on the left side of
> the menu bar - no main window...

Generally this is done by adding a key to the application's Info.plist--
set key LSUIElement to true, and you don't get a menu bar or appear in
the Dock.  You can still have status items, though.  This may help:
<http://developer.apple.com/documentation/MacOSX/Conceptual/BPRuntimeConf
ig/Articles/PListKeys.html>

> Q: preferably I want to avoid learning objective C and stick with c++
> - so I guess Carbon is the way to go here... are there any examples of
> this sort of application written in Carbon?

Sorry, never tried to do it in Carbon.  Don't avoid learning new
things-- Cocoa is a great environment to develop with.

Signature

Tom "Tom" Harrington
MondoMouse makes your mouse mightier
See http://www.atomicbird.com/mondomouse/

mlabs - 20 Aug 2007 05:31 GMT
thanks Tom - it's not that I don't *want* to learn new things ..
but for me its a question of deadlines.. the Carbon route looks much
more like what i'm used to... the Cocoa stuff looks more high-level -
reminds me of smalltalk .. I prefer to drive stick...   :-)
Michael Ash - 20 Aug 2007 05:46 GMT
> thanks Tom - it's not that I don't *want* to learn new things ..
> but for me its a question of deadlines.. the Carbon route looks much
> more like what i'm used to... the Cocoa stuff looks more high-level -
> reminds me of smalltalk .. I prefer to drive stick...   :-)

For any non-trivial app, it will probably be faster to learn Objective-C
and Cocoa than it will be to learn Carbon. Carbon has no advantage unless
you're already familiar with it or something similar like the Mac toolbox,
and even there it has a lot of changes.

In any case, it's completely academic for this discussion. NSStatusItem is
Cocoa-only and there is no Carbon equivalent, so you have no choice.

Signature

Michael Ash
Rogue Amoeba Software

mlabs - 20 Aug 2007 08:49 GMT
i'm a win32 programmer boldly going where few of us have gone
before.. :)

so before cocoa, there were no little icons over on the right side of
the menu bar?
Gregory Weston - 20 Aug 2007 14:22 GMT
> i'm a win32 programmer boldly going where few of us have gone
> before.. :)

You'd be surprised...

> so before cocoa, there were no little icons over on the right side of
> the menu bar?

Before OS X, there was a different "best" mechanism to do get such an
item into the menu bar and it wasn't as good or as supported. Most
people looking to do the equivalent of a Windows tray icon would've
instead done a plugin for a facility called the control strip.

Let me put it this way. You're "boldly going" some place you haven't
been. You've sought the advice of people who've already been there.
Don't then argue with the advice you've been given in good faith.
mlabs - 20 Aug 2007 19:32 GMT
didn't mean to come across as 'argumentative' !  all the advice is
greatly appreciated..  I'm just trying to understand the full picture
- as you guys do..
I read somewhere that a lot of Cocoa is implemented with Carbon .. so
that's why I pushed with the last question ... I sort of thought that
Carbon was like win32 and that Cocoa was a higher-level layer/
framework on top of that - but this is not the case? Totally separate
beasts? I mean at some point, the Cocoa widget must be calling some
lower level Mac api to insert itself into the menu no?
would love to get all these pieces straight in my head - I might just
make a decent Mac programmer one day - lol

by the way, i've been doing Windows since Windows 1.0 .. in C/C++ ..
and last time I tried Mac programming (pre OS X) it was a pain in the
butt .. but now I am pleasantly surprised at how nice it is ...
enjoying it even...
:-)
Michael Ash - 20 Aug 2007 20:28 GMT
> didn't mean to come across as 'argumentative' !  all the advice is
> greatly appreciated..  I'm just trying to understand the full picture
[quoted text clipped - 5 lines]
> beasts? I mean at some point, the Cocoa widget must be calling some
> lower level Mac api to insert itself into the menu no?

Neither view is correct. Cocoa and Carbon are on an equal footing, but
they aren't totally independent either.

The low level framework is CoreGraphics, also known as Quartz, which
includes a bunch of private stuff like the core window objects which both
Cocoa and Carbon windows are implemented on top of.

Many things are implemented independently. For example, the control
hierarchies are done completely separately, which often causes odd
inconsisitencies in the behavior of what should be identical Carbon and
Cocoa controls.

Many things are implemented in terms of the other framework. As an
example, Cocoa menus are implemented on top of Carbon menus. And Carbon
font and color panels use the Cocoa ones.

The end result is that both frameworks are capable of doing most things
but there are certain things which only one can do, and you can't say
which framework will be able to do it without actually researching it.

The good news is that the frameworks mix just fine in most cases,
especially if you do all of your GUI in only one of them. I believe, and I
think many other will agree, that the most productive environment is to
write your GUI in Cocoa and use Cocoa liberally otherwise, but to use
Carbon whenever it's necessary or convenient to get things done inside the
app.

Signature

Michael Ash
Rogue Amoeba Software

Gregory Weston - 20 Aug 2007 21:44 GMT
> didn't mean to come across as 'argumentative' !  all the advice is
> greatly appreciated..  I'm just trying to understand the full picture
[quoted text clipped - 3 lines]
> Carbon was like win32 and that Cocoa was a higher-level layer/
> framework on top of that - but this is not the case?

No.

> Totally separate beasts?

Not totally separate. What you read "somewhere" about portions of Cocoa
being implemented in Carbon is accurate. But I wouldn't say that it's a
lot and certainly not most, and there are some things that today simply
are not cost-effective (and sometimes not realistically possible) to do
in Carbon.

> I mean at some point, the Cocoa widget must be calling some
> lower level Mac api to insert itself into the menu no?

Not necessarily. The libraries themselves can certainly be exposing only
Objective-C interfaces with no public procedural API for a given task.

> would love to get all these pieces straight in my head - I might just
> make a decent Mac programmer one day - lol
[quoted text clipped - 4 lines]
> enjoying it even...
> :-)

Hmmm... Having been a Mac programmer since 1988 and a Windows programmer
since 1993 I suggest that what's probably changed most is you. If you're
looking at Carbon, there's really not much fundamentally different from
what you might have seen at any time in the 1990s. If anything, some
things have gotten more convoluted and opaque since then.

If you're a capable C programmer - and I expect you are - and don't let
yourself get defensive/hostile about the "weird" syntax, you can gain at
least a functional knowledge of Objective-C in literally an hour or two.
It'll probably take some practical use and a couple of missteps to
really "get" it - like really understanding the difference between
calling a function and sending a message, and the implications of that
difference - but putting together a status bar item is a piece of cake
once you let the tools work for you.

Depending on the complexity of your app, I think it's entirely likely
that you'll be creating a single subclass (of NSObject) that will act as
the delegate for your application and the target of the menu commands
you create. The trickiest thing you'll hit is a bug in either the
implementation or documentation of NSStatusBar, depending on your point
of view:

The way you create your status bar item is by sending
statusItemWithlength: to the NSStatusBar. This will create the item and
give you back a reference so you can actually set its properties. The
documentation notes, "The receiver does not retain a reference to the
status item, so you need to retain it."

The Cocoa memory contract says that if you retain something, you're
responsible for releasing it later. But if you try to release the status
item, your app will crash because apparently the status bar also sends a
release. So don't, and all will be fine.
mlabs - 20 Aug 2007 22:46 GMT
Gregory & Michael - thanks v much this is very helpful indeed. I bit
the bullet and went down the bookshop to browse objective-C .. yeah
you're totally right - it's trivial if you already know C/C++ and
OOP... so that will be the way I will proceed..My little app just
needs to do some HTTP calls and read/write to a local XML file ... I
found some usefull looking URL classes in the framework, but so far
nothing regarding XML .. any clues there?
again - thanks.
Tom Harrington - 21 Aug 2007 00:12 GMT
> Gregory & Michael - thanks v much this is very helpful indeed. I bit
> the bullet and went down the bookshop to browse objective-C .. yeah
[quoted text clipped - 3 lines]
> found some usefull looking URL classes in the framework, but so far
> nothing regarding XML .. any clues there?

For general purpose XML manipulation, Cocoa has NSXMLDocument and
associated classes (e.g. NSXMLElement).  There's a CF equivalent
(CFXMLNode, etc) if you prefer a C-style interface.

At a lower level, Mac OS X includes libxml2.

Signature

Tom "Tom" Harrington
MondoMouse makes your mouse mightier
See http://www.atomicbird.com/mondomouse/

mlabs - 21 Aug 2007 06:45 GMT
thanks Tom - looks like the 'cocoa in a nutshell' book I got is out of
date - that class isn't in it...
mlabs - 22 Aug 2007 09:21 GMT
ok i have managed to confuse myself...
I've got my 'menulet' application running over on the right side of
the menu bar .. got my icon in there, got a menu hanging off it with 5
menu items ... got rid of the default menu and window ... the menulet,
containing the NSStatusItem reference, is derived from NSObject....
next I added 5 actions for the menu items.. hooked them up..
next I used IB to construct a panel... .. currently it's not hooked up
to anything..

what I want to do next is 2 things:

1. in one of the actions, bring up my panel, in a modal state...
Q: should I wrap this in it's own class??

2. in the 'about' action, bring up the standard about box..

no idea how to do #1.

as for #2, I read that NSApplication has a
'orderFrontStandardAboutPanel' message .. but I can't figure out how
to get to my NSApplication instance from the action handler..  is this
thing global?
Gregory Weston - 22 Aug 2007 12:51 GMT
> ok i have managed to confuse myself...

Excellent. A necessary early step. (And I'm only half kidding. I find
most people learn more when they don't initially just "get it.")

> I've got my 'menulet' application running over on the right side of
> the menu bar .. got my icon in there, got a menu hanging off it with 5
[quoted text clipped - 7 lines]
>
> 1. in one of the actions, bring up my panel, in a modal state...

How modal are we talking about? System-wide modality is pretty much
anathema for OS X and most other modern desktop operating systems. If
you just need to prevent access to other bits of your program while this
window is displayed, no problem.

> Q: should I wrap this in it's own class??

Probably not. You can probably get what you want with this method:
<http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/
Classes/NSApplication_Class/Reference/Reference.html#//apple_ref/occ/inst
m/NSApplication/beginModalSessionForWindow:>

> 2. in the 'about' action, bring up the standard about box..
>
[quoted text clipped - 4 lines]
> to get to my NSApplication instance from the action handler..  is this
> thing global?

There's a global instance of NSApplication called NSApp. Send it
messages and you should get what you want. If you're offended by the
notion of gratuitous globals you can use the NSApplication's class
method sharedApplication to get this object instead.

Many Cocoa classes that are at least treated as singletons have a
sharedFoo or standardFoo method to retrieve their "stock" instance, btw.
mlabs - 23 Aug 2007 02:59 GMT
thanks again Gregory - the tip regarding the globals and how to find
them was key. I feel like i'm getting the hang of this now .. i've got
my about box up and i've added a self-contianed prefs panel (in it's
own nib) and I have that hanging off one of the menu items as well.
It's all very spiffy. The action/outlet thing is just like DDX and
event handling in MFC .. once I had that epiphany I was good to go.
:-)
Michael Ash - 20 Aug 2007 15:52 GMT
> i'm a win32 programmer boldly going where few of us have gone
> before.. :)
>
> so before cocoa, there were no little icons over on the right side of
> the menu bar?

"Before Cocoa" is not a phrase which makes sense in this context. Cocoa is
a fundamental part of the operating system and there was never any version
of Mac OS X or its technological predecessors which did not have it.

Signature

Michael Ash
Rogue Amoeba Software

Gregory Weston - 20 Aug 2007 14:10 GMT
> I want to write the equivalent of a windows 'tray application' .. but
> for the mac.
[quoted text clipped - 5 lines]
> menu? It should have nothing else .. no menu items on the left side of
> the menu bar - no main window...

You set a key named LSUIElement to the string "1" in the app's property
list file.

Just a quick concern about terminology. I'm assuming when you say
"context menu" you mean that you'd like a menu to drop down from the
menu bar icon you plan on installing. Actually context menus -
right-click on some UI element like a file icon and have a menu of
relevant operations appear - is a different mechanism.

> Q: preferably I want to avoid learning objective C and stick with c++
> - so I guess Carbon is the way to go here... are there any examples of
> this sort of application written in Carbon?

I'm going to contradict the assertion instead of answering the explicit
question. You want to learn Objective-C. It'll take you all of a couple
hours at most and save more than that in the long run even if you never
release another Mac program again.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.