TweetFollow Us on Twitter

Chain Events
Volume Number:9
Issue Number:12
Column Tag:Jörg's Folder

Related Info: Event Manager Apple Event Mgr

Apple Event Handlers in MacForth

Changing the main event loop with executable chains

By Jörg Langowski, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Which Macintosh programming environment - other than Forth - do you know where you can change the main event loop on the fly, plug in new routines or change them while you’re trying them out, or even while your application is running? Maybe Lisp, but certainly none of the development systems (C and C++ of various flavors, Pascal, maybe Fortran and Basic) that account for the majority of all applications on the market. You can solve some of these problems through runtime binding in C++, but for developing a program you still face that edit/compile/link/crash cycle. When I did the example for this column, implementing high-level event support in MacForth, I had plenty of opportunity to appreciate the incremental compiling that Forth offers, together with a good low-level debugger like TMON.

So after having seen how to use Apple events in Fortran and C++, you knew that a Forth example on that subject was just unavoidable. MacForth 4.0, the version that I am still using, does not offer built-in support for high level events. I’m not sure whether the latest 4.2 update does, but I’ll keep you informed as soon as I receive it. Anyway, for the sake of example it is better to use a version that does not support Apple events, because then you can see how to do it.

You remember that an application that supports Apple events has to tell this to the system. There is a bit in the SIZE resource reserved for this purpose. So the first step to make our MacForth kernel high-level event aware is to use ResEdit and set this bit to 1. Now the application’s name will, e.g., be visible in the PPC browser dialog when you select an application to interact with. Now comes the actual work, implementing an Apple event handler in MacForth.

Hooking into the main event loop

Apple events have an event code of 23. Older applications that did not handle Apple events did not check for this code. So how do you persuade the MacForth runtime system to react to high-level events, that is, how does one change the main event loop? (Let’s assume we don’t have access to the source). Easy. MacForth solves this and related problems - like how do you change its behavior on startup and during quit - through the concept of executable chains. These are lists of Forth words that are executed in sequence on various occasions; for instance, the Eventer chain is executed on each pass through the main event loop. There is a standard mechanism to add new Forth words to this chain. If you would add a word to the Eventer chain, all you do is to write Eventer linked followed by your word’s definition (see near the end of the example listing). This word is then automatically linked into the main event loop.

Words linked into the Eventer chain take an event code as a parameter and leave an event code (which may be different from the original one) plus a flag on the stack. The flag tells the system whether the event has been processed (true) or not (false). Thus, we’ll define a word that looks for event code 23 and if it finds it, calls ProcessAppleEvent, the routine that finds the handler corresponding to the Apple event received.

Event handlers

The handlers themselves are installed in a dispatch table through the Forth word Install.Event.Handler (see listing). The parameters to the trap are, from bottom to top of stack:

• the event class specifier (32 bit);

• the event ID (32 bit);

• a pointer to the handler routine (32 bit);

• a reference constant (32 bit);

• a boolean (16 bit) which is true if the handler is installed in the system event dispatch table, false otherwise.

The event class for the required Apple events is ‘aevt’, and the event IDs are ‘oapp’, ‘odoc’, ‘pdoc’, and ‘quit’. We will define handlers for all these events except ‘oapp’.

The address of the handler that is passed to the AEInstallEventHander trap must be a pointer to a routine that follows Pascal calling conventions. Of course, a standard MacForth word doesn’t do that, and you have to define the handler routines in a particular way. MacForth offers two ‘filter routine’ definition words, filter: and ;filter that can be used for that purpose. If you use them instead of : and ; at the start and end of a Forth definition, the resulting word will be callable as a subroutine from machine language. All the word does on execution is to put the entry address of the routine on the stack, which can then be passed as a parameter to a toolbox trap.

The first handler that we’ll write is for the ‘quit’ event. It seems pretty simple; just call ExitToShell and you’re done. But wait we can’t pull the rug from underneath other things that may be going on in the run time system; e.g. open files that have been edited and are not saved, etc. There is a Forth word, bye, that quits the MacForth system gracefully and will for instance present you with the ‘Save Changes ’ dialog box when there are unsaved files. So why not call bye from within the handler? I’ve tried that, with spectacular crashes as a result. This approach does not work because the handler routine would never return correctly, and I guess this leaves things hanging in the air that should be safely fixed on the ground. A safe way to quit is to install a token for a word that quits in the event.filter variable. If this variable is nonzero, MacForth assumes that it contains the pointer to a routine that is called on every pass through the main event loop. We now define a word ciao which resets event.filter to zero and then calls bye, and the quit handler will install the token for this word in event.filter. This way we make sure that ciao is called only once when the ‘quit’ event is received.

You can try out the ‘quit’ handler by using any of the tools to send an Apple event to other applications (there is a Hypercard stack and an Apple event test application available on the Apple ETO CD-ROM, and I’m sure there are public domain programs floating around in cyberspace). You’ll see that MacForth quits, and if there is any open file with unsaved changes, opens the dialog that asks you whether you want to save the changes. I had some problems with the quit handler when I used it in connection with the extended editor (see my last Forth column), and don’t know the reason for sure, but with the standard Sibley editor it works alright.

The other two handlers are for opening and printing documents from the finder. They are very similar, and one could easily modify this so that only one handler is called for both purposes, the switch between open and print behavior being done through the reference constant that is passed when the handler is installed.

These handlers are direct translations from the proposed ‘odoc’ and ‘pdoc’ handlers in Inside Mac vol. 6. First, we get the parameter descriptor out of the Apple event that corresponds to a list of files to be opened or printed. The keyword for this descriptor is ‘list’. The descriptor returned consists of two 32-bit words, the first one containing the parameter type (‘list’), the second one a handle to this parameter. The address of the descriptor is then passed to a routine that counts the number of items in the list (CountItems), and then a loop is executed that steps an index starting at 1 to the number of items, and opens - or prints - each item. The information about the file to be opened is obtained through the routine Get.Nth.Ptr which gets the data corresponding to the n-th item out of a descriptor list. The parameters taken by the trap, AEGetNthPtr, used in this routine are the following:

• a pointer to the descriptor list (32 bits);

• the number of the desired item (32 bits);

• the type of the desired item (32 bits);

• a keyword if one is associated with the descriptor record (32 bits);

• the descriptor type of the item returned (32 bits);

• a pointer to the data buffer (32 bits);

• the maximum size of the data to be returned (32 bits);

• the address of a variable that contains the actual size of the data returned (32 bits).

We call this trap with our descriptor list as a parameter, and specifying that we want the data to be returned in the format of a file system specifier (‘fss ’). Actually, the information about the files to be processed in the ‘odoc’ and ‘pdoc’ Apple events is in the format of alias records, which allows file aliases to be processed transparently. If we specify as the desired type ‘fss ’, the Apple Event manager will automatically ‘coerce’ the alias data to the FSS format, thus resolve the alias. You don’t have to worry about this, all that’s important is that the buffer myFSS contains the volume reference number (16 bits), the directory ID (32 bits), and the file name (63 byte string), in that order. After the successful call to GetNthPtr, this information is extracted from the buffer and used to call either of two routines: se.open$ for opening a file in a new editor window, or (paper) for printing it.

After all these preliminaries, try and load the Forth code, and then drag a few files from the finder to the MacForth item, or select the print command. You’ll see how it works. If it doesn’t, make sure that you have modified your SIZE resource with ResEdit so that the high-level events bit is set.

Things to be resolved

There remain quite a few things that could still be done, for instance, I haven’t told you how to install Apple event handlers in a stand-alone MacForth application, or how to handle other types of Apple events. In fact, I have a very nice project: to install a ‘dosc’ handler in MacForth that interprets Forth code that is sent from other applications, and send back the result in text form. Sort of a Forth tool server. But this is something that you may find in a later column. Until then, happy threading.

Example: Implementing the required Apple events in MacForth 4.0
anew testAE


\ generic interface word to the AE manager

<code AEPack
 popd0, \ pop selector into d0
 MAC Pack8 w,
 next,


\ some AE routines that we need in this example
\ the Pack8 routines all return a 16-bit result,
\ so we have to leave space below the parameters
\ on the stack. Here, we push a 32-bit 0 and later shift the 
\ result code by 16 bit ( hex 10000 / )

hex
: Process.Apple.Event
 0 event.record 021b AEPack 10000 /
;

: Install.Event.Handler  ( AEclass AEID AEproc -- OSErr )      
 0 3 bury ( space for result)
 0 ( refcon ) 0 0 wbury ( false ) 091f ( selector )
 AEPack 10000 /
;

: Get.Param.Desc 
 ( theEvent AEKey DescType AEDesc -- OSErr )
 0 4 bury ( space for result)
 0812 AEPack 10000 /
;

: Count.Items ( DescList theCount -- OSErr )
 0 2 bury ( space for result)
 0407 AEPack 10000 /
;

: Get.Nth.Ptr ( DescList index desiredType theAEKey
 typeCode dataPtr maximumSize actualSize -- OSErr )
 0 8 bury ( space for result )
 100A AEPack 10000 /
;
 
: Dispose.Desc ( AEDesc -- OSErr )
 0 swap 0204 AEPack 10000 /
;
decimal


\ global variables

create myFSS 70 allot
create docList 8 allot


\ the ‘quit’ handler does not call bye directly (this leads to
\ crashes), but just puts a token to a ‘bye’ routine into 
\ EVENT.FILTER. The ‘bye’ routine then sets the filter
\ back to ‘off’ again, so that it won’t be called all over 
\ again in case the user cancels the bye process.

\ our ‘bye’ routine. 

: ciao 
 event.filter off
 bye
;


\ the quit handler itself

filter: AEquit
 locals| refcon reply theEvent |
 token.for ciao event.filter !
;filter


\ ‘odoc’ handler, uses routines from the Sibley editor

filter: AEodoc
 0 0 0 0 
 locals| actualSize descType myKey #items 
 refcon reply theEvent |
 
 theEvent  ascii ---- ( DirectObject )
 ascii list ( AEList ) docList
 Get.Param.Desc 
 if ." GetParamDesc error" cr then
 
 docList addr.of #items  
 Count.Items
 if ." CountItems error" cr then
 
 #items 1+ 1 do
 docList i ascii fss_ addr.of myKey 
 addr.of descType myFSS 70 addr.of actualSize  
 Get.Nth.Ptr
 if ." Get.Nth.Ptr error" cr then
 myFSS 6+ myFSS w@ myFSS 2+ @ se.open$ 
 loop
 
 docList Dispose.Desc
 if ." Dispose.Desc error" cr then
;filter


\ ‘pdoc’ handler, very similar to the ‘odoc’ handler (some
\ code could be factored out here). Uses the printing 
\ routine from ‘Paper’

filter: AEpdoc
 0 0 0 0 
 locals| actualSize descType myKey #items 
 refcon reply theEvent |
 
 theEvent  ascii ---- ( DirectObject )
 ascii list ( AEList ) docList
 Get.Param.Desc 
 if ." GetParamDesc error" cr then
 
 docList addr.of #items  
 Count.Items
 if ." CountItems error" cr then
 
 #items 1+ 1 do
 docList i ascii fss_ addr.of myKey addr.of descType
 myFSS 70 addr.of actualSize  
 Get.Nth.Ptr
 if ." Get.Nth.Ptr error" cr then
 myFSS w@ volref# !
 myFSS 2+ @ ioDir !
 myFSS 6+ (paper) 
 loop
 
 docList Dispose.Desc
 if ." Dispose.Desc error" cr then
;filter

\ space for filter routine return stack
256 allot here 64 - FilterRP !


\ word to install the handlers. We have not provided the 
\ ‘oapp’ handler, but put in there anything you like

: install.req.handlers
   ascii aevt ascii quit AEquit Install.Event.Handler drop
\ ascii aevt ascii oapp AEoapp Install.Event.Handler drop
   ascii aevt ascii odoc AEodoc Install.Event.Handler drop
   ascii aevt ascii pdoc AEpdoc Install.Event.Handler drop
;

\ we need to catch the high level event. MacForth allows to 
\ easily hook new event handlers into the main event loop, 
\ through the Eventer executable chain.
 
Eventer Linked
: AEDispatch
 dup
 23 = if
 Process.Apple.Event drop
 drop 0 true
 else
 false
 then
;

\ finally, install the handlers
install.req.handlers

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Hazel 5.3 - Create rules for organizing...
Hazel is your personal housekeeper, organizing and cleaning folders based on rules you define. Hazel can also manage your trash and uninstall your applications. Organize your files using a familiar... Read more
Duet 3.15.0.0 - Use your iPad as an exte...
Duet is the first app that allows you to use your iDevice as an extra display for your Mac using the Lightning or 30-pin cable. Note: This app requires a iOS companion app. Release notes were... Read more
DiskCatalogMaker 9.0.3 - Catalog your di...
DiskCatalogMaker is a simple disk management tool which catalogs disks. Simple, light-weight, and fast Finder-like intuitive look and feel Super-fast search algorithm Can compress catalog data for... Read more
Maintenance 3.1.2 - System maintenance u...
Maintenance is a system maintenance and cleaning utility. It allows you to run miscellaneous tasks of system maintenance: Check the the structure of the disk Repair permissions Run periodic scripts... Read more
Final Cut Pro 10.7 - Professional video...
Redesigned from the ground up, Final Cut Pro combines revolutionary video editing with a powerful media organization and incredible performance to let you create at the speed of thought.... Read more
Pro Video Formats 2.3 - Updates for prof...
The Pro Video Formats package provides support for the following codecs that are used in professional video workflows: Apple ProRes RAW and ProRes RAW HQ Apple Intermediate Codec Avid DNxHD® / Avid... Read more
Apple Safari 17.1.2 - Apple's Web b...
Apple Safari is Apple's web browser that comes bundled with the most recent macOS. Safari is faster and more energy efficient than other browsers, so sites are more responsive and your notebook... Read more
LaunchBar 6.18.5 - Powerful file/URL/ema...
LaunchBar is an award-winning productivity utility that offers an amazingly intuitive and efficient way to search and access any kind of information stored on your computer or on the Web. It provides... Read more
Affinity Designer 2.3.0 - Vector graphic...
Affinity Designer is an incredibly accurate vector illustrator that feels fast and at home in the hands of creative professionals. It intuitively combines rock solid and crisp vector art with... Read more
Affinity Photo 2.3.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more

Latest Forum Discussions

See All

New ‘Zenless Zone Zero’ Trailer Showcase...
I missed this late last week, but HoYoverse released another playable character trailer for the upcoming urban fantasy action RPG Zenless Zone Zero. We’ve had a few trailers so far for playable characters, but the newest one focuses on Nicole... | Read more »
Get your heart pounding quicker as Autom...
When it comes to mobile battle royales, it wouldn’t be disrespectful to say the first ones to pop to mind would be PUBG Mobile, but there is one that perhaps doesn’t get the worldwide recognition it should and that's Garena’s Free Fire. Now,... | Read more »
‘Disney Dreamlight Valley Arcade Edition...
Disney Dreamlight Valley Arcade Edition () launches beginning Tuesday worldwide on Apple Arcade bringing a new version of the game without any passes or microtransactions. While that itself is a huge bonus for Apple Arcade, the fact that Disney... | Read more »
Adventure Game ‘Urban Legend Hunters 2:...
Over the weekend, publisher Playism announced a localization of the Toii Games-developed adventure game Urban Legend Hunters 2: Double for iOS, Android, and Steam. Urban Legend Hunters 2: Double is available on mobile already without English and... | Read more »
‘Stardew Valley’ Creator Has Made a Ton...
Stardew Valley ($4.99) game creator Eric Barone (ConcernedApe) has been posting about the upcoming major Stardew Valley 1.6 update on Twitter with reveals for new features, content, and more. Over the weekend, Eric Tweeted that a ton of progress... | Read more »
Pour One Out for Black Friday – The Touc...
After taking Thanksgiving week off we’re back with another action-packed episode of The TouchArcade Show! Well, maybe not quite action-packed, but certainly discussion-packed! The topics might sound familiar to you: The new Steam Deck OLED, the... | Read more »
TouchArcade Game of the Week: ‘Hitman: B...
Nowadays, with where I’m at in my life with a family and plenty of responsibilities outside of gaming, I kind of appreciate the smaller-scale mobile games a bit more since more of my “serious" gaming is now done on a Steam Deck or Nintendo Switch.... | Read more »
SwitchArcade Round-Up: ‘Batman: Arkham T...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for December 1st, 2023. We’ve got a lot of big games hitting today, new DLC For Samba de Amigo, and this is probably going to be the last day this year with so many heavy hitters. I... | Read more »
Steam Deck Weekly: Tales of Arise Beyond...
Last week, there was a ton of Steam Deck coverage over here focused on the Steam Deck OLED. | Read more »
World of Tanks Blitz adds celebrity amba...
Wargaming is celebrating the season within World of Tanks Blitz with a new celebrity ambassador joining this year's Holiday Ops. In particular, British footballer and movie star Vinnie Jones will be brightening up the game with plenty of themed in-... | Read more »

Price Scanner via MacPrices.net

Apple M1-powered iPad Airs are back on Holida...
Amazon has 10.9″ M1 WiFi iPad Airs back on Holiday sale for $100 off Apple’s MSRP, with prices starting at $499. Each includes free shipping. Their prices are the lowest available among the Apple... Read more
Sunday Sale: Apple 14-inch M3 MacBook Pro on...
B&H Photo has new 14″ M3 MacBook Pros, in Space Gray, on Holiday sale for $150 off MSRP, only $1449. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8-Core M3 MacBook Pro (8GB... Read more
Blue 10th-generation Apple iPad on Holiday sa...
Amazon has Apple’s 10th-generation WiFi iPad (in Blue) on Holiday sale for $349 including free shipping. Their discount applies to WiFi models only and includes a $50 instant discount + $50 clippable... Read more
All Apple Pencils are on Holiday sale for $79...
Amazon has all Apple Pencils on Holiday sale this weekend for $79, ranging up to 39% off MSRP for some models. Shipping is free: – Apple Pencil 1: $79 $20 off MSRP (20%) – Apple Pencil 2: $79 $50 off... Read more
Deal Alert! Apple Smart Folio Keyboard for iP...
Apple iPad Smart Keyboard Folio prices are on Holiday sale for only $79 at Amazon, or 50% off MSRP: – iPad Smart Folio Keyboard for iPad (7th-9th gen)/iPad Air (3rd gen): $79 $79 (50%) off MSRP This... Read more
Apple Watch Series 9 models are now on Holida...
Walmart has Apple Watch Series 9 models now on Holiday sale for $70 off MSRP on their online store. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
Holiday sale this weekend at Xfinity Mobile:...
Switch to Xfinity Mobile (Mobile Virtual Network Operator..using Verizon’s network) and save $500 instantly on any iPhone 15, 14, or 13 and up to $800 off with eligible trade-in. The total is applied... Read more
13-inch M2 MacBook Airs with 512GB of storage...
Best Buy has the 13″ M2 MacBook Air with 512GB of storage on Holiday sale this weekend for $220 off MSRP on their online store. Sale price is $1179. Price valid for online orders only, in-store price... Read more
B&H Photo has Apple’s 14-inch M3/M3 Pro/M...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on Holiday sale this weekend for $100-$200 off MSRP, starting at only $1499. B&H offers free 1-2 day delivery to most... Read more
15-inch M2 MacBook Airs are $200 off MSRP on...
Best Buy has Apple 15″ MacBook Airs with M2 CPUs in stock and on Holiday sale for $200 off MSRP on their online store. Their prices are among the lowest currently available for new 15″ M2 MacBook... Read more

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Senior Product Manager - *Apple* - DISH Net...
…Responsibilities** We are seeking an ambitious, data-driven thinker to assist the Apple Product Development team as our Wireless Product division continues to grow Read more
Senior Product Manager - *Apple* - DISH Net...
…Responsibilities** We are seeking an ambitious, data-driven thinker to assist the Apple Product Development team as our Wireless Product division continues to grow Read more
Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.