[futurebasic] Sending AppleEvents to System process

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Sat, 26 Jan 2008 00:11:43 +0100
Hi,

I've just tried to translate a snippet of code from Apple. It seems to 
work, at least with the sleep command (I have not tested the other 
possibilities). Perhaps, it might interest others. Use with caution.


#define AEEventID     as FourCharCode
#define AEAddressDesc as AEDesc

_kAEShutDown           = _"shut"
_kAESleep              = _"slep"
_kAELogOut             = _"logo"
_kAEReallyLogOut       = _"rlgo"
_kAEShowRestartDialog  = _"rrst"
_kAEShowShutdownDialog = _"rsdn"
_kAERestart            = _"rest"
_kAEQuitAll            = _"quia"



clear local mode
local fn SendAppleEventToSystemProcess( myEventToSend as AEEventID )
dim as AEAddressDesc       targetDesc
dim as ProcessSerialNumber PSNOfSystemProcess
dim as AppleEvent          eventReply, appleEventToSend
dim as OSStatus            err, ignore

PSNOfSystemProcess.lowLongOfPSN = _kSystemProcess
err = fn AECreateDesc(_typeProcessSerialNumber, PSNOfSystemProcess, 
                        sizeof(PSNOfSystemProcess), @targetDesc)
long if (err == _noErr)
err = fn AECreateAppleEvent(_kCoreEventClass, myEventToSend, 
     targetDesc, _kAutoGenerateReturnID, _kAnyTransactionID, 
@appleEventToSend)
ignore = fn AEDisposeDesc(targetDesc)
long if (err == _noErr )
err = fn AESend(appleEventToSend, eventReply, 
     _kAENoReply, _kAENormalPriority, _kAEDefaultTimeout, _nil, _nil)
ignore = fn AEDisposeDesc(appleEventToSend)
long if (err == _noErr)
ignore = fn AEDisposeDesc(eventReply)
end if
end if
end if

end fn = err

// main program
long if ( fn SendAppleEventToSystemProcess(_kAESleep) )
print "Gloops! Something went wrong."
end if


do
handleevents
until 0

Alain