FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

SYSTEM

Send Apple Events


I am trying to add support for sending an Apple Event to another running application. I don't need to receive a reply, just send.

Do I need to set the high level event bit in the size resource if I only want to just send Apple Events?

Can I send events in the FB Runtime environment or do I need to have a compiled app to do this??

The code below will be used in conjunction with an ADB device from Sophisticated Circuits called a Powerkey Rebound. It is a really cool product that will restart your computer in the event of a crash. The hardware device will send the "Control" "Command", Power On" key combination to the CPU if the software doesn't communicate with the ADB device within a certain amount of time.

If someone can help me get this code working, it will allow an FB application to add support for the Rebound product so that if the FB app fails to communicate with the device, it will also restart your computer for you. (Anyone can use the following code in their app if they wish...)

The problem with the code is that when the ResetRebound FN is called, it hangs the computer at the AECreateAppleEvent FN. I can't figure out why???

The Apple Event functions were all created with FB's "Pascal Converter" from Apple's .p interface files.

Any of you Apple Event gurus have any ideas for me??

Bill Sanford

'------------------------  Apple Event Constants 

_keyDirectObject          = _"----"
_typeLongInteger          = _"long"
_typeApplSignature        = _"sign"
_typeNull                 = _"null"

_kAENoReply               = &00000001
_kAENeverInteract         = &00000010
_kAutoGenerateReturnID    = -1
_kAnyTransactionID        = 0
_kAEDefaultTimeout        = -1
_kAENormalPriority        = &00000000

_sizeOfLong               = &00000004

'--------------------- Apple Event Manager Records 

DIM RECORD AEDesc
  DIM descType&
  DIM descHandle&
DIM END RECORD.AEDesc

DIM RECORD AEKeyDesc
  DIM descKey&
  DIM descContent.AEDesc
DIM END RECORD.AEKeyDesc

DIM RECORD AEAddressDesc
  DIM addressDesc.AEDesc
DIM END RECORD.AEAddressDesc

DIM RECORD AEDescList
  DIM descList.AEDesc
DIM END RECORD .AEDescList

DIM RECORD AERecord
  DIM recDesList.AEDescList
DIM END RECORD .AERecord

DIM RECORD AppleEvent
  DIM AEventRecord.AERecord
DIM END RECORD .AppleEvent

'--------------------------------------------

LOCAL MODE
LOCAL FN AECreateDesc(typeCode&,dataPtr&,dataSize&,@result&)
  `     CLR.W     -(SP)
  `     MOVE.L    ^typeCode&,-(SP)                ;DescType
  `     MOVE.L    ^dataPtr&,-(SP)                 ;UNIVPtr
  `     MOVE.L    ^dataSize&,-(SP)                ;Size
  `     MOVE.L    ^result&,-(SP)                  ;Var: AEDesc
  `     DC.W      $303C,$0825,$A816
  `     MOVE.W    (SP)+,D0
  `     EXT.L     D0
END FN = REGISTER(D0)                             'OSErr

'--------------------------------------------

LOCAL MODE
LOCAL FN
AECreateAppleEvent(eventClass&,eventID&,@target&,returnID%,transactionID&,@result&)
  `     CLR.W     -(SP)
  `     MOVE.L    ^eventClass&,-(SP)              ;AEEventClass
  `     MOVE.L    ^eventID&,-(SP)                 ;AEEventID
  `     MOVE.L    ^target&,-(SP)                  ;Var: AEAddressDesc
  `     MOVE.W    ^returnID%,-(SP)                ;AEReturnID
  `     MOVE.L    ^transactionID&,-(SP)           ;AETransactionID
  `     MOVE.L    ^result&,-(SP)                  ;Var: AppleEvent
  `     DC.W      $303C,$0B14,$A816
  `     MOVE.W    (SP)+,D0
  `     EXT.L     D0
END FN = REGISTER(D0)                             'OSErr

'-----------------------------------------

LOCAL MODE
LOCAL FN AEPutParamDesc(@theAppleEvent&,theAEKeyword&,@theAEDesc&)
  `     CLR.W     -(SP)
  `     MOVE.L    ^theAppleEvent&,-(SP)           ;Var: AppleEvent
  `     MOVE.L    ^theAEKeyword&,-(SP)            ;AEKeyword
  `     MOVE.L    ^theAEDesc&,-(SP)               ;Var: AEDesc
  `     DC.W      $303C,$0610,$A816
  `     MOVE.W    (SP)+,D0
  `     EXT.L     D0
END FN = REGISTER(D0)                             'OSErr

'--------------------------------------------

LOCAL MODE
LOCAL FN
AESend(@theAppleEvent&,@reply&,sendMode&,sendPriority%,timeOutInTicks&,idleProc&,filterProc&)
  `     CLR.W     -(SP)
  `     MOVE.L    ^theAppleEvent&,-(SP)           ;Var: AppleEvent
  `     MOVE.L    ^reply&,-(SP)                   ;Var: AppleEvent
  `     MOVE.L    ^sendMode&,-(SP)                ;AESendMode
  `     MOVE.W    ^sendPriority%,-(SP)            ;AESendPriority
  `     MOVE.L    ^timeOutInTicks&,-(SP)          ;LONGINT
  `     MOVE.L    ^idleProc&,-(SP)                ;AEIdleUPP
  `     MOVE.L    ^filterProc&,-(SP)              ;AEFilterUPP
  `     DC.W      $303C,$0D17,$A816
  `     MOVE.W    (SP)+,D0
  `     EXT.L     D0
END FN = REGISTER(D0)                             'OSErr

'--------------------------------------------

LOCAL MODE
LOCAL FN xAEDisposeDesc(theAEDesc&)
  `     CLR.W     -(SP)
  `     MOVE.L    ^theAEDesc&,-(SP)               ;Var: AEDesc
  `     DC.W      $303C,$0204,$A816
  `     MOVE.W    (SP)+,D0
  `     EXT.L     D0
END FN = REGISTER(D0)                             'OSErr

'----------------------------------------------------

LOCAL MODE
LOCAL FN ShowMe(theText$, err)
  myText$ = STR$(err)
  PRINT theText$ ; " " ; myText$
  DO
  UNTIL FN BUTTON = _true
  DO
  UNTIL FN BUTTON = _false
END FN

'----------------------------------------------------

CLEAR LOCAL
DIM newAEDesc.AEDesc                         ' Descriptor record for Apple Event
DIM newDataDesc.AEDesc                       ' Descriptor record for data
DIM newAEvent.appleEvent                     ' A new Apple Event
DIM newAEreply.AEDesc
LOCAL FN ResetRebound(interval&)
  
  newAEDesc.descType&   = _typeNull
  newAEDesc.descHandle& = _nil
  
  newDataDesc.descType&   = _typeNull
  newDataDesc.descHandle& = _nil
  
  newAEvent.descType&   = _typeNull
  newAEvent.descHandle& = _nil
  
  targetSign& = _"Fido"
  
  osErr% = FN AECreateDesc(_typeApplSignature, @targetSign&, _sizeOfLong, newAEDesc)
  FN ShowMe("CreateAEDesc Err = ", osErr%)
  LONG IF osErr% = _noErr
                                    ' Create the data desc for the new timer value
    osErr% = FN AECreateDesc(_typeLongInteger, @interval&, _sizeOfLong, newDataDesc)
    FN ShowMe("CreateDataDesc Err = ", osErr%)
    LONG IF osErr%  = _noErr
      
      osErr% = FN AECreateAppleEvent(_"PKPr", _"Tick", newAEDesc, _kAutoGenerateReturnID, 
                                    _kAnyTransactionID, newAEvent)
      FN ShowMe("CreateAppleEvent Err = ", osErr%)
      LONG IF osErr% = _noErr
        
        osErr% = FN AEPutParamDesc(newAEvent, _keyDirectObject, newDataDesc)
        FN ShowMe("PutParamDesc Err = ", osErr%)
        LONG IF osErr% = _noErr
          
          osErr% = FN AESend(newAEvent, _ newAEreply,_kAENoReply+_kAENeverInteract, 
                         _kAENormalPriority ,_kAEDefaultTimeout, _nil, _nil)
          FN ShowMe("AESend Err = ", osErr%)
        END IF                             ' { END IF AEPutParamDesc= noErr}
      END IF                               ' { END IF AECreateAppleEvent = noErr }
    END IF                                 ' { END IF AECreateDesc = noErr }
  END IF                                   ' { END IF AECreateDesc = noErr }
  
  dErr% = FN xAEDisposeDesc(@newAEDesc)           'going
  dErr% = FN xAEDisposeDesc(@newDataDesc)         'going
  dErr% = FN xAEDisposeDesc(@newAEvent)           'gone
  dErr% = FN xAEDisposeDesc(@newAEreply)
END FN  = osErr%

'----------------------------------------------------
'----------------------------------------------------
'----------------------------------------------------

WINDOW #1

DO
  CLS
  err = FN ResetRebound(30)
  PRINT "Reset Error = " ; err
  
  nextTime& = TIMER + 10
  DO
    HANDLEEVENTS
  UNTIL FN BUTTON = _true OR TIMER > nextTime&
UNTIL FN BUTTON

err = FN ResetRebound(0)

WINDOW CLOSE #1
Jamin