How to react to Apple events

  • Hello,

    I'm trying to create a program that will perform some action when my
    computer awakes from sleep.  I've found the NSAppleEventManager class
    that seems to be the one that I should use and I've located the
    eventclass(kAEMacPowerMgtEvt) and event(kAEMacToWake) ids that I believe
    relate to the awake from sleep event.  But the code I've written doesn't
    work.  Here's the code I've written.

    I do this to register my intent to handle the event:

    - (void) awakeFromNib
    {
        NSAppleEventManager* eventManager = [NSAppleEventManager
    sharedAppleEventManager];

        [eventManager setEventHandler: self
                          andSelector:
    @selector(handleAppleEvent:withReplyEvent:)
                        forEventClass: kAEMacPowerMgtEvt
                            andEventID: kAEMacToWake];
    }

    and I have this method to handle the event:

    - (void) handleAppleEvent: (NSAppleEventDescriptor*) event
                withReplyEvent: (NSAppleEventDescriptor*) replyEvent
    {
        NSLog(@"Awake!");
    }

    But I never seem to get any notification of the event when the computer
    wakes up.  The documentation on
    NSAppleEventManager>>setEventHandler:andSelector:forEventClass:andEventId:
      doesn't seem very clear to me.  Can anyone help me see what I'm doing
    wrong here.

    Kind regards,
    Jens
  • Hello Jens,

    If you wish to register for sleep events look in the IOPMLib.h header
    file, basically what you have to do is the following:

    io_connect_t        groot_port;

    void callback(void * x,io_service_t y,natural_t messageType,void *
    messageArgument)
    {
        switch ( messageType ) {
        case kIOMessageSystemWillSleep:
              // COMPUTER WILL GO TO SLEEP.
            IOAllowPowerChange(groot_port,(long)messageArgument);
            break;
        case kIOMessageCanSystemSleep:
              // Add code to decide whether to allow sleep or not.
    //        IOCancelPowerChange(groot_port,(long)messageArgument);
            IOAllowPowerChange(groot_port,(long)messageArgument);
            break;
        case kIOMessageSystemHasPoweredOn:
            break;
        }

    }

    int  main(int argc, char **argv)
    {
        IONotificationPortRef    notify;
        io_object_t         anIterator;

        groot_port = IORegisterForSystemPower
    (0,&notify,callback,&anIterator);
        if ( groot_port == NULL ) {
                printf("IORegisterForSystemPower failed\n");
                return 1;
        }


        CFRunLoopAddSource(CFRunLoopGetCurrent(),
                            IONotificationPortGetRunLoopSource(notify),
                            kCFRunLoopDefaultMode);

        return NSApplicationMain(argc, argv);

    }

    > Hello,
    >
    > I'm trying to create a program that will perform some action when my
    > computer awakes from sleep.  I've found the NSAppleEventManager class
    > that seems to be the one that I should use and I've located the
    > eventclass(kAEMacPowerMgtEvt) and event(kAEMacToWake) ids that I believe
    > relate to the awake from sleep event.  But the code I've written doesn't
    > work.  Here's the code I've written.
    >
    > I do this to register my intent to handle the event:
    >
    > - (void) awakeFromNib
    > {
    > NSAppleEventManager* eventManager = [NSAppleEventManager
    > sharedAppleEventManager];
    >
    > [eventManager setEventHandler: self
    > andSelector:
    > @selector(handleAppleEvent:withReplyEvent:)
    > forEventClass: kAEMacPowerMgtEvt
    > andEventID: kAEMacToWake];
    > }
    >
    > and I have this method to handle the event:
    >
    > - (void) handleAppleEvent: (NSAppleEventDescriptor*) event
    > withReplyEvent: (NSAppleEventDescriptor*) replyEvent
    > {
    > NSLog(@"Awake!");
    > }
    >
    > But I never seem to get any notification of the event when the computer
    > wakes up.  The documentation on
    > NSAppleEventManager> > setEventHandler:andSelector:forEventClass:andEventId:
    > doesn't seem very clear to me.  Can anyone help me see what I'm doing
    > wrong here.
    >
    > Kind regards,
    > Jens
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev

    -----------------------------------------------------------------------
    Lawrence R. Nipko
    Macintosh Software Development Supervisor    <lnipko...>
    Mac Division of Smith Micro Software, Inc.  http://www.smithmicro.com
    1260 NE Windsor Drive                        Voice: 816.554.8565 x116
    Lee's Summit MO 64086                        Fax: 816.554.9397
    -----------------------------------------------------------------------
  • Thank you Larry,

    The code sample you send looks like it will do what I want, but I had
    hoped to be able to do this in Objective C since the rest of the
    application is written in ObjC.  It is a bit inconvenient to have to
    locate the object I want to work with and send it messages from C;
    especially since the object is instantiated from a nib file.  I will
    figure out how to do this if there isn't any way to be notified in ObjC.

    But first I would like to restate my question to the list.  Is there any
    way to receive notification of sleep events from the system through some
    class in the Cocoa framework?  E.g. the NSAppleEventManager class I
    refered to in my original post.  If not what are the constants
    kAEMacPowerMgtEvt and kAEMacToWake used for.

    Kind regards,
    Jens
  • >
    > But first I would like to restate my question to the list.  Is there
    > any way to receive notification of sleep events from the system through
    > some class in the Cocoa framework?

    No.  You will need to register for IOKit notifications (not through
    Cocoa).  Read the archives of this list.  Sample code was posted last
    year.

    Eric

    > E.g. the NSAppleEventManager class I refered to in my original post.
    > If not what are the constants kAEMacPowerMgtEvt and kAEMacToWake used
    > for.
    >
    > Kind regards,
    > Jens
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • On Tuesday, February 26, 2002, at 12:03  PM, Larry Nipko wrote:

    > Hello Jens,
    >
    > If you wish to register for sleep events look in the IOPMLib.h header
    > file, basically what you have to do is the following:
    >
    > io_connect_t        groot_port;
    >
    >
    > void callback(void * x,io_service_t y,natural_t messageType,void *
    > messageArgument)
    > {
    > switch ( messageType ) {
    > case kIOMessageSystemWillSleep:
    > // COMPUTER WILL GO TO SLEEP.
    > IOAllowPowerChange(groot_port,(long)messageArgument);
    > break;
    > case kIOMessageCanSystemSleep:
    > // Add code to decide whether to allow sleep or not.
    > //        IOCancelPowerChange(groot_port,(long)messageArgument);
    > IOAllowPowerChange(groot_port,(long)messageArgument);
    > break;
    > case kIOMessageSystemHasPoweredOn:
    > break;
    > }
    >
    > }
    >
    > int  main(int argc, char **argv)
    > {
    > IONotificationPortRef    notify;
    > io_object_t         anIterator;
    >
    > groot_port = IORegisterForSystemPower
    > (0,&notify,callback,&anIterator);
    > if ( groot_port == NULL ) {
    > printf("IORegisterForSystemPower failed\n");
    > return 1;
    > }
    >
    >
    > CFRunLoopAddSource(CFRunLoopGetCurrent(),
    > IONotificationPortGetRunLoopSource(notify),
    > kCFRunLoopDefaultMode);
    >
    > return NSApplicationMain(argc, argv);
    >
    > }
    >
    >
    >
    >> Hello,
    >>
    >> I'm trying to create a program that will perform some action when my
    >> computer awakes from sleep.  I've found the NSAppleEventManager class
    >> that seems to be the one that I should use and I've located the
    >> eventclass(kAEMacPowerMgtEvt) and event(kAEMacToWake) ids that I
    >> believe
    >> relate to the awake from sleep event.  But the code I've written
    >> doesn't
    >> work.  Here's the code I've written.
    >>
    >> I do this to register my intent to handle the event:
    >>
    >> - (void) awakeFromNib
    >> {
    >> NSAppleEventManager* eventManager = [NSAppleEventManager
    >> sharedAppleEventManager];
    >>
    >> [eventManager setEventHandler: self
    >> andSelector:
    >> @selector(handleAppleEvent:withReplyEvent:)
    >> forEventClass: kAEMacPowerMgtEvt
    >> andEventID: kAEMacToWake];
    >> }
    >>
    >> and I have this method to handle the event:
    >>
    >> - (void) handleAppleEvent: (NSAppleEventDescriptor*) event
    >> withReplyEvent: (NSAppleEventDescriptor*) replyEvent
    >> {
    >> NSLog(@"Awake!");
    >> }
    >>
    >> But I never seem to get any notification of the event when the computer
    >> wakes up.  The documentation on
    >> NSAppleEventManager>>setEventHandler:andSelector:forEventClass:andEventId:
    >> doesn't seem very clear to me.  Can anyone help me see what I'm doing
    >> wrong here.
    >>
    >> Kind regards,
    >> Jens
    >>
    >> _______________________________________________
    >> MacOSX-dev mailing list
    >> <MacOSX-dev...>
    >> http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
    >
    > -----------------------------------------------------------------------
    > Lawrence R. Nipko
    > Macintosh Software Development Supervisor    <lnipko...>
    > Mac Division of Smith Micro Software, Inc.  http://www.smithmicro.com
    > 1260 NE Windsor Drive                        Voice: 816.554.8565 x116
    > Lee's Summit MO 64086                        Fax: 816.554.9397
    > -----------------------------------------------------------------------
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
  • Sorry, sent a blank message last time.

    I tried this code, but it keeps giving me an error of
    /usr/bin/ld:
    /Programming/WakeActions/build/intermediates/WakeActions.build/master.o
    illegal reference to symbol: _IORegisterForSystemPower defined in
    indirectly referenced dynamic library
    /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit

    What am I doing wrong? :(
    On Tuesday, February 26, 2002, at 12:03  PM, Larry Nipko wrote:

    > Hello Jens,
    >
    > If you wish to register for sleep events look in the IOPMLib.h header
    > file, basically what you have to do is the following:
    >
    > io_connect_t        groot_port;
    >
    >
    > void callback(void * x,io_service_t y,natural_t messageType,void *
    > messageArgument)
    > {
    > switch ( messageType ) {
    > case kIOMessageSystemWillSleep:
    > // COMPUTER WILL GO TO SLEEP.
    > IOAllowPowerChange(groot_port,(long)messageArgument);
    > break;
    > case kIOMessageCanSystemSleep:
    > // Add code to decide whether to allow sleep or not.
    > //        IOCancelPowerChange(groot_port,(long)messageArgument);
    > IOAllowPowerChange(groot_port,(long)messageArgument);
    > break;
    > case kIOMessageSystemHasPoweredOn:
    > break;
    > }
    >
    > }
    >
    > int  main(int argc, char **argv)
    > {
    > IONotificationPortRef    notify;
    > io_object_t         anIterator;
    >
    > groot_port = IORegisterForSystemPower
    > (0,&notify,callback,&anIterator);
    > if ( groot_port == NULL ) {
    > printf("IORegisterForSystemPower failed\n");
    > return 1;
    > }
    >
    >
    > CFRunLoopAddSource(CFRunLoopGetCurrent(),
    > IONotificationPortGetRunLoopSource(notify),
    > kCFRunLoopDefaultMode);
    >
    > return NSApplicationMain(argc, argv);
    >
    > }
    >
    >
    >
    >> Hello,
    >>
    >> I'm trying to create a program that will perform some action when my
    >> computer awakes from sleep.  I've found the NSAppleEventManager class
    >> that seems to be the one that I should use and I've located the
    >> eventclass(kAEMacPowerMgtEvt) and event(kAEMacToWake) ids that I
    >> believe
    >> relate to the awake from sleep event.  But the code I've written
    >> doesn't
    >> work.  Here's the code I've written.
    >>
    >> I do this to register my intent to handle the event:
    >>
    >> - (void) awakeFromNib
    >> {
    >> NSAppleEventManager* eventManager = [NSAppleEventManager
    >> sharedAppleEventManager];
    >>
    >> [eventManager setEventHandler: self
    >> andSelector:
    >> @selector(handleAppleEvent:withReplyEvent:)
    >> forEventClass: kAEMacPowerMgtEvt
    >> andEventID: kAEMacToWake];
    >> }
    >>
    >> and I have this method to handle the event:
    >>
    >> - (void) handleAppleEvent: (NSAppleEventDescriptor*) event
    >> withReplyEvent: (NSAppleEventDescriptor*) replyEvent
    >> {
    >> NSLog(@"Awake!");
    >> }
    >>
    >> But I never seem to get any notification of the event when the computer
    >> wakes up.  The documentation on
    >> NSAppleEventManager>>setEventHandler:andSelector:forEventClass:andEventId:
    >> doesn't seem very clear to me.  Can anyone help me see what I'm doing
    >> wrong here.
    >>
    >> Kind regards,
    >> Jens
    >>
    >> _______________________________________________
    >> MacOSX-dev mailing list
    >> <MacOSX-dev...>
    >> http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
    >
    > -----------------------------------------------------------------------
    > Lawrence R. Nipko
    > Macintosh Software Development Supervisor    <lnipko...>
    > Mac Division of Smith Micro Software, Inc.  http://www.smithmicro.com
    > 1260 NE Windsor Drive                        Voice: 816.554.8565 x116
    > Lee's Summit MO 64086                        Fax: 816.554.9397
    > -----------------------------------------------------------------------
    >
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
    >
  • You are not linking to the framework `IOKit'.

    Thanks,
    Andrew Pinski

    On Saturday, March 9, 2002, at 10:54 , Kent Sutherland wrote:

    > Sorry, sent a blank message last time.
    >
    > I tried this code, but it keeps giving me an error of
    > /usr/bin/ld:
    > /Programming/WakeActions/build/intermediates/WakeActions.build/master.o
    > illegal reference to symbol: _IORegisterForSystemPower defined in
    > indirectly referenced dynamic library
    > /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
    >
    > What am I doing wrong? :(
    > On Tuesday, February 26, 2002, at 12:03  PM, Larry Nipko wrote:
previous month february 2002 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28      
Go to today