From: Daniel A. S. <st...@ic...> - 2002-03-06 13:16:07
|
some more info on all this from steve z on carbondev At 18:47 -0800 17/1/02, car...@li... wrote: >Message: 23 >Date: Thu, 17 Jan 2002 17:53:53 -0800 >Subject: Re: Sending AppleEvents to a OS X Unix Tool Deamon >From: Steve Zellers <ze...@ap...> >To: Kris Amico <kr...@co...>, Carbon_Development > <car...@li...>, <dbu...@ma...>, Ben Hines > <bh...@al...> > >ApplicationServices and CoreServices can both be used by Daemons, although >all the edge cases haven't been worked out. And there will be edge cases >;-) Carbon.framework cannot be used in a Daemon since it requires a >connection to the window server. (And ApplicationServices.framework and >CoreServices.framework are both part of the Carbon API and are thus relevant >to this group ;-) > >That being said, a command line tool that wants to field incoming >AppleEvents have a few options. Either case requires fiddling with mach >ports directly. The easiest path is: > > #include <AE/AEMach.h> > >static void _aeMachPortCallback(CFMachPortRef port, void *msg, CFIndex size, >void *info) > { > AEProcessMessage((mach_msg_header_t*) msg); > } > > AEInstallEventHandler(...) > > mach_port_t port = AEGetRegisteredMachPort(); > > machPortRef = CFMachPortCreateWithPort(NULL, port, _aeMachPortCallback, >NULL, NULL); > > if (machPortRef != NULL) { > CFRunLoopSourceRef runLoopSource = >CFMachPortCreateRunLoopSource(NULL, machPortRef, 0); > CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, >kCFRunLoopDefaultMode); > } > > CFRunLoopRun(); // <- never returns > > Clients can then target your daemon by sending to typeKernelProcessID >(assuming they can find your pid, which you probably wrote to a file in >/var/tmp, or /var/run or somesuch) > >OR: > > mach_port_t port = makeAndRegisterMyOwnMachPort(); > > // wire up as above... Clients access the daemon by > // looking up the name that the daemon registered in the bootstrap >server and creating the target address as typeMachPort. > >OR: > // either of the above to get a port, and then: > > addToPortSet(port, myPortSet); > mach_msg_header_t* header = malloc(kLargeBufferSize); > mach_msg(...myPortSet...) > > if (AEProcessMessage(header) != noErr) > handleMyOwnMessage(header) > >IF you don't know anything about mach_msg (and you wouldn't normally, its >not part of carbon) then none of these approaches are particularly happy. > >I'll try and put together an example in the next couple of days since I keep >getting asked for it. > >Note that none of these approaches make you AppleScriptable, which is a >frequent request as well. AppleScript find processes by looking up their >PSN based on the target name or signature. Daemons do not have PSNs and so >are not found. > >--smz > -- ** Daniel A. Steffen ** "And now to something completely ** Department of Mathematics ** different" Monty Python ** Macquarie University ** <mailto:st...@ma...> ** NSW 2109 Australia ** <http://www.maths.mq.edu.au/~steffen/> |