 |
|
 |
|
Next: rotating an NSString
|
| Author |
Message |
External

Since: Jan 24, 2008 Posts: 1
|
(Msg. 1) Posted: Thu Jan 24, 2008 8:45 pm
Post subject: command line program in need of error message popup Archived from groups: comp>sys>mac>programmer>help (more info?)
|
|
|
I have a C/C++ program that I've ported from Windows to Mac OS X 10.4. It is
built using a traditional makefile and g++. While in development, I'm just
running it from a command line in Terminal. When released, it will just run
in the background.
The problem I have is this: in order to notify the user of error conditions,
I used MessageBox() in Windows, which pops up a window with the error message
text in it and an "OK" button. Is there an analogous function in MacOS,
without having to build some kind of Carbon/Cocoa application? I saw some
mention of Alert()/StandardAlert(), but the way I understand it, those are
used are part of Carbon. I also would like to avoid using Xcode (for
portability reasons).
Any advice would be well appreciated (please keep in mind that I've only been
digging through this Mac stuff for about a week now, so my terminology may
not be accurate).
Thanks. >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
External

Since: Aug 19, 2003 Posts: 1469
|
(Msg. 2) Posted: Thu Jan 24, 2008 8:45 pm
Post subject: Re: command line program in need of error message popup [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
In article , "lra" wrote:
> I have a C/C++ program that I've ported from Windows to Mac OS X 10.4. It is
> built using a traditional makefile and g++. While in development, I'm just
> running it from a command line in Terminal. When released, it will just run
> in the background.
>
> The problem I have is this: in order to notify the user of error conditions,
> I used MessageBox() in Windows, which pops up a window with the error message
> text in it and an "OK" button. Is there an analogous function in MacOS,
> without having to build some kind of Carbon/Cocoa application? I saw some
> mention of Alert()/StandardAlert(), but the way I understand it, those are
> used are part of Carbon. I also would like to avoid using Xcode (for
> portability reasons).
>
> Any advice would be well appreciated (please keep in mind that I've only been
> digging through this Mac stuff for about a week now, so my terminology may
> not be accurate).
I'd probably go with a CFUserNotification--
<http://developer.apple.com/documentation/CoreFoundation/Reference/CFUser
NotificationRef/Reference/reference.html>. It does require linking
CoreFoundation.framework, but honestly that's not a big deal.
In the simplest case it could be something like this:
CFUserNotificationDisplayNotice (
20.0, // Timeout, in seconds (use 0 for no timeout)
kCFUserNotificationCautionAlertLevel,
NULL,
NULL,
NULL,
CFSTR("Hey, something happened!"),
CFSTR("Your message here"), // Can embed newlines here
CFSTR("OK) // default button text
);
If you set a timeout, you can check the return value to see if the user
clicked the button or if the alert timed out.
--
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/ >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
External

Since: Dec 16, 2003 Posts: 288
|
(Msg. 3) Posted: Thu Jan 24, 2008 9:28 pm
Post subject: Re: command line program in need of error message popup [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
lra wrote:
> I have a C/C++ program that I've ported from Windows to Mac OS X 10.4. It is
> built using a traditional makefile and g++. While in development, I'm just
> running it from a command line in Terminal. When released, it will just run
> in the background.
>
> The problem I have is this: in order to notify the user of error conditions,
> I used MessageBox() in Windows, which pops up a window with the error message
> text in it and an "OK" button. Is there an analogous function in MacOS,
> without having to build some kind of Carbon/Cocoa application? I saw some
> mention of Alert()/StandardAlert(), but the way I understand it, those are
> used are part of Carbon. I also would like to avoid using Xcode (for
> portability reasons).
>
> Any advice would be well appreciated (please keep in mind that I've only been
> digging through this Mac stuff for about a week now, so my terminology may
> not be accurate).
>
How about using Growl ?
<http://growl.info/about.php>
Paul >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
External

Since: Dec 02, 2004 Posts: 191
|
(Msg. 4) Posted: Thu Jan 24, 2008 10:28 pm
Post subject: Re: command line program in need of error message popup [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"lra" wrote:
> I have a C/C++ program that I've ported from Windows to Mac OS X 10.4. It is
> built using a traditional makefile and g++. While in development, I'm just
> running it from a command line in Terminal. When released, it will just run
> in the background.
This aritcle may be of interest to you:
<http://developer.apple.com/technotes/tn2005/tn2083.html>
It sounds like you are making an 'agent' type of program.
> The problem I have is this: in order to notify the user of error conditions,
> I used MessageBox() in Windows, which pops up a window with the error message
> text in it and an "OK" button. Is there an analogous function in MacOS,
> without having to build some kind of Carbon/Cocoa application? I saw some
> mention of Alert()/StandardAlert(), but the way I understand it, those are
> used are part of Carbon.
If you want to display a GUI you'll have to link to a framework that
makes that happen (I guess this will be true on Windows too). Either
Cocoa or Carbon can do this. There is nothing special about
'Carbon/Cocoa applications'; a command line tool can link to Cocoa or
Carbon. You don't need to make a bundled app with an icon to use Cocoa
or Carbon.
Minimal Cocoa app:
main.m
------
// to compile: cc main.m -o hello -framework Cocoa
#import <Cocoa/Cocoa.h>
int main (int argc, char *argv[]) { NSLog(@"Hello world"); return 0; }
Adding alerts to your command line app will require only a few lines of
code + linking to Carbon or Cocoa, whichever you prefer (but Carbon may
be a closer fit with your C/C++ code base).
> I also would like to avoid using Xcode (for portability reasons).
Xcode is not a requirement for creating Mac programs (everyting Xcode
does can equally be done from the command line, although it can be a lot
of work...). In other words: Xcode adds no 'magic' ingredients to your
apps.
Hope this helps.
patrick >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
External

Since: Dec 02, 2004 Posts: 191
|
(Msg. 5) Posted: Fri Jan 25, 2008 11:58 am
Post subject: Re: command line program in need of error message popup [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"lra via MacKB.com" wrote:
> Here's essentially what I want to do:
>
> Given a "C" code fragment, such as:
> ...
> if (stat("/path/to/file", &statstruct))
> {
> SomeFunction("Can't stat() file: /path/to/file");
> /* clean-up code */
> exit(1);
> }
> ...
>
> What I need to know is what code I should replace "SomeFunction()" with to
> display a (modal) message box that will alert the user to the fatal error.
I'd forgotten about it, but Tom's suggestion of using CFUserNotification
is probably most suited. I imagine you could end up with code like this
(untested!)
// crossplatform.c
#ifdef __APPLE__
// also add linker flag: '-framework CoreFoundation'
#include <CoreFoundation/CoreFoundation.h>
#endif
void DisplayWarning(const char *title, const char *message)
{
#ifdef __APPLE__
// create CFStrings
CFStringRef titleStr = CFStringCreateWithCString(NULL, title,
kCFStringEncodingUTF8);
CFStringRef messageStr = CFStringCreateWithCString(NULL, message,
kCFStringEncodingUTF8);
// present warning
CFUserNotificationDisplayNotice (
0, // no timeout
kCFUserNotificationCautionAlertLevel,
NULL,
NULL,
NULL,
titleStr,
messageStr,
CFSTR("OK)
);
// clean up allocated memory
CFRelease(titleStr);
CFRelease(messageStr);
#else
// your Windows code with equal semantics
#endif
}
patrick >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
External

Since: Feb 18, 2004 Posts: 137
|
(Msg. 6) Posted: Fri Jan 25, 2008 8:47 pm
Post subject: Re: command line program in need of error message popup [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
In article , "lra" wrote:
> I have a C/C++ program that I've ported from Windows to Mac OS X 10.4. It is
> built using a traditional makefile and g++. While in development, I'm just
> running it from a command line in Terminal. When released, it will just run
> in the background.
>
> The problem I have is this: in order to notify the user of error conditions,
> I used MessageBox() in Windows, which pops up a window with the error message
> text in it and an "OK" button. Is there an analogous function in MacOS,
> without having to build some kind of Carbon/Cocoa application?
[lots of useful replies snipped for the sake of conciseness]
In article , "lra via MacKB.com"
clarified:
> I've done a lot of reading, but I'm still unsure how
> to proceed. Here's essentially what I want to do:
>
> Given a "C" code fragment, such as:
> ...
> if (stat("/path/to/file", &statstruct))
> {
> SomeFunction("Can't stat() file: /path/to/file");
> /* clean-up code */
> exit(1);
> }
> ...
>
> What I need to know is what code I should replace "SomeFunction()" with to
> display a (modal) message box that will alert the user to the fatal error.
If you (eventually) want to release a real Mac program, I suggest you
consider the following approach:
- replace
SomeFunction("Can't stat() file: /path/to/file");
by
fprintf( stderr, "Can't stat() file: /path/to/file");
(or the equivalent std::cerr << "...")
- wrap your command-line program in a 'real' Mac program
- have that Mac program call your command-line tool
(see
<http://www.cocoadev.com/index.pl?CocoaWrapperAroundConsoleApplication>)
This will clearly separate the platform-specific stuff from a (possibly
100% platform-independent) core. From the Mac program, it should be
simple to pop up a notification whenever something is written to
standard error.
If you go for a Mac program, also read up on the culture. "Can't stat()
file: /path/to/file" is not a good error message on the Mac, for several
reasons:
- error messages should be more polite ("I am sorry, but I could not...")
- error messages should be less technical (what is 'stat'? I know, but
Mac users will not)
- Mac users may not be that familiar with path separators
(see for example
<http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHI
Guidelines/XHIGWindows/chapter_18_section_7.html#//apple_ref/doc/uid/2000
0961-TPXREF23>)
Reinder >> Stay informed about: command line program in need of error message popup |
|
| Back to top |
|
 |  |
| Related Topics: | compiling command-line program on 10.3 for 10.2 - I have a (fairly simple) file checksumming utility that runs from the command line. I'm compiling on 10.3, however it immediatly gives a segmentation fault when run on 10.2.8. The devtools was installed with the options for creating programs that would...
How to run a command line tool? - Hi, A few of the Apple code examples build command line tools rather than proper apps. How do you 'run' these apps? I'm not talking about specific usage for any particular one, but just generally? This unix stuff is new to me. Thanks LB
Command-line arguments in Mac App - I'm porting a Unix program to Mac OS X. I've made a bundle for the app, and I'd like to be able to have the executable run with some command line arguments. I was hoping there was a way to specify the arguments in the Info.plist or something like..
command line application - Hello all: I am using MAC OS X.3 and THe latest version of Xcode (1.2 I think). How do I set up a project to make a simple terminal application? I am not going to use an cocoa/mac stuff, its just going to be a command line utility. Only standard....
starting a command line app from code - I want to start a command line program (deamon) from my Cocoa app. It seems there are a lot of ways to do this. I found at least four: - (void)startDeamon { NSString *execPath = @"path/to/executable"; NSString *execName = [execPath.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|