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

DISK I/O

Handle incoming files with Apple events


Sorry in advance for the long post, but I thought some of you might like a minature AppleEventShell.
Thanks to David Blache for showing me the "Single AE Handler" technique used in this demo - It will help you cut down the amount of code in the first segment. Remember that if your not a "Rez Tolz" user, then you will have to modify the size resource yourself. Also watch for the longer lines of code being wrapped or split by your mail package.

Jamin

WINDOW OFF : COORDINATE WINDOW
POKE LONG EVENT-8, 50 ' Use WaitNextEvent
OUTPUT FILE "CoreAppleEventsShell"

DIM gUserQuits%
END GLOBALS

COMPILE 0,_strResource ' Added by Rez
Rez$ = "^SIZE: 500/500"
Rez$ = "^APPLE EVENTS: ON"
Rez$ = "^REMOTE APPLE EVENTS: OFF"

GOTO "End of ENTERPROCs"

"AppleEventHandlers"
ENTERPROC(procAEvtPtr&,procAEvtReplyPtr&,procAEvtRefCon&)
osErr% = _noErr
SELECT procAEvtRefCon&
CASE _"oapp"
PRINT "Open Application AppleEvent"
CASE _"odoc"
PRINT "Open Document AppleEvent"
CASE _"quit"
PRINT "Quit Application AppleEvent"
gUserQuits% = _true
END SELECT
EXITPROC = osErr%

"End of ENTERPROCs"

LOCAL FN InstallAEHandlers
osErr% = _noErr
LONG IF SYSTEM(_sysVers) > 699
LONG IF FN GESTALT(_gestaltAppleEventsAttr)
AEEventProcPtr& = LINE "AppleEventHandlers"
IF osErr% = _noErr THEN osErr% = FN AEINSTALLEVENTHANDLER(_"aevt", _"oapp", AEEventProcPtr&,_"oapp",_false)
IF osErr% = _noErr THEN osErr% = FN AEINSTALLEVENTHANDLER(_"aevt", _"quit", AEEventProcPtr&,_"quit",_false)
IF osErr% = _noErr THEN osErr% = FN AEINSTALLEVENTHANDLER(_"aevt", _"odoc", AEEventProcPtr&,_"odoc",_false)
END IF
END IF
IF osErr% <> _noErr THEN END
END FN

WINDOW 1,"Command Period to Quit"
FN InstallAEHandlers

DO
HANDLEEVENTS
UNTIL gUserQuits%
END


The excerpted code is below. Keep it in the 1st segment.

'---------------------------------- Apple Events ------------------------------

CLEAR LOCAL
LOCAL FN AEErrorHandler(which, osErr)
DIM q, tmp$, item, sH&
LONG IF osErr = 0
tmp$ = "0(0)"
XELSE
tmp$ = STR$(which) + "(" + STR$(osErr) +")"
END IF
CALL ERRORSOUND(0)
sH& = FN WeHaveAProblem&(2010)
CALL PARAMTEXT(tmp$,"","","")
item = FN ALERT(_AEErrAlert, 0)
FN WaitDisposeSound(sH&)
END FN

CLEAR LOCAL
LOCAL FN AEReadDocList(procAEvtPtr&, printFlag)
DIM 255 paramBlock$,pbPtr&,1 tmp$
DIM AEDesc&;0, descType&, dataHndl&
DIM fsSpec;0, fsVRefNum%, fsParID&, 63 fsName$, fsSize&
DIM osErr, totalDescItems&, iCount, volRefNum
DIM fileType&, keyWord&, returnType&, actualSize&
pbPtr& = @paramBlock$
fsSize& = @fsSize& - @fsSpec
osErr = FN AEGETPARAMDESC(procAEvtPtr&, _keyDirectObject, _typeAEList, AEDesc&)
LONG IF osErr = _noErr
osErr = FN AECOUNTITEMS(AEDesc&, totalDescItems&)
LONG IF osErr = _noErr
LONG IF totalDescItems& > 1
BEEP
END IF
osErr = FN AEGETNTHPTR(AEDesc&, 1, _typeFSS, keyWord&, returnType&, @fsSpec, fsSize&, actualSize&)
LONG IF osErr = _noErr
COMPILE LONG IF 0
DIM s$
DEFSTR LONG
s$ = "fsVRefNum%: " + STR$(fsVRefNum%) + CHR$(_CR)
s$ = s$ + "fsParID&: " + HEX$(fsParID&) + CHR$(_CR)
s$ = s$ + "fsName$: " + fsName$
CALL DEBUGSTR(s$)
COMPILE END IF
pbPtr&.ioNamePtr& = @tmp$
pbPtr&.ioVRefNum% = fsVRefNum%
pbPtr&.ioWDDirID& = fsParID&
osErr = FN OPENWD(pbPtr&)
LONG IF osErr = _noErr
volRefNum = pbPtr&.ioVRefNum%
pbPtr&.ioNamePtr& = @fsName$
pbPtr&.ioFDirIndex& = 0
osErr = FN GETFILEINFO(pbPtr&)
LONG IF osErr = _noErr
fileType& = pbPtr&.ioBuffer&
LONG IF fileType& = _"TRCT"
gFilename$ = fsName$
gVolRefNo = volRefNum
LONG IF printFlag
BEEP
XELSE
IF gSubsequent THEN FN FNLoadTrct
END IF
XELSE
BEEP
osErr = _paramErr
FN AEErrorHandler(1,osErr)
END IF 'end filetype check
XELSE
FN AEErrorHandler(2,osErr)
END IF 'end getfileinfo
XELSE
FN AEErrorHandler(3,osErr)
END IF 'end openwd
XELSE
FN AEErrorHandler(4,osErr)
END IF 'end getnthptr
XELSE
FN AEErrorHandler(5,osErr)
END IF 'end countitems
XELSE
FN AEErrorHandler(6,osErr)
END IF 'end getparamdesc
IF AEDesc& <> 0 THEN osErr = FN AEDISPOSEDESC (AEDesc&)
IF osErr THEN FN AEErrorHandler(7,osErr)
END FN = osErr

"AEOpenDocument"
DIM procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&
ENTERPROC(procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&)
FN AEReadDocList(procAEvtPtr&, _false)
EXITPROC

"AEPrintDocument"
ENTERPROC(procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&)
FN AEReadDocList(procAEvtPtr&, _zTrue)
EXITPROC

"AEOpenApplication"
ENTERPROC(procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&)
EXITPROC

"AEQuitApplication"
ENTERPROC(procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&)
FN CleanUp
FN QuitSound
gThatsAllFolks = _zTrue
EXITPROC

"AEActivate"
ENTERPROC(procAEvtPtr&, procAEvtReplyPtr&, procAEvtRefCon&)
LONG IF FN TAppInFront <> _zTrue
FN TAppToFront
END IF
EXITPROC

CLEAR LOCAL
LOCAL FN InstallAppleEvents
DIM osErr
LONG IF SYSTEM(_sysVers) > 699 AND FN GESTALT(_gestaltAppleEventsAttr)
POKE WORD EVENT-8, 3
gOpenApplAEPtr& = LINE "AEOpenApplication"
osErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEOpenApplication, gOpenApplAEPtr&, _false, _false)
gQuitApplAEPtr& = LINE "AEQuitApplication"
osErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEQuitApplication, gQuitApplAEPtr&, _false, _false)
gOpenDocAEPtr& = LINE "AEOpenDocument"
osErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEOpenDocuments, gOpenDocAEPtr& , _false, _false)
gPrintDocAEPtr& = LINE "AEPrintDocument"
osErr = FN AEINSTALLEVENTHANDLER(_kCoreEventClass, _kAEPrintDocuments, gPrintDocAEPtr&, _false, _false)
gActivateAEPtr& = LINE "AEActivate"
osErr = FN AEINSTALLEVENTHANDLER(_kMiscEventClass, _kAEActivate, gActivateAEPtr&, _false, _false)
XELSE
FN AEErrorHandler(8,0)
END
END IF
IF osErr FN AEErrorHandler(9,osErr) : END
END FN

CLEAR LOCAL
LOCAL FN TAppInFront
DIM process.procSerNumRecSize
DIM tProcess.procSerNumRecSize
DIM osErr, sameProc
process.psnHigh& = 0
process.psnLow& = _kNoProcess
tProcess.psnHigh& = 0
tProcess.psnLow& = _kCurrentProcess
osErr = FN GetCurrentProcess(tProcess)
osErr = FN GetFrontProcess(process)
osErr = FN SameProcess(tProcess, process, sameProc)
END FN = sameProc

CLEAR LOCAL
LOCAL FN TAppToFront
DIM osErr, tProcess.procSerNumRecSize
tProcess.psnHigh& = 0
tProcess.psnLow& = _kCurrentProcess
osErr = FN GetCurrentProcess(tProcess)
osErr = FN SetFrontProcess(tProcess)
END FN

Charlie Dickman


Thanks to some private email help from Charlie Dickman I finally got Apple Events working properly in my little program. Just a note of warning to anyone wanting to do AE stuff. If you copy the code from the on disk Apple Events Handbook demo be warned that the function FN DoEvents needs the following line added as per "Handling high level events" page 165 of FB Handbook;

theError% = FN AEPROCESSAPPLEEVENT(EVENT)

so it looks like this;

LOCAL FN DoEvent
macEvnt% = {EVENT}
LONG IF macEvnt% = _khighlevelevent
theError% = FN AEPROCESSAPPLEEVENT(EVENT); '**this is needed
END IF
END FN

On disk demo only has;

LOCAL FN DoEvent
macEvnt% = {EVENT}
LONG IF macEvnt% = _khighlevelevent
'PRINT : PRINT "Apple Event:";
END IF
END FN

theError% = FN AEPROCESSAPPLEEVENT(EVENT)

is what actually processes the Apple Events

Probably most of you who replied already know this, but for those of us who are just beginning to use AE, the on disk demo is verrrry misleading by not having this function.

I see that the "incoming files" thread is now on a very different track.

Thanks to all who replied to my original question;

I still have an outstanding question; can I have a splash screen in my program, and how do I bypass it or get it to close if I'm dragging files to my program icon?? Will the presence of a splash screen interupt the automatic opening of the program and processing of incoming files??

I've commented out

'cntr%=FN ALERT(_AboutParsePara,_nil)

in MAIN

Bob